Skip to content

Commit 359a8e3

Browse files
committed
Fixes
1 parent 529042d commit 359a8e3

File tree

5 files changed

+29
-11
lines changed

5 files changed

+29
-11
lines changed

src/Bridges/Bridges.jl

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,12 @@ function _test_dual(
307307
Bridge::Type{<:AbstractBridge},
308308
input_fn::Function;
309309
dual,
310+
eltype,
310311
model_eltype,
311312
)
312313
inner = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{model_eltype}())
313314
mock = MOI.Utilities.MockOptimizer(inner)
314-
model = _bridged_model(Bridge{model_eltype}, mock)
315+
model = _bridged_model(Bridge{eltype}, mock)
315316
input_fn(model)
316317
final_touch(model)
317318
# Should be able to call final_touch multiple times.
@@ -331,17 +332,21 @@ function _test_dual(
331332
MOI.set(model, MOI.ConstraintDual(), ci, _fake_start(dual, set))
332333
end
333334
end
334-
model_dual = MOI.Utilities.get_fallback(
335-
model,
336-
MOI.DualObjectiveValue(),
337-
model_eltype,
338-
)
335+
model_dual =
336+
MOI.Utilities.get_fallback(model, MOI.DualObjectiveValue(), eltype)
339337
mock_dual =
340-
MOI.Utilities.get_fallback(mock, MOI.DualObjectiveValue(), model_eltype)
338+
MOI.Utilities.get_fallback(mock, MOI.DualObjectiveValue(), eltype)
341339
# Need `atol` in case one of them is zero and the other one almost zero
342340
Test.@test model_dual mock_dual atol = 1e-6
343341
end
344342

343+
function _check_bridged(model; no_bridge_used)
344+
unused = isempty(MOI.Bridges.Variable.bridges(model)) &&
345+
isempty(MOI.Bridges.Constraint.bridges(model)) &&
346+
isempty(MOI.Bridges.Objective.bridges(model))
347+
Test.@test unused == no_bridge_used
348+
end
349+
345350
function _runtests(
346351
Bridge::Type{<:AbstractBridge},
347352
input_fn::Function,
@@ -353,17 +358,22 @@ function _runtests(
353358
model_eltype = eltype,
354359
print_inner_model::Bool = false,
355360
cannot_unbridge::Bool = false,
361+
no_bridge_used::Bool = false,
356362
)
357363
# Load model and bridge it
358364
inner = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{model_eltype}())
359365
model = _bridged_model(Bridge{eltype}, inner)
360366
input_fn(model)
367+
_check_bridged(model; no_bridge_used)
361368
final_touch(model)
362369
# Should be able to call final_touch multiple times.
363370
final_touch(model)
364371
if print_inner_model
365372
print(inner)
366373
end
374+
if no_bridge_used
375+
return
376+
end
367377
Test.@testset "Test outer bridged model appears like the input" begin # COV_EXCL_LINE
368378
test =
369379
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{model_eltype}())
@@ -455,7 +465,7 @@ function _runtests(
455465
end
456466
if !isnothing(dual)
457467
Test.@testset "Test ConstraintDual" begin
458-
_test_dual(Bridge, input_fn; dual, model_eltype)
468+
_test_dual(Bridge, input_fn; dual, eltype, model_eltype)
459469
end
460470
end
461471
return

src/Utilities/results.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,14 @@ function _dual_objective_value(
8888
)
8989
end
9090

91+
_lower(set::MOI.Interval, ::Type) = set.lower
92+
_upper(set::MOI.Interval, ::Type) = set.upper
93+
_lower(::MOI.ZeroOne, ::Type{T}) where {T} = zero(T)
94+
_upper(::MOI.ZeroOne, ::Type{T}) where {T} = one(T)
95+
9196
function _dual_objective_value(
9297
model::MOI.ModelLike,
93-
ci::MOI.ConstraintIndex{<:MOI.AbstractScalarFunction,<:MOI.Interval},
98+
ci::MOI.ConstraintIndex{<:MOI.AbstractScalarFunction,<:Union{MOI.ZeroOne,MOI.Interval}},
9499
::Type{T},
95100
result_index::Integer,
96101
) where {T}
@@ -100,10 +105,10 @@ function _dual_objective_value(
100105
if dual < zero(dual)
101106
# The dual is negative so it is in the dual of the MOI.LessThan cone
102107
# hence the upper bound of the Interval set is tight
103-
constant -= set.upper
108+
constant -= _upper(set, T)
104109
else
105110
# the lower bound is tight
106-
constant -= set.lower
111+
constant -= _lower(set, T)
107112
end
108113
return set_dot(constant, dual, set)
109114
end

test/Bridges/Constraint/IndicatorSOS1Bridge.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ function test_runtests()
345345
[z, 2.0 * x * x] in Indicator{ACTIVATE_ON_ONE}(LessThan(2.0))
346346
z in ZeroOne()
347347
""",
348+
no_bridge_used = true,
348349
)
349350
return
350351
end

test/Bridges/Constraint/NumberConversionBridge.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ function test_runtests()
104104
variables: x
105105
::Float64: [x, x] in Zeros(2)
106106
""",
107+
no_bridge_used = true,
107108
)
108109
# VectorAffineFunction
109110
MOI.Bridges.runtests(

test/Bridges/Constraint/ScalarFunctionizeBridge.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ function test_FunctionConversionBridge()
317317
variables: x, y
318318
ScalarNonlinearFunction(1.0 * x * x + 2.0 * x * y + 3.0 * y + 4.0) >= 1.0
319319
""",
320+
dual = nothing, # `get_fallback` ignores the constant `4.0` of the function
320321
)
321322
# VectorAffineFunction -> VectorQuadraticFunction
322323
MOI.Bridges.runtests(

0 commit comments

Comments
 (0)