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 getting ConstraintPrimal if Variable bridges are present #2396

Merged
merged 4 commits into from
Jan 11, 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
25 changes: 25 additions & 0 deletions src/Bridges/bridge_optimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,31 @@ function MOI.get(
return unbridged_function(b, func)
end

function MOI.get(
b::AbstractBridgeOptimizer,
attr::MOI.ConstraintPrimal,
ci::MOI.ConstraintIndex{F,S},
) where {F<:MOI.AbstractScalarFunction,S<:MOI.AbstractScalarSet}
if is_bridged(b, ci)
MOI.throw_if_not_valid(b, ci)
return call_in_context(MOI.get, b, ci, attr)
end
if Variable.has_bridges(Variable.bridges(b))
# In this case, the scalar constraint might contain bridged variables
# that include constants, like `x >= 1` being mapped to `y >= 0`,
# `x := y + 1`. If this is true, then `normalize_and_add_constraint`
# may move the constants into the set, and querying `ConstraintPrimal`
# from `b.model` will return the value of `y`, not `y + 1`. As a
# work-around, we use `get_fallback`, which first queries
# `ConstraintFunction` and then evaluates the function at the point
# defined by `VariablePrimal`. Querying `ConstraintFunction` accounts
# for the case where constants were moved to the set, so we return the
# correct value.
return MOI.Utilities.get_fallback(b, attr, ci)
end
return MOI.get(b.model, attr, ci)
end

function MOI.supports(
b::AbstractBridgeOptimizer,
attr::MOI.AbstractConstraintAttribute,
Expand Down
18 changes: 18 additions & 0 deletions test/Bridges/lazy_bridge_optimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,24 @@ function test_MOI_runtests_GeometricSDPAModel()
return
end

function test_get_primal_variable_bridge()
cache = MOI.Utilities.Model{Float64}()
x, c1 = MOI.add_constrained_variable(cache, MOI.GreaterThan(1.0))
c2 = MOI.add_constraint(cache, 1.0x, MOI.EqualTo(2.0))
inner = MOI.Utilities.MockOptimizer(StandardSDPAModel{Float64}())
model = MOI.Bridges.full_bridge_optimizer(inner, Float64)
index_map = MOI.copy_to(model, cache)
MOI.Utilities.set_mock_optimize!(
inner,
mock -> MOI.Utilities.mock_optimize!(mock, [1.0]),
)
MOI.optimize!(model)
@test MOI.get(model, MOI.VariablePrimal(), index_map[x]) == 2.0
@test MOI.get(model, MOI.ConstraintPrimal(), index_map[c1]) == 2.0
@test MOI.get(model, MOI.ConstraintPrimal(), index_map[c2]) == 2.0
return
end

function test_index_constraint_conflict()
optimizer = StandardSDPAModel{Float64}()
model = MOI.Bridges.full_bridge_optimizer(optimizer, Float64)
Expand Down
Loading