Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release-0.18] Fix objective constant bug in conic problems #1727

Merged
merged 3 commits into from
Jan 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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