Skip to content

Commit

Permalink
[release-0.18] Fix objective constant bug in conic problems (#1727)
Browse files Browse the repository at this point in the history
* Fix objective constant bug in conic problems

* [ci skip] handle constant verbosely for clarity

* Update sdp.jl
  • Loading branch information
odow authored and mlubin committed Jan 5, 2019
1 parent 6228547 commit d8f526d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/solvers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,13 @@ function solve(m::Model; suppress_warnings=false,
end
end

# The MathProgBase interface defines a conic problem to always be
# a minimization problem, so we need to flip the objective before
# reporting it to the user
# The MathProgBase interface defines a conic problem to always be a
# minimization problem, so we need to flip the objective before reporting it
# to the user. We also need to account for the objective constant which was
# added above by subtracting it prior to the flip and then re-adding it.
if traits.conic && m.objSense == :Max
m.objBound *= -1
m.objVal *= -1
m.objBound = -1 * (m.objBound - m.obj.aff.constant) + m.obj.aff.constant
m.objVal = -1 * (m.objVal - m.obj.aff.constant) + m.obj.aff.constant
end

# If the solver was initially not set, we will restore this status
Expand Down
28 changes: 28 additions & 0 deletions test/sdp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -770,4 +770,32 @@ ispsd(x::JuMP.JuMPArray) = ispsd(x.innerArray)
@test norm(getvalue(X)) < 1e-5
@test isapprox(getdual(X), Matrix(1.0I, 3, 3), atol=1e-5)
end

@testset "Objective constants with $solver" for solver in sdp_solvers
# Test handling of constants in the objective. See JuMP issue #1390.
@testset "Minimize" begin
model = Model(solver = solver)
@variable(model, Xs1[1:3, 1:3], SDP)
@variable(model, Xs2[1:1, 1:1], SDP)
@variable(model, Xs3[1:1, 1:1], SDP)
@constraint(model, Xs1[1, 1] == 1)
@constraint(model, Xs2[1, 1] + Xs1[2, 2] + Xs1[3, 3] == 4)
@constraint(model, Xs3[1, 1] - Xs1[2, 1] - Xs1[3, 1] == 0)
@objective(model, Min, -Xs1[1, 3] + 0.5)
@test solve(model) == :Optimal
@test isapprox(getobjectivevalue(model), -1.5000, atol=1e-3)
end
@testset "Maximize" begin
model = Model(solver = solver)
@variable(model, Xs1[1:3, 1:3], SDP)
@variable(model, Xs2[1:1, 1:1], SDP)
@variable(model, Xs3[1:1, 1:1], SDP)
@constraint(model, Xs1[1, 1] == 1)
@constraint(model, Xs2[1, 1] + Xs1[2, 2] + Xs1[3, 3] == 4)
@constraint(model, Xs3[1, 1] - Xs1[2, 1] - Xs1[3, 1] == 0)
@objective(model, Max, -Xs1[1, 3] + 0.5)
@test solve(model) == :Optimal
@test isapprox(getobjectivevalue(model), 1.9142, atol=1e-3)
end
end
end

0 comments on commit d8f526d

Please sign in to comment.