Skip to content

Commit 253e16c

Browse files
authored
Merge pull request #454 from JuliaOpt/od/fix-bridge
Fix bug in bridges querying index via string
2 parents c164eab + 1fe897c commit 253e16c

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/Bridges/bridgeoptimizer.jl

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ function isbridged end
2727
function isbridged(b::AbstractBridgeOptimizer, ::Type{CI{F, S}}) where {F, S}
2828
return isbridged(b, F, S)
2929
end
30+
# We don't bridge variables.
31+
isbridged(b::AbstractBridgeOptimizer, ::Type{VI}) = false
3032

3133
"""
3234
supportsbridgingconstraint(b::AbstractBridgeOptimizer,
@@ -318,11 +320,36 @@ end
318320
# Name
319321
function MOI.canget(b::AbstractBridgeOptimizer, IdxT::Type{<:MOI.Index},
320322
name::String)
321-
return MOI.canget(b.model, IdxT, name)
323+
if isbridged(b, IdxT)
324+
return MOI.canget(b.bridged, IdxT, name)
325+
else
326+
return MOI.canget(b.model, IdxT, name)
327+
end
322328
end
323329
function MOI.get(b::AbstractBridgeOptimizer, IdxT::Type{<:MOI.Index},
324330
name::String)
325-
return MOI.get(b.model, IdxT, name)
331+
if isbridged(b, IdxT)
332+
return MOI.get(b.bridged, IdxT, name)
333+
else
334+
return MOI.get(b.model, IdxT, name)
335+
end
336+
end
337+
338+
# For the type-unstable case, we have no information as to whether the
339+
# constraint is in the bridge or the model. Therefore, we try the model first,
340+
# and then the bridge if that fails. As such, we need canget for either the
341+
# bridge or the model.
342+
function MOI.canget(b::AbstractBridgeOptimizer, IdxT::Type{CI},
343+
name::String)
344+
return MOI.canget(b.bridged, IdxT, name) || MOI.canget(b.model, IdxT, name)
345+
end
346+
function MOI.get(b::AbstractBridgeOptimizer, IdxT::Type{CI},
347+
name::String)
348+
if MOI.canget(b.model, IdxT, name)
349+
return MOI.get(b.model, IdxT, name)
350+
else
351+
return MOI.get(b.bridged, IdxT, name)
352+
end
326353
end
327354

328355
# Constraints

test/bridge.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,28 @@ end
3939
mock = MOIU.MockOptimizer(SimpleModel{Float64}())
4040
bridgedmock = MOIB.SplitInterval{Float64}(mock)
4141

42+
@testset "Issue #453" begin
43+
MOI.empty!(bridgedmock)
44+
MOIU.loadfromstring!(bridgedmock, """
45+
variables: x
46+
maxobjective: 3.0x
47+
c: 2.0x in Interval(1.0, 4.0)
48+
d: x in LessThan(1.5)
49+
""")
50+
x = MOI.get(bridgedmock, MOI.VariableIndex, "x")
51+
@test isa(x, MOI.VariableIndex)
52+
c1 = MOI.get(bridgedmock, MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64}, MOI.Interval{Float64}}, "c")
53+
@test isa(c1, MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64}, MOI.Interval{Float64}})
54+
c2 = MOI.get(bridgedmock, MOI.ConstraintIndex, "c")
55+
@test c1 == c2
56+
d1 = MOI.get(bridgedmock, MOI.ConstraintIndex{MOI.SingleVariable, MOI.LessThan{Float64}}, "d")
57+
@test isa(d1, MOI.ConstraintIndex{MOI.SingleVariable, MOI.LessThan{Float64}})
58+
d2 = MOI.get(bridgedmock, MOI.ConstraintIndex, "d")
59+
@test d1 == d2
60+
end
61+
62+
MOI.empty!(bridgedmock)
63+
4264
@testset "Name test" begin
4365
MOIT.nametest(bridgedmock)
4466
end

0 commit comments

Comments
 (0)