Skip to content
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: 22 additions & 15 deletions src/Bridges/Constraint/single_bridge_optimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,37 @@ even if they are supported by one of its bridges.
"""
mutable struct SingleBridgeOptimizer{BT<:AbstractBridge, OT<:MOI.ModelLike} <: MOIB.AbstractBridgeOptimizer
model::OT
con_to_name::Dict{CI, String}
map::Map # index of bridged constraint -> constraint bridge
con_to_name::Dict{MOI.ConstraintIndex, String}
name_to_con::Union{Dict{String, MOI.ConstraintIndex}, Nothing}
# Constraint Index of bridged constraint -> Bridge.
# It is set to `nothing` when the constraint is deleted.
bridges::Vector{Union{Nothing, AbstractBridge}}
# Constraint Index of bridged constraint -> Constraint type.
constraint_types::Vector{Tuple{DataType, DataType}}
# For `SingleVariable` constraints: (variable, set type) -> bridge
single_variable_constraints::Dict{Tuple{Int64, DataType}, AbstractBridge}
end
function SingleBridgeOptimizer{BT}(model::OT) where {BT, OT <: MOI.ModelLike}
SingleBridgeOptimizer{BT, OT}(
model, Dict{CI, String}(), nothing,
Union{Nothing, AbstractBridge}[], Tuple{DataType, DataType}[],
Dict{Tuple{Int64, DataType}, AbstractBridge}())
model, Map(), Dict{MOI.ConstraintIndex, String}(), nothing)
end

function MOIB.supports_bridging_constraint(b::SingleBridgeOptimizer{BT},
F::Type{<:MOI.AbstractFunction},
S::Type{<:MOI.AbstractSet}) where BT
function bridges(bridge::MOI.Bridges.AbstractBridgeOptimizer)
return EmptyMap()
end
function bridges(bridge::SingleBridgeOptimizer)
return bridge.map
end

MOIB.supports_constraint_bridges(::SingleBridgeOptimizer) = true
function MOIB.is_bridged(::SingleBridgeOptimizer, ::Type{<:MOI.AbstractSet})
return false
end
function MOIB.supports_bridging_constraint(
::SingleBridgeOptimizer{BT}, F::Type{<:MOI.AbstractFunction},
S::Type{<:MOI.AbstractSet}) where BT
return MOI.supports_constraint(BT, F, S)
end
function MOIB.is_bridged(b::SingleBridgeOptimizer, F::Type{<:MOI.AbstractFunction},
S::Type{<:MOI.AbstractSet})
return MOIB.supports_bridging_constraint(b, F, S)
end
MOIB.bridge_type(b::SingleBridgeOptimizer{BT}, ::Type{<:MOI.AbstractFunction}, ::Type{<:MOI.AbstractSet}) where BT = BT
function MOIB.bridge_type(::SingleBridgeOptimizer{BT},
::Type{<:MOI.AbstractFunction},
::Type{<:MOI.AbstractSet}) where BT
return BT
end
2 changes: 2 additions & 0 deletions src/Bridges/Constraint/slack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ MOI.supports_constraint(::Type{ScalarSlackBridge{T}},
MOI.supports_constraint(::Type{ScalarSlackBridge{T}},
::Type{<:MOI.AbstractScalarFunction},
::Type{<:MOI.EqualTo}) where {T} = false
MOIB.added_constrained_variable_types(::Type{<:ScalarSlackBridge}) = Tuple{DataType}[]
function MOIB.added_constraint_types(::Type{ScalarSlackBridge{T, F, S}}) where {T, F, S}
return [(F, MOI.EqualTo{T}), (MOI.SingleVariable, S)]
end
Expand Down Expand Up @@ -137,6 +138,7 @@ MOI.supports_constraint(::Type{VectorSlackBridge{T}},
MOI.supports_constraint(::Type{VectorSlackBridge{T}},
::Type{<:MOI.VectorOfVariables},
::Type{<:MOI.AbstractVectorSet}) where {T} = false
MOIB.added_constrained_variable_types(::Type{<:VectorSlackBridge}) = Tuple{DataType}[]
function MOIB.added_constraint_types(::Type{VectorSlackBridge{T, F, S}}) where {T, F<:MOI.AbstractVectorFunction, S}
return [(F, MOI.Zeros), (MOI.VectorOfVariables, S)]
end
Expand Down
3 changes: 3 additions & 0 deletions src/Bridges/Variable/Variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ include("bridge.jl")
# Mapping between variable indices and bridges
include("map.jl")

# Bridge optimizer bridging a specific variable bridge
include("single_bridge_optimizer.jl")

end
50 changes: 50 additions & 0 deletions src/Bridges/Variable/single_bridge_optimizer.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""
SingleBridgeOptimizer{BT<:AbstractBridge, OT<:MOI.ModelLike} <: AbstractBridgeOptimizer

The `SingleBridgeOptimizer` bridges any constrained variables supported by the
bridge `BT`. This is in contrast with the [`MathOptInterface.Bridges.LazyBridgeOptimizer`](@ref)
which only bridges the constrained variables that are unsupported by the internal model,
even if they are supported by one of its bridges.

!!! note
Two bridge optimizers using variable bridges cannot be used together as both
of them assume that the underlying model only returns variable indices with
nonnegative values.
"""
mutable struct SingleBridgeOptimizer{BT<:AbstractBridge, OT<:MOI.ModelLike} <: MOIB.AbstractBridgeOptimizer
model::OT
map::Map # index of bridged variable -> variable bridge
var_to_name::Dict{MOI.VariableIndex, String}
name_to_var::Union{Dict{String, MOI.VariableIndex}, Nothing}
con_to_name::Dict{MOI.ConstraintIndex, String}
name_to_con::Union{Dict{String, MOI.ConstraintIndex}, Nothing}
end
function SingleBridgeOptimizer{BT}(model::OT) where {BT, OT <: MOI.ModelLike}
SingleBridgeOptimizer{BT, OT}(
model, Map(),
Dict{MOI.VariableIndex, String}(), nothing,
Dict{MOI.ConstraintIndex, String}(), nothing)
end

function bridges(bridge::MOI.Bridges.AbstractBridgeOptimizer)
return EmptyMap()
end
bridges(bridge::SingleBridgeOptimizer) = bridge.map

MOIB.supports_constraint_bridges(::SingleBridgeOptimizer) = false
function MOIB.supports_bridging_constrained_variable(
::SingleBridgeOptimizer{BT}, S::Type{<:MOI.AbstractSet}) where BT
return supports_constrained_variable(BT, S)
end
function MOIB.is_bridged(b::SingleBridgeOptimizer, S::Type{<:MOI.AbstractSet})
return MOIB.supports_bridging_constrained_variable(b, S)
end
function MOIB.is_bridged(::SingleBridgeOptimizer,
::Type{<:MOI.AbstractFunction},
::Type{<:MOI.AbstractSet})
return false
end
function MOIB.bridge_type(::SingleBridgeOptimizer{BT},
::Type{<:MOI.AbstractSet}) where BT
return BT
end
Loading