Skip to content

Commit

Permalink
Merge pull request #454 from JuliaOpt/od/fix-bridge
Browse files Browse the repository at this point in the history
Fix bug in bridges querying index via string
  • Loading branch information
blegat authored Aug 6, 2018
2 parents c164eab + 1fe897c commit 253e16c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/Bridges/bridgeoptimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ function isbridged end
function isbridged(b::AbstractBridgeOptimizer, ::Type{CI{F, S}}) where {F, S}
return isbridged(b, F, S)
end
# We don't bridge variables.
isbridged(b::AbstractBridgeOptimizer, ::Type{VI}) = false

"""
supportsbridgingconstraint(b::AbstractBridgeOptimizer,
Expand Down Expand Up @@ -318,11 +320,36 @@ end
# Name
function MOI.canget(b::AbstractBridgeOptimizer, IdxT::Type{<:MOI.Index},
name::String)
return MOI.canget(b.model, IdxT, name)
if isbridged(b, IdxT)
return MOI.canget(b.bridged, IdxT, name)
else
return MOI.canget(b.model, IdxT, name)
end
end
function MOI.get(b::AbstractBridgeOptimizer, IdxT::Type{<:MOI.Index},
name::String)
return MOI.get(b.model, IdxT, name)
if isbridged(b, IdxT)
return MOI.get(b.bridged, IdxT, name)
else
return MOI.get(b.model, IdxT, name)
end
end

# For the type-unstable case, we have no information as to whether the
# constraint is in the bridge or the model. Therefore, we try the model first,
# and then the bridge if that fails. As such, we need canget for either the
# bridge or the model.
function MOI.canget(b::AbstractBridgeOptimizer, IdxT::Type{CI},
name::String)
return MOI.canget(b.bridged, IdxT, name) || MOI.canget(b.model, IdxT, name)
end
function MOI.get(b::AbstractBridgeOptimizer, IdxT::Type{CI},
name::String)
if MOI.canget(b.model, IdxT, name)
return MOI.get(b.model, IdxT, name)
else
return MOI.get(b.bridged, IdxT, name)
end
end

# Constraints
Expand Down
22 changes: 22 additions & 0 deletions test/bridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,28 @@ end
mock = MOIU.MockOptimizer(SimpleModel{Float64}())
bridgedmock = MOIB.SplitInterval{Float64}(mock)

@testset "Issue #453" begin
MOI.empty!(bridgedmock)
MOIU.loadfromstring!(bridgedmock, """
variables: x
maxobjective: 3.0x
c: 2.0x in Interval(1.0, 4.0)
d: x in LessThan(1.5)
""")
x = MOI.get(bridgedmock, MOI.VariableIndex, "x")
@test isa(x, MOI.VariableIndex)
c1 = MOI.get(bridgedmock, MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64}, MOI.Interval{Float64}}, "c")
@test isa(c1, MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64}, MOI.Interval{Float64}})
c2 = MOI.get(bridgedmock, MOI.ConstraintIndex, "c")
@test c1 == c2
d1 = MOI.get(bridgedmock, MOI.ConstraintIndex{MOI.SingleVariable, MOI.LessThan{Float64}}, "d")
@test isa(d1, MOI.ConstraintIndex{MOI.SingleVariable, MOI.LessThan{Float64}})
d2 = MOI.get(bridgedmock, MOI.ConstraintIndex, "d")
@test d1 == d2
end

MOI.empty!(bridgedmock)

@testset "Name test" begin
MOIT.nametest(bridgedmock)
end
Expand Down

0 comments on commit 253e16c

Please sign in to comment.