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
43 changes: 39 additions & 4 deletions src/Bridges/Constraint/set_map.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ function MOI.get(
return MOIU.convert_approx(G, func)
end

function MOI.set(
model::MOI.ModelLike,
attr::MOI.ConstraintFunction,
bridge::SetMapBridge{T,S2,S1,F,G},
func::G,
) where {T,S2,S1,F,G}
MOI.set(
model,
attr,
bridge.constraint,
MOIB.map_function(typeof(bridge), func),
)
return
end

function MOI.get(
model::MOI.ModelLike,
attr::MOI.ConstraintSet,
Expand Down Expand Up @@ -164,14 +179,34 @@ function MOI.set(
return
end

# By linearity of the map, we can just change the constant/coefficient
function _map_change(::Type{BT}, change::MOI.ScalarConstantChange) where {BT}
constant = MOIB.map_function(BT, change.new_constant)
return MOI.ScalarConstantChange(constant)
end
function _map_change(::Type{BT}, change::MOI.VectorConstantChange) where {BT}
constant = MOIB.map_function(BT, change.new_constant)
return MOI.VectorConstantChange(constant)
end
function _map_change(::Type{BT}, change::MOI.ScalarCoefficientChange) where {BT}
coefficient = MOIB.map_function(BT, change.new_coefficient)
return MOI.ScalarCoefficientChange(change.variable, coefficient)
end
function _map_change(::Type{BT}, change::MOI.MultirowChange) where {BT}
# It is important here that `change.new_coefficients` contains
# the complete new sparse column associated to the variable.
# Calling modify twice with part of the column won't work since
# the linear map might reset all the column each time.
coefficients = MOIB.map_function(BT, change.new_coefficients)
return MOI.MultirowChange(change.variable, coefficients)
end

function MOI.modify(
model::MOI.ModelLike,
bridge::SetMapBridge,
change::MOI.VectorConstantChange,
change::MOI.AbstractFunctionModification,
)
# By linearity of the map, we can just change the constant
constant = MOIB.map_function(typeof(bridge), change.new_constant)
MOI.modify(model, bridge.constraint, MOI.VectorConstantChange(constant))
MOI.modify(model, bridge.constraint, _map_change(typeof(bridge), change))
return
end

Expand Down
2 changes: 2 additions & 0 deletions src/Bridges/Constraint/single_bridge_optimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,5 @@ function MOIB.bridge_type(
end

MOIB.bridging_cost(::SingleBridgeOptimizer, args...) = 1.0

MOIB.recursive_model(b::SingleBridgeOptimizer) = b.model
2 changes: 2 additions & 0 deletions src/Bridges/Objective/single_bridge_optimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,5 @@ function MOIB.bridge_type(
) where {BT}
return BT
end

MOIB.recursive_model(b::SingleBridgeOptimizer) = b.model
40 changes: 38 additions & 2 deletions src/Bridges/Variable/set_map.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,21 @@ bridge.
"""
abstract type SetMapBridge{T,S1,S2} <: AbstractBridge end

function _add_constrained_var(model, set::MOI.AbstractScalarSet)
return MOI.add_constrained_variable(model, set)
end

function _add_constrained_var(model, set::MOI.AbstractVectorSet)
return MOI.add_constrained_variables(model, set)
end

function bridge_constrained_variable(
BT::Type{<:SetMapBridge{T,S1,S2}},
model::MOI.ModelLike,
set::S2,
) where {T,S1,S2}
variables, constraint =
MOI.add_constrained_variables(model, MOIB.inverse_map_set(BT, set))
_add_constrained_var(model, MOIB.inverse_map_set(BT, set))
return BT(variables, constraint)
end

Expand Down Expand Up @@ -88,7 +96,17 @@ function MOI.get(
end

# References
function MOI.delete(model::MOI.ModelLike, bridge::SetMapBridge)
function MOI.delete(
model::MOI.ModelLike,
bridge::SetMapBridge{T,S1,S2},
) where {T,S1,S2<:MOI.AbstractScalarSet}
MOI.delete(model, bridge.variable)
return
end
function MOI.delete(
model::MOI.ModelLike,
bridge::SetMapBridge{T,S1,S2},
) where {T,S1,S2<:MOI.AbstractVectorSet}
MOI.delete(model, bridge.variables)
return
end
Expand All @@ -104,6 +122,17 @@ function MOI.get(
return MOIB.map_set(typeof(bridge), set)
end

function MOI.set(
model::MOI.ModelLike,
attr::MOI.ConstraintSet,
bridge::SetMapBridge{T,S1},
set::S1,
) where {T,S1}
mapped = MOIB.inverse_map_set(typeof(bridge), set)
MOI.set(model, attr, bridge.constraint, mapped)
return
end

function MOI.get(
model::MOI.ModelLike,
attr::MOI.ConstraintPrimal,
Expand Down Expand Up @@ -164,6 +193,13 @@ function MOIB.bridged_function(
return convert(MOI.ScalarAffineFunction{T}, func)
end

function unbridged_map(bridge::SetMapBridge{T}, vi::MOI.VariableIndex) where {T}
F = MOI.ScalarAffineFunction{T}
func = MOI.SingleVariable(vi)
mapped = MOIB.inverse_map_function(typeof(bridge), func)
return Pair{MOI.VariableIndex,F}[bridge.variable=>mapped]
end

function unbridged_map(
bridge::SetMapBridge{T},
vis::Vector{MOI.VariableIndex},
Expand Down
2 changes: 2 additions & 0 deletions src/Bridges/Variable/single_bridge_optimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,5 @@ function MOIB.bridge_type(
end

MOIB.bridging_cost(::SingleBridgeOptimizer, args...) = 1.0

MOIB.recursive_model(b::SingleBridgeOptimizer) = b.model
Loading