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

Add Variable bridges to use runtests without unbridged_variable #2498

Merged
merged 6 commits into from
Jun 25, 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
37 changes: 33 additions & 4 deletions src/Bridges/Bridges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@

include("precompile.jl")

function _test_structural_identical(a::MOI.ModelLike, b::MOI.ModelLike)
function _test_structural_identical(
a::MOI.ModelLike,
b::MOI.ModelLike;
cannot_unbridge::Bool = false,
)
# Test that the variables are the same. We make the strong assumption that
# the variables are added in the same order to both models.
a_x = MOI.get(a, MOI.ListOfVariableIndices())
Expand Down Expand Up @@ -190,7 +194,16 @@
Test.@test MOI.supports_constraint(b, F, S)
# Check that each function in `b` matches a function in `a`
for ci in MOI.get(b, MOI.ListOfConstraintIndices{F,S}())
f_b = MOI.get(b, MOI.ConstraintFunction(), ci)
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
end
f_b = MOI.Utilities.map_indices(x_map, f_b)
s_b = MOI.get(b, MOI.ConstraintSet(), ci)
# We don't care about the order that constraints are added, only
Expand Down Expand Up @@ -225,13 +238,18 @@
variable_start = 1.2,
constraint_start = 1.2,
eltype = Float64,
cannot_unbridge::Bool = false,
)

Run a series of tests that check the correctness of `Bridge`.

`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.

## Example

```jldoctest; setup=:(import MathOptInterface as MOI)
Expand All @@ -253,6 +271,7 @@
constraint_start = 1.2,
eltype = Float64,
print_inner_model::Bool = false,
cannot_unbridge::Bool = false,
)
# Load model and bridge it
inner = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{eltype}())
Expand All @@ -267,7 +286,7 @@
# Load a non-bridged input model, and check that getters are the same.
test = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{eltype}())
input_fn(test)
_test_structural_identical(test, model)
_test_structural_identical(test, model; cannot_unbridge = cannot_unbridge)
# Load a bridged target model, and check that getters are the same.
target = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{eltype}())
output_fn(target)
Expand All @@ -291,7 +310,17 @@
# Test ConstraintPrimalStart and ConstraintDualStart
for (F, S) in MOI.get(model, MOI.ListOfConstraintTypesPresent())
for ci in MOI.get(model, MOI.ListOfConstraintIndices{F,S}())
set = MOI.get(model, MOI.ConstraintSet(), ci)
set = try
MOI.get(model, MOI.ConstraintSet(), ci)
catch err
# Could be thrown by `unbridged_function`
if cannot_unbridge &&
err isa MOI.GetAttributeNotAllowed{MOI.ConstraintFunction}
continue
else
rethrow(err)

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

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Bridges.jl#L321

Added line #L321 was not covered by tests
end
end
for attr in (MOI.ConstraintPrimalStart(), MOI.ConstraintDualStart())
if MOI.supports(model, attr, MOI.ConstraintIndex{F,S})
MOI.set(model, attr, ci, nothing)
Expand Down
23 changes: 23 additions & 0 deletions test/Bridges/Variable/zeros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,29 @@ end

include("../utilities.jl")

function test_runtests()
MOI.Bridges.runtests(
MOI.Bridges.Variable.ZerosBridge,
model -> begin
x, _ = MOI.add_constrained_variables(model, MOI.Zeros(2))
MOI.add_constraint(
model,
1.0 * x[1] + 2.0 * x[2],
MOI.EqualTo(3.0),
)
end,
model -> begin
MOI.add_constraint(
model,
zero(MOI.ScalarAffineFunction{Float64}),
MOI.EqualTo(3.0),
)
end;
cannot_unbridge = true,
)
return
end

function test_zeros()
mock = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
bridged_mock = MOI.Bridges.Variable.Zeros{Float64}(mock)
Expand Down
Loading