Skip to content

Commit

Permalink
[Bridges] fix querying result attributes with result_index != 1 (#2583)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Nov 29, 2024
1 parent 02dc52c commit c4fbc48
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 10 deletions.
2 changes: 0 additions & 2 deletions src/Bridges/Constraint/bridges/det.jl
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,6 @@ function MOI.get(
attr::Union{MOI.ConstraintPrimal,MOI.ConstraintPrimalStart},
bridge::LogDetBridge,
)
d = length(bridge.lcindex)
Δ = MOI.get(model, MOI.VariablePrimal(), bridge.Δ)
t =
MOI.get(model, attr, bridge.tlindex) +
sum(MOI.get(model, attr, ci)[1] for ci in bridge.lcindex)
Expand Down
10 changes: 5 additions & 5 deletions src/Bridges/Constraint/bridges/norm_spec_nuc_to_psd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,10 @@ end
# constraint is (tr(U) + tr(V), 2X) in NormNuclearCone.
function MOI.get(
model::MOI.ModelLike,
::MOI.ConstraintDual,
attr::MOI.ConstraintDual,
bridge::NormSpectralBridge,
)
dual = MOI.get(model, MOI.ConstraintDual(), bridge.psd_index)
dual = MOI.get(model, attr, bridge.psd_index)
column_dim = bridge.column_dim
side_dim = bridge.row_dim + column_dim
t = sum(dual[MOI.Utilities.trimap(i, i)] for i in 1:side_dim)
Expand Down Expand Up @@ -450,11 +450,11 @@ end

function MOI.get(
model::MOI.ModelLike,
::MOI.ConstraintPrimal,
attr::MOI.ConstraintPrimal,
bridge::NormNuclearBridge,
)
ge_primal = MOI.get(model, MOI.ConstraintPrimal(), bridge.ge_index)
psd_primal = MOI.get(model, MOI.ConstraintPrimal(), bridge.psd_index)
ge_primal = MOI.get(model, attr, bridge.ge_index)
psd_primal = MOI.get(model, attr, bridge.psd_index)
side_dim = bridge.row_dim + bridge.column_dim
t =
ge_primal +
Expand Down
7 changes: 5 additions & 2 deletions src/Bridges/Objective/bridges/slack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,11 @@ function MOI.get(
# value of `bridge.slack`. Since `bridge.constraint` is `g - slack`, we can
# get the value of the original objective by summing the value of `slack`
# with the `ConstraintPrimal` of the constraint.
obj_slack_constant =
MOI.get(model, MOI.ConstraintPrimal(), bridge.constraint)
obj_slack_constant = MOI.get(
model,
MOI.ConstraintPrimal(attr.result_index),
bridge.constraint,
)
return obj_slack_constant + slack + bridge.constant
end

Expand Down
6 changes: 5 additions & 1 deletion src/Bridges/Objective/bridges/vector_slack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,11 @@ function MOI.get(
N = attr_g.result_index
attr_f = MOI.Bridges.ObjectiveFunctionValue{MOI.VectorOfVariables}(N)
objective_value = MOI.get(model, attr_f)
con_primal = MOI.get(model, MOI.ConstraintPrimal(), bridge.constraint)
con_primal = MOI.get(
model,
MOI.ConstraintPrimal(attr_g.result_index),
bridge.constraint,
)
sense = MOI.get(model, MOI.ObjectiveSense())
if sense == MOI.MIN_SENSE
# con_primal = objective_value - f(x)
Expand Down
28 changes: 28 additions & 0 deletions test/Bridges/Objective/slack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,34 @@ function test_SlackBridge_ObjectiveFunctionValue()
return
end

function test_SlackBridge_ObjectiveFunctionValue_2()
inner = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
model = MOI.Bridges.Objective.Slack{Float64}(inner)
x = MOI.add_variable(model)
MOI.add_constraint(model, x, MOI.GreaterThan(1.0))
f = 1.1 * x - 1.2
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.Utilities.set_mock_optimize!(
inner,
mock -> begin
MOI.set(mock, MOI.ResultCount(), 2)
MOI.set(mock, MOI.TerminationStatus(), MOI.OPTIMAL)
MOI.set(mock, MOI.PrimalStatus(1), MOI.FEASIBLE_POINT)
MOI.set(mock, MOI.PrimalStatus(2), MOI.FEASIBLE_POINT)
y = MOI.get(mock, MOI.ListOfVariableIndices())
MOI.set.(mock, MOI.VariablePrimal(1), y, [1.0, -0.1])
MOI.set.(mock, MOI.VariablePrimal(2), y, [2.0, 1.0])
end,
)
MOI.optimize!(model)
@test MOI.get(model, MOI.ObjectiveValue(1)) -0.1
@test MOI.get(model, MOI.VariablePrimal(1), x) 1.0
@test MOI.get(model, MOI.ObjectiveValue(2)) 1.0
@test MOI.get(model, MOI.VariablePrimal(2), x) 2.0
return
end

function test_original()
mock = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
bridged_mock = MOI.Bridges.Objective.Slack{Float64}(mock)
Expand Down
28 changes: 28 additions & 0 deletions test/Bridges/Objective/vector_slack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,34 @@ function test_modify_vector_constant_change()
return
end

function test_SlackBridge_ObjectiveFunctionValue_2()
inner = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
model = MOI.Bridges.Objective.VectorSlack{Float64}(inner)
x = MOI.add_variable(model)
MOI.add_constraint(model, x, MOI.GreaterThan(1.0))
f = MOI.Utilities.operate(vcat, Float64, 1.1 * x - 1.2)
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.Utilities.set_mock_optimize!(
inner,
mock -> begin
MOI.set(mock, MOI.ResultCount(), 2)
MOI.set(mock, MOI.TerminationStatus(), MOI.OPTIMAL)
MOI.set(mock, MOI.PrimalStatus(1), MOI.FEASIBLE_POINT)
MOI.set(mock, MOI.PrimalStatus(2), MOI.FEASIBLE_POINT)
y = MOI.get(mock, MOI.ListOfVariableIndices())
MOI.set.(mock, MOI.VariablePrimal(1), y, [1.0, -0.1])
MOI.set.(mock, MOI.VariablePrimal(2), y, [2.0, 1.0])
end,
)
MOI.optimize!(model)
@test MOI.get(model, MOI.ObjectiveValue(1)) [-0.1]
@test MOI.get(model, MOI.VariablePrimal(1), x) 1.0
@test MOI.get(model, MOI.ObjectiveValue(2)) [1.0]
@test MOI.get(model, MOI.VariablePrimal(2), x) 2.0
return
end

end # module

TestObjectiveVectorSlack.runtests()

0 comments on commit c4fbc48

Please sign in to comment.