|
| 1 | +export @bridge |
| 2 | + |
| 3 | +""" |
| 4 | + AbstractBridge |
| 5 | +
|
| 6 | +A bridge represents a bridged constraint in an `AbstractBridgeOptimizer`. It contains the indices of the constraints that it has created in the model. |
| 7 | +These can be obtained using `MOI.NumberOfConstraints` and `MOI.ListOfConstraintIndices` and using the bridge in place of a `ModelLike`. |
| 8 | +Attributes of the bridged model such as `MOI.ConstraintDual` and `MOI.ConstraintPrimal`, can be obtained using the bridge in place of the constraint index. |
| 9 | +These calls are used by the `AbstractBridgeOptimizer` to communicate with the bridge so they should be implemented by the bridge. |
| 10 | +""" |
| 11 | +abstract type AbstractBridge end |
| 12 | + |
| 13 | +""" |
| 14 | + MOI.get(b::AbstractBridge, ::MOI.NumberOfVariables) |
| 15 | +
|
| 16 | +The number of variables created by the bridge `b` in the model. |
| 17 | +""" |
| 18 | +MOI.get(b::AbstractBridge, ::MOI.NumberOfVariables) = 0 |
| 19 | +""" |
| 20 | + MOI.get(b::AbstractBridge, ::MOI.NumberOfConstraints{F, S}) where {F, S} |
| 21 | +
|
| 22 | +The number of constraints of the type `F`-in-`S` created by the bridge `b` in the model. |
| 23 | +""" |
| 24 | +MOI.get(b::AbstractBridge, ::MOI.NumberOfConstraints) = 0 |
| 25 | +""" |
| 26 | + MOI.get(b::AbstractBridge, ::MOI.NumberOfConstraints{F, S}) where {F, S} |
| 27 | +
|
| 28 | +A `Vector{ConstraintIndex{F,S}}` with indices of all constraints of |
| 29 | +type `F`-in`S` created by the bride `b` in the model (i.e., of length equal to the value of `NumberOfConstraints{F,S}()`). |
| 30 | +""" |
| 31 | +MOI.get(b::AbstractBridge, ::MOI.ListOfConstraintIndices{F, S}) where {F, S} = CI{F, S}[] |
| 32 | + |
| 33 | +""" |
| 34 | + MOI.candelete(model::MOI.ModelLike, b::AbstractBridge) |
| 35 | +
|
| 36 | +Return a `Bool` indicating whether the bridge `b` can be removed from the model `model`. |
| 37 | +""" |
| 38 | +MOI.candelete(model::MOI.ModelLike, c::AbstractBridge) = true |
| 39 | + |
| 40 | +const InstanceConstraintAttribute = Union{MOI.ConstraintName, MOI.ConstraintFunction, MOI.ConstraintSet} |
| 41 | +const SolverConstraintAttribute = Union{MOI.ConstraintPrimalStart, MOI.ConstraintDualStart, MOI.ConstraintPrimal, MOI.ConstraintDual, MOI.ConstraintBasisStatus} |
| 42 | + |
| 43 | +abstract type AbstractBridgeOptimizer <: MOI.AbstractOptimizer end |
| 44 | +bridge(b::AbstractBridgeOptimizer, ci::CI) = b.bridges[ci.value] |
| 45 | +MOI.optimize!(b::AbstractBridgeOptimizer) = MOI.optimize!(b.model) |
| 46 | + |
| 47 | +MOI.isempty(b::AbstractBridgeOptimizer) = isempty(b.bridges) && MOI.isempty(b.model) |
| 48 | +function MOI.empty!(b::AbstractBridgeOptimizer) |
| 49 | + MOI.empty!(b.model) |
| 50 | + MOI.empty!(b.bridged) |
| 51 | + empty!(b.bridges) |
| 52 | +end |
| 53 | +MOI.supports(b::AbstractBridgeOptimizer, attr::Union{MOI.AbstractModelAttribute, MOI.AbstractOptimizerAttribute}) = MOI.supports(b.model, attr) |
| 54 | +MOI.copy!(b::AbstractBridgeOptimizer, src::MOI.ModelLike; copynames=false) = MOIU.defaultcopy!(b, src, copynames) |
| 55 | + |
| 56 | +# References |
| 57 | +MOI.candelete(b::AbstractBridgeOptimizer, idx::MOI.Index) = MOI.candelete(b.model, idx) |
| 58 | +MOI.isvalid(b::AbstractBridgeOptimizer, idx::MOI.Index) = MOI.isvalid(b.model, idx) |
| 59 | +MOI.delete!(b::AbstractBridgeOptimizer, idx::MOI.Index) = MOI.delete!(b.model, idx) |
| 60 | + |
| 61 | +# Attributes |
| 62 | +function MOI.get(b::AbstractBridgeOptimizer, loc::MOI.ListOfConstraintIndices) |
| 63 | + locr = MOI.get(b.model, loc) |
| 64 | + for bridge in values(b.bridges) |
| 65 | + for c in MOI.get(bridge, loc) |
| 66 | + i = findfirst(locr, c) |
| 67 | + if (VERSION >= v"0.7.0-DEV.3395" && i !== nothing) || (VERSION < v"0.7.0-DEV.3395" && !iszero(i)) |
| 68 | + MOI.deleteat!(locr, i) |
| 69 | + end |
| 70 | + end |
| 71 | + end |
| 72 | + locr |
| 73 | +end |
| 74 | +function MOI.get(b::AbstractBridgeOptimizer, attr::Union{MOI.NumberOfConstraints, MOI.NumberOfVariables}) |
| 75 | + s = MOI.get(b.model, attr) |
| 76 | + for v in values(b.bridges) |
| 77 | + s -= MOI.get(v, attr) |
| 78 | + end |
| 79 | + s |
| 80 | +end |
| 81 | +MOI.canget(b::AbstractBridgeOptimizer, attr::MOI.ListOfConstraints) = MOI.canget(b.model, attr) && MOI.canget(b.bridged, attr) |
| 82 | +_noc(b, fs) = MOI.get(b, MOI.NumberOfConstraints{fs...}()) |
| 83 | +function MOI.get(b::AbstractBridgeOptimizer, attr::MOI.ListOfConstraints) |
| 84 | + loc = MOI.get(b.model, attr) |
| 85 | + rm = find(_noc.(b, loc) .== 0) |
| 86 | + deleteat!(loc, rm) |
| 87 | + append!(loc, MOI.get(b.bridged, attr)) |
| 88 | +end |
| 89 | +for f in (:canget, :canset, :get, :get!) |
| 90 | + @eval begin |
| 91 | + MOI.$f(b::AbstractBridgeOptimizer, attr::Union{MOI.AbstractModelAttribute, MOI.AbstractOptimizerAttribute}) = MOI.$f(b.model, attr) |
| 92 | + end |
| 93 | +end |
| 94 | +# Objective function and model name |
| 95 | +MOI.set!(b::AbstractBridgeOptimizer, attr::Union{MOI.AbstractModelAttribute, MOI.AbstractOptimizerAttribute}, value) = MOI.set!(b.model, attr, value) |
| 96 | +for f in (:canget, :canset) |
| 97 | + @eval begin |
| 98 | + MOI.$f(b::AbstractBridgeOptimizer, attr::Union{MOI.AbstractVariableAttribute, MOI.AbstractConstraintAttribute}, index::Type{<:MOI.Index}) = MOI.$f(b.model, attr, index) |
| 99 | + end |
| 100 | +end |
| 101 | +MOI.get(b::AbstractBridgeOptimizer, attr::Union{MOI.AbstractVariableAttribute, MOI.AbstractConstraintAttribute}, index::MOI.Index) = MOI.get(b.model, attr, index) |
| 102 | +MOI.get(b::AbstractBridgeOptimizer, attr::Union{MOI.AbstractVariableAttribute, MOI.AbstractConstraintAttribute}, indices::Vector{<:MOI.Index}) = MOI.get(b.model, attr, indices) |
| 103 | +MOI.set!(b::AbstractBridgeOptimizer, attr::Union{MOI.AbstractVariableAttribute, MOI.AbstractConstraintAttribute}, index::MOI.Index, value) = MOI.set!(b.model, attr, index, value) |
| 104 | +MOI.set!(b::AbstractBridgeOptimizer, attr::Union{MOI.AbstractVariableAttribute, MOI.AbstractConstraintAttribute}, indices::Vector{<:MOI.Index}, values::Vector) = MOI.set!(b.model, attr, indices, values) |
| 105 | + |
| 106 | +# Name |
| 107 | +MOI.canget(b::AbstractBridgeOptimizer, IdxT::Type{<:MOI.Index}, name::String) = MOI.canget(b.model, IdxT, name) |
| 108 | +MOI.get(b::AbstractBridgeOptimizer, IdxT::Type{<:MOI.Index}, name::String) = MOI.get(b.model, IdxT, name) |
| 109 | + |
| 110 | +# Constraints |
| 111 | +MOI.supportsconstraint(b::AbstractBridgeOptimizer, ::Type{F}, ::Type{S}) where {F<:MOI.AbstractFunction, S<:MOI.AbstractSet} = MOI.supportsconstraint(b.model, F, S) |
| 112 | +MOI.canaddconstraint(b::AbstractBridgeOptimizer, ::Type{F}, ::Type{S}) where {F<:MOI.AbstractFunction, S<:MOI.AbstractSet} = MOI.canaddconstraint(b.model, F, S) |
| 113 | +function MOI.addconstraint!(b::AbstractBridgeOptimizer, f::MOI.AbstractFunction, s::MOI.AbstractSet) |
| 114 | + MOI.addconstraint!(b.model, f, s) |
| 115 | +end |
| 116 | +MOI.canmodifyconstraint(b::AbstractBridgeOptimizer, ci::CI, change) = MOI.canmodifyconstraint(b.model, ci, change) |
| 117 | +MOI.modifyconstraint!(b::AbstractBridgeOptimizer, ci::CI, change) = MOI.modifyconstraint!(b.model, ci, change) |
| 118 | + |
| 119 | +# Objective |
| 120 | +MOI.canmodifyobjective(b::AbstractBridgeOptimizer, ::Type{M}) where M<:MOI.AbstractFunctionModification = MOI.canmodifyobjective(b.model, M) |
| 121 | +MOI.modifyobjective!(b::AbstractBridgeOptimizer, change::MOI.AbstractFunctionModification) = MOI.modifyobjective!(b.model, change) |
| 122 | + |
| 123 | +# Variables |
| 124 | +MOI.canaddvariable(b::AbstractBridgeOptimizer) = MOI.canaddvariable(b.model) |
| 125 | +MOI.addvariable!(b::AbstractBridgeOptimizer) = MOI.addvariable!(b.model) |
| 126 | +MOI.addvariables!(b::AbstractBridgeOptimizer, n) = MOI.addvariables!(b.model, n) |
| 127 | + |
| 128 | +function _mois(t) |
| 129 | + MOIU._moi.(t.args) |
| 130 | +end |
| 131 | + |
| 132 | +""" |
| 133 | +macro bridge(modelname, bridge, scalarsets, typedscalarsets, vectorsets, typedvectorsets, scalarfunctions, typedscalarfunctions, vectorfunctions, typedvectorfunctions) |
| 134 | +
|
| 135 | +Creates a type `modelname` implementing the MOI model interface and bridging the `scalarsets` scalar sets `typedscalarsets` typed scalar sets, `vectorsets` vector sets, `typedvectorsets` typed vector sets, `scalarfunctions` scalar functions, `typedscalarfunctions` typed scalar functions, `vectorfunctions` vector functions and `typedvectorfunctions` typed vector functions. |
| 136 | +To give no set/function, write `()`, to give one set `S`, write `(S,)`. |
| 137 | +
|
| 138 | +### Examples |
| 139 | +
|
| 140 | +The optimizer layer bridging the constraints `ScalarAffineFunction`-in-`Interval` is created as follows: |
| 141 | +```julia |
| 142 | +@bridge SplitInterval MOIB.SplitIntervalBridge () (Interval,) () () () (ScalarAffineFunction,) () () |
| 143 | +``` |
| 144 | +Given an optimizer `optimizer` implementing `ScalarAffineFunction`-in-`GreaterThan` and `ScalarAffineFunction`-in-`LessThan`, the optimizer |
| 145 | +``` |
| 146 | +bridgedmodel = SplitInterval(model) |
| 147 | +``` |
| 148 | +will additionally support `ScalarAffineFunction`-in-`Interval`. |
| 149 | +""" |
| 150 | +macro bridge(modelname, bridge, ss, sst, vs, vst, sf, sft, vf, vft) |
| 151 | + bridgedmodelname = Symbol(string(modelname) * "Instance") |
| 152 | + bridgedfuns = :(Union{$(_mois(sf)...), $(_mois(sft)...), $(_mois(vf)...), $(_mois(vft)...)}) |
| 153 | + bridgedsets = :(Union{$(_mois(ss)...), $(_mois(sst)...), $(_mois(vs)...), $(_mois(vst)...)}) |
| 154 | + |
| 155 | + # Attributes |
| 156 | + attributescode = :() |
| 157 | + |
| 158 | + for f in (:canget, :get) |
| 159 | + attributescode = quote |
| 160 | + $attributescode |
| 161 | + |
| 162 | + function $MOI.$f(b::$modelname, attr::Union{$MOI.ListOfConstraintIndices{<:$bridgedfuns, <:$bridgedsets}, $MOI.NumberOfConstraints{<:$bridgedfuns, <:$bridgedsets}}) |
| 163 | + $MOI.$f(b.bridged, attr) |
| 164 | + end |
| 165 | + end |
| 166 | + end |
| 167 | + |
| 168 | + for f in (:canget, :canset) |
| 169 | + attributescode = quote |
| 170 | + $attributescode |
| 171 | + |
| 172 | + function $MOI.$f(b::$modelname, attr::$MOIB.InstanceConstraintAttribute, ci::Type{$CI{F, S}}) where {F<:$bridgedfuns, S<:$bridgedsets} |
| 173 | + $MOI.$f(b.bridged, attr, ci) |
| 174 | + end |
| 175 | + function $MOI.$f(b::$modelname{T}, attr::$MOIB.SolverConstraintAttribute, ci::Type{$CI{F, S}}) where {T, F<:$bridgedfuns, S<:$bridgedsets} |
| 176 | + $MOI.$f(b.model, attr, $bridge{T}) |
| 177 | + end |
| 178 | + end |
| 179 | + end |
| 180 | + |
| 181 | + for f in (:set!, :get, :get!) |
| 182 | + attributescode = quote |
| 183 | + $attributescode |
| 184 | + |
| 185 | + function $MOI.$f(b::$modelname, attr::$MOIB.InstanceConstraintAttribute, ci::$CI{<:$bridgedfuns, <:$bridgedsets}) |
| 186 | + $MOI.$f(b.bridged, attr, ci) |
| 187 | + end |
| 188 | + function $MOI.$f(b::$modelname, attr::$MOIB.SolverConstraintAttribute, ci::$CI{<:$bridgedfuns, <:$bridgedsets}) |
| 189 | + $MOI.$f(b.model, attr, $MOIB.bridge(b, ci)) |
| 190 | + end |
| 191 | + end |
| 192 | + end |
| 193 | + |
| 194 | + esc(quote |
| 195 | + $MOIU.@model $bridgedmodelname $ss $sst $vs $vst $sf $sft $vf $vft |
| 196 | + |
| 197 | + struct $modelname{T, IT<:$MOI.ModelLike} <: $MOIB.AbstractBridgeOptimizer |
| 198 | + model::IT |
| 199 | + bridged::$bridgedmodelname{T} |
| 200 | + bridges::Dict{Int64, $bridge{T}} |
| 201 | + function $modelname{T}(model::IT) where {T, IT <: $MOI.ModelLike} |
| 202 | + new{T, IT}(model, $bridgedmodelname{T}(), Dict{Int64, $bridge{T}}()) |
| 203 | + end |
| 204 | + end |
| 205 | + |
| 206 | + # References |
| 207 | + $MOI.candelete(b::$modelname{T}, ci::$CI{<:$bridgedfuns, <:$bridgedsets}) where T = $MOI.candelete(b.bridged, ci) && $MOI.candelete(b.model, $MOIB.bridge(b, ci)) |
| 208 | + |
| 209 | + $MOI.isvalid(b::$modelname{T}, ci::$CI{<:$bridgedfuns, <:$bridgedsets}) where T = $MOI.isvalid(b.bridged, ci) |
| 210 | + |
| 211 | + function $MOI.delete!(b::$modelname{T}, ci::$CI{<:$bridgedfuns, <:$bridgedsets}) where T |
| 212 | + $MOI.delete!(b.model, $MOIB.bridge(b, ci)) |
| 213 | + delete!(b.bridges, ci.value) |
| 214 | + $MOI.delete!(b.bridged, ci) |
| 215 | + end |
| 216 | + |
| 217 | + $attributescode |
| 218 | + |
| 219 | + # Constraints |
| 220 | + $MOI.supportsconstraint(b::$modelname, ::Type{F}, ::Type{S}) where {F<:$bridgedfuns, S<:$bridgedsets} = $MOI.supportsconstraint(b.bridged, F, S) |
| 221 | + $MOI.canaddconstraint(b::$modelname, ::Type{F}, ::Type{S}) where {F<:$bridgedfuns, S<:$bridgedsets} = $MOI.canaddconstraint(b.bridged, F, S) |
| 222 | + function $MOI.addconstraint!(b::$modelname{T}, f::$bridgedfuns, s::$bridgedsets) where T |
| 223 | + ci = $MOI.addconstraint!(b.bridged, f, s) |
| 224 | + @assert !haskey(b.bridges, ci.value) |
| 225 | + b.bridges[ci.value] = $bridge{T}(b.model, f, s) |
| 226 | + ci |
| 227 | + end |
| 228 | + function $MOI.canmodifyconstraint(b::$modelname, ci::$CI{<:$bridgedfuns, <:$bridgedsets}, change) |
| 229 | + $MOI.canmodifyconstraint(b.bridged, ci, change) && $MOI.canmodifyconstraint(b.model, $MOIB.bridge(b, ci), change) |
| 230 | + end |
| 231 | + function $MOI.modifyconstraint!(b::$modelname, ci::$CI{<:$bridgedfuns, <:$bridgedsets}, change) |
| 232 | + $MOI.modifyconstraint!(b.model, $MOIB.bridge(b, ci), change) |
| 233 | + $MOI.modifyconstraint!(b.bridged, ci, change) |
| 234 | + end |
| 235 | + end) |
| 236 | +end |
0 commit comments