Skip to content

Fix Bridge test for non-invertible constraint bridge #2713

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

Merged
merged 5 commits into from
Apr 12, 2025
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
1 change: 1 addition & 0 deletions docs/src/reference/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ The different [`UnsupportedError`](@ref) and [`NotAllowedError`](@ref) are the
following errors:
```@docs
UnsupportedAttribute
GetAttributeNotAllowed
SetAttributeNotAllowed
AddVariableNotAllowed
UnsupportedConstraint
Expand Down
39 changes: 26 additions & 13 deletions src/Bridges/Bridges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@

MOI.is_copyable(::ListOfNonstandardBridges) = false

MOI.get_fallback(model::MOI.ModelLike, ::ListOfNonstandardBridges) = Type[]
MOI.get_fallback(::MOI.ModelLike, ::ListOfNonstandardBridges) = Type[]

function _test_structural_identical(
a::MOI.ModelLike,
Expand Down Expand Up @@ -191,12 +191,8 @@
f_b = try
MOI.get(b, MOI.ConstraintFunction(), ci)
catch err
if cannot_unbridge &&
err isa MOI.GetAttributeNotAllowed{MOI.ConstraintFunction}
continue
else
rethrow(err)
end
_runtests_error_handler(err, cannot_unbridge)
continue
end
f_b = MOI.Utilities.map_indices(x_map, f_b)
s_b = MOI.get(b, MOI.ConstraintSet(), ci)
Expand Down Expand Up @@ -227,7 +223,10 @@
_runtests_error_handler(err, ::Bool) = rethrow(err)

function _runtests_error_handler(
err::MOI.GetAttributeNotAllowed{MOI.ConstraintFunction},
err::Union{
MOI.GetAttributeNotAllowed{MOI.ConstraintFunction},
MOI.GetAttributeNotAllowed{MOI.ConstraintPrimalStart},
},
cannot_unbridge::Bool,
)
if cannot_unbridge
Expand All @@ -252,9 +251,11 @@
`input_fn` and `output_fn` are functions such that `input_fn(model)`
and `output_fn(model)` load the corresponding model into `model`.

Set `cannot_unbridge` to `true` if the bridge is a variable bridge
for which [`Variable.unbridged_map`](@ref) returns `nothing` so that
the tests allow errors that can be raised due to this.
Set `cannot_unbridge` to `true` if the bridge transformation is not invertible.
If `Bridge` is a variable bridge this allows [`Variable.unbridged_map`](@ref)
to returns `nothing` so that the tests allow errors that can be raised due to this.
If `Bridge` is a constraint bridge this allows the getter of [`MOI.ConstraintFunction`](@ref)
and [`MOI.ConstraintPrimalStart`](@ref) to throw [`MOI.GetAttributeNotAllowed`](@ref).

## Example

Expand Down Expand Up @@ -328,7 +329,19 @@
Test.@test MOI.get(model, attr, ci) === nothing
start = _fake_start(constraint_start, set)
MOI.set(model, attr, ci, start)
Test.@test MOI.get(model, attr, ci) ≈ start
returned_start = try
MOI.get(model, attr, ci)
catch err
# For a Constraint bridge for which the map is not invertible, the constraint primal cannot
# be inverted
_runtests_error_handler(
err,
Bridge <: MOI.Bridges.Constraint.AbstractBridge &&
cannot_unbridge,
)
continue
end
Test.@test returned_start ≈ start
end
end
end
Expand Down Expand Up @@ -416,7 +429,7 @@

_fake_start(value, set::MOI.AbstractVectorSet) = fill(value, MOI.dimension(set))

_fake_start(value::AbstractVector, set::MOI.AbstractVectorSet) = value
_fake_start(value::AbstractVector, ::MOI.AbstractVectorSet) = value

Check warning on line 432 in src/Bridges/Bridges.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Bridges.jl#L432

Added line #L432 was not covered by tests

function _bridged_model(Bridge::Type{<:Constraint.AbstractBridge}, inner)
return Constraint.SingleBridgeOptimizer{Bridge}(inner)
Expand Down
25 changes: 25 additions & 0 deletions test/Bridges/set_map.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ function MOI.Bridges.inverse_map_set(
return bridge.set
end

# Otherwise, it fails Bridges.runtests for `NOT_INVERTIBLE`
function MOI.supports(
::MOI.ModelLike,
::MOI.ConstraintDualStart,
::Type{<:ConstraintSwapBridge},
)
return false
end

const SwapBridge{T} = Union{VariableSwapBridge{T},ConstraintSwapBridge{T}}

function MOI.Bridges.map_function(bridge::SwapBridge, func)
Expand Down Expand Up @@ -214,6 +223,22 @@ function test_runtests()
MOI.add_constraint(model, func, set)
end,
)
MOI.Bridges.runtests(
ConstraintSwapBridge,
model -> begin
x = MOI.add_variables(model, 2)
func = MOI.VectorOfVariables(x)
set = SwapSet(do_swap, NOT_INVERTIBLE)
MOI.add_constraint(model, func, set)
end,
model -> begin
x = MOI.add_variables(model, 2)
func = MOI.VectorOfVariables(swap(x, do_swap))
set = MOI.Nonnegatives(2)
MOI.add_constraint(model, func, set)
end,
cannot_unbridge = true,
)
MOI.Bridges.runtests(
VariableSwapBridge,
model -> begin
Expand Down
Loading