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

[Bridges] fix querying result attributes with result_index != 1 #2583

Merged
merged 2 commits into from
Nov 29, 2024
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
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 @@
# 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)

Check warning on line 206 in src/Bridges/Constraint/bridges/norm_spec_nuc_to_psd.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/norm_spec_nuc_to_psd.jl#L206

Added line #L206 was not covered by tests
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 @@

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)

Check warning on line 457 in src/Bridges/Constraint/bridges/norm_spec_nuc_to_psd.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/norm_spec_nuc_to_psd.jl#L456-L457

Added lines #L456 - L457 were not covered by tests
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 @@
# 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(

Check warning on line 158 in src/Bridges/Objective/bridges/slack.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Objective/bridges/slack.jl#L158

Added line #L158 was not covered by tests
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 @@
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(

Check warning on line 162 in src/Bridges/Objective/bridges/vector_slack.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Objective/bridges/vector_slack.jl#L162

Added line #L162 was not covered by tests
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()
Loading