Skip to content

Commit ee9643e

Browse files
authored
[Bridges] fix support for Rational in SplitIntervalBridge (#2137)
1 parent a624e06 commit ee9643e

File tree

3 files changed

+76
-13
lines changed

3 files changed

+76
-13
lines changed

src/Bridges/Bridges.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ end
224224
output::String;
225225
variable_start = 1.2,
226226
constraint_start = 1.2,
227+
eltype = Float64,
227228
)
228229
229230
Run a series of tests that check the correctness of `Bridge`.
@@ -254,20 +255,21 @@ function runtests(
254255
output::String;
255256
variable_start = 1.2,
256257
constraint_start = 1.2,
258+
eltype = Float64,
257259
)
258260
# Load model and bridge it
259-
inner = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
260-
model = _bridged_model(Bridge, inner)
261+
inner = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{eltype}())
262+
model = _bridged_model(Bridge{eltype}, inner)
261263
MOI.Utilities.loadfromstring!(model, input)
262264
final_touch(model)
263265
# Should be able to call final_touch multiple times.
264266
final_touch(model)
265267
# Load a non-bridged input model, and check that getters are the same.
266-
test = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
268+
test = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{eltype}())
267269
MOI.Utilities.loadfromstring!(test, input)
268270
_test_structural_identical(test, model)
269271
# Load a bridged target model, and check that getters are the same.
270-
target = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
272+
target = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{eltype}())
271273
MOI.Utilities.loadfromstring!(target, output)
272274
_test_structural_identical(target, inner)
273275
# Test VariablePrimalStart
@@ -337,15 +339,15 @@ _fake_start(value, ::MOI.AbstractScalarSet) = value
337339
_fake_start(value, set::MOI.AbstractVectorSet) = fill(value, MOI.dimension(set))
338340

339341
function _bridged_model(Bridge::Type{<:Constraint.AbstractBridge}, inner)
340-
return Constraint.SingleBridgeOptimizer{Bridge{Float64}}(inner)
342+
return Constraint.SingleBridgeOptimizer{Bridge}(inner)
341343
end
342344

343345
function _bridged_model(Bridge::Type{<:Objective.AbstractBridge}, inner)
344-
return Objective.SingleBridgeOptimizer{Bridge{Float64}}(inner)
346+
return Objective.SingleBridgeOptimizer{Bridge}(inner)
345347
end
346348

347349
function _bridged_model(Bridge::Type{<:Variable.AbstractBridge}, inner)
348-
return Variable.SingleBridgeOptimizer{Bridge{Float64}}(inner)
350+
return Variable.SingleBridgeOptimizer{Bridge}(inner)
349351
end
350352

351353
function _general_bridge_tests(bridge::B) where {B<:AbstractBridge}

src/Bridges/Constraint/bridges/interval.jl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,22 @@ const SplitInterval{T,OT<:MOI.ModelLike} =
6767
_lower_set(set::MOI.Interval) = MOI.GreaterThan(set.lower)
6868
_upper_set(set::MOI.Interval) = MOI.LessThan(set.upper)
6969

70-
function _lower_set(set::MOI.Interval{T}) where {T<:AbstractFloat}
70+
function _lower_set(
71+
set::MOI.Interval{T},
72+
) where {T<:Union{AbstractFloat,Rational}}
7173
if set.lower == typemin(T)
7274
return nothing
73-
else
74-
return MOI.GreaterThan(set.lower)
7575
end
76+
return MOI.GreaterThan(set.lower)
7677
end
7778

78-
function _upper_set(set::MOI.Interval{T}) where {T<:AbstractFloat}
79+
function _upper_set(
80+
set::MOI.Interval{T},
81+
) where {T<:Union{AbstractFloat,Rational}}
7982
if set.upper == typemax(T)
8083
return nothing
81-
else
82-
return MOI.LessThan(set.upper)
8384
end
85+
return MOI.LessThan(set.upper)
8486
end
8587

8688
_lower_set(set::MOI.EqualTo) = MOI.GreaterThan(set.value)

test/Bridges/Constraint/interval.jl

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,65 @@ function test_runtests()
397397
3.0 * x <= 2.0
398398
""",
399399
)
400+
MOI.Bridges.runtests(
401+
MOI.Bridges.Constraint.SplitIntervalBridge,
402+
"""
403+
variables: x
404+
x in Interval(1.0, Inf)
405+
""",
406+
"""
407+
variables: x
408+
x >= 1.0
409+
""",
410+
)
411+
MOI.Bridges.runtests(
412+
MOI.Bridges.Constraint.SplitIntervalBridge,
413+
"""
414+
variables: x
415+
x in Interval(-Inf, 2.0)
416+
""",
417+
"""
418+
variables: x
419+
x <= 2.0
420+
""",
421+
)
422+
MOI.Bridges.runtests(
423+
MOI.Bridges.Constraint.SplitIntervalBridge,
424+
"""
425+
variables: x
426+
x in Interval(1 // 2, 3 // 2)
427+
""",
428+
"""
429+
variables: x
430+
x in GreaterThan(1 // 2)
431+
x in LessThan(3 // 2)
432+
""",
433+
eltype = Rational{Int},
434+
)
435+
MOI.Bridges.runtests(
436+
MOI.Bridges.Constraint.SplitIntervalBridge,
437+
"""
438+
variables: x
439+
x in Interval(1 // 2, 1 // 0)
440+
""",
441+
"""
442+
variables: x
443+
x in GreaterThan(1 // 2)
444+
""",
445+
eltype = Rational{Int},
446+
)
447+
MOI.Bridges.runtests(
448+
MOI.Bridges.Constraint.SplitIntervalBridge,
449+
"""
450+
variables: x
451+
x in Interval(-1 // 0, 1 // 2)
452+
""",
453+
"""
454+
variables: x
455+
x in LessThan(1 // 2)
456+
""",
457+
eltype = Rational{Int},
458+
)
400459
return
401460
end
402461

0 commit comments

Comments
 (0)