Skip to content

Commit

Permalink
Allow calling fix! twice in a row (jump-dev#299)
Browse files Browse the repository at this point in the history
* Allow calling `fix!` twice without a `free!`

* Remove `:fixed` and just use `ConstVexity`

* Use getter instead of direct field access
  • Loading branch information
ericphanson committed Jul 22, 2019
1 parent 80ca791 commit ec6e147
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
5 changes: 1 addition & 4 deletions src/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ end

function conic_form!(x::Variable, unique_conic_forms::UniqueConicForms=UniqueConicForms())
if !has_conic_form(unique_conic_forms, x)
if :fixed in x.sets
if vexity(x) == ConstVexity()
# do exactly what we would for a constant
objective = ConicObj()
objective[objectid(:constant)] = (vec([real(x.value);]),vec([imag(x.value);]))
Expand All @@ -115,7 +115,6 @@ end
# fix variables to hold them at their current value, and free them afterwards
function fix!(x::Variable)
x.value === nothing && error("This variable has no value yet; cannot fix value to nothing!")
push!(x.sets, :fixed)
x.vexity = ConstVexity()
x
end
Expand All @@ -127,8 +126,6 @@ end
fix!(x::Variable, v::Number) = fix!(x, fill(v, (1, 1)))

function free!(x::Variable)
# TODO this won't work if :fixed appears other than at the end of x.sets
x.sets[end] == :fixed && pop!(x.sets)
x.vexity = AffineVexity()
x
end
19 changes: 19 additions & 0 deletions test/test_const.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,24 @@
@test prob.optval 0.0 atol = TOL
end

@testset "Test double `fix!`" begin
x = Variable()
y = Variable()
fix!(x, 1.0)
prob = minimize(y*x, [y >= x, x >= 0.5])
solve!(prob, solver)
@test prob.optval 1.0 atol = TOL

fix!(x, 2.0)
solve!(prob, solver)
@test prob.optval 4.0 atol = TOL

free!(x)
fix!(y, 1.0)
solve!(prob, solver)
@test prob.optval 0.5 atol = TOL

end


end

0 comments on commit ec6e147

Please sign in to comment.