|
| 1 | +""" |
| 2 | + FlipSignBridge{S1, S2, F} |
| 3 | +
|
| 4 | +Bridge a `G`-in-`S1` constraint into an `F`-in-`S2` constraint by multiplying |
| 5 | +the function by `-1` and taking the point reflection of the set across the |
| 6 | +origin. The flipped `F`-in-`S` constraint is stored in the `flipped_constraint` |
| 7 | +field by convention. |
| 8 | +""" |
| 9 | +abstract type FlipSignBridge{ |
| 10 | + S1<:MOI.AbstractSet, S2<:MOI.AbstractSet, |
| 11 | + F<:MOI.AbstractFunction} <: AbstractBridge end |
| 12 | + |
| 13 | +function MOI.supports_constraint(::Type{<:FlipSignBridge{S1}}, |
| 14 | + ::Type{<:MOI.AbstractScalarFunction}, |
| 15 | + ::Type{S1}) where {S1<:MOI.AbstractSet} |
| 16 | + return true |
| 17 | +end |
| 18 | +function added_constraint_types( |
| 19 | + ::Type{<:FlipSignBridge{S1, S2, F}}) where {S1, S2, F} |
| 20 | + return [(F, S2)] |
| 21 | +end |
| 22 | + |
| 23 | +# Attributes, Bridge acting as an model |
| 24 | +function MOI.get(::FlipSignBridge{S1, S2, F}, |
| 25 | + ::MOI.NumberOfConstraints{F, S2}) where {S1, S2, F} |
| 26 | + return 1 |
| 27 | +end |
| 28 | +function MOI.get(bridge::FlipSignBridge{S1, S2, F}, |
| 29 | + ::MOI.ListOfConstraintIndices{F, S2}) where {S1, S2, F} |
| 30 | + return [bridge.flipped_constraint] |
| 31 | +end |
| 32 | + |
| 33 | +# References |
| 34 | +function MOI.delete(model::MOI.ModelLike, bridge::FlipSignBridge) |
| 35 | + MOI.delete(model, bridge.flipped_constraint) |
| 36 | +end |
| 37 | + |
| 38 | +# Attributes, Bridge acting as a constraint |
| 39 | +function MOI.get(model::MOI.ModelLike, |
| 40 | + attr::Union{MOI.ConstraintPrimal, MOI.ConstraintDual}, |
| 41 | + bridge::FlipSignBridge) |
| 42 | + return -MOI.get(model, attr, bridge.flipped_constraint) |
| 43 | +end |
| 44 | + |
| 45 | +function MOI.modify(model::MOI.ModelLike, bridge::FlipSignBridge, |
| 46 | + change::MOI.ScalarCoefficientChange) |
| 47 | + MOI.modify( |
| 48 | + model, bridge.flipped_constraint, |
| 49 | + MOI.ScalarCoefficientChange(change.variable, -change.new_coefficient)) |
| 50 | +end |
| 51 | + |
| 52 | +function MOI.modify(model::MOI.ModelLike, bridge::FlipSignBridge, |
| 53 | + change::MOI.MultirowChange{T}) where T |
| 54 | + new_coefficients = Tuple{Int64, T}[ |
| 55 | + (index, -coef) for (index, coef) in change.new_coefficients] |
| 56 | + MOI.modify(model, bridge.flipped_constraint, |
| 57 | + MOI.MultirowChange(change.variable, |
| 58 | + new_coefficients)) |
| 59 | +end |
| 60 | +function MOI.modify(model::MOI.ModelLike, bridge::FlipSignBridge, |
| 61 | + change::MOI.VectorConstantChange) |
| 62 | + MOI.modify(model, bridge.flipped_constraint, |
| 63 | + MOI.VectorConstantChange(-change.new_constant)) |
| 64 | +end |
| 65 | + |
| 66 | +""" |
| 67 | + GreaterToLessBridge{T, F<:MOI.AbstractScalarFunction} <: |
| 68 | + FlipSignBridge{MOI.GreaterThan{T}, MOI.LessThan{T}, F} |
| 69 | +
|
| 70 | +Transforms a `G`-in-`GreaterThan{T}` constraint into an `F`-in-`LessThan{T}` |
| 71 | +constraint. |
| 72 | +""" |
| 73 | +struct GreaterToLessBridge{T, F<:MOI.AbstractScalarFunction} <: |
| 74 | + FlipSignBridge{MOI.GreaterThan{T}, MOI.LessThan{T}, F} |
| 75 | + flipped_constraint::CI{F, MOI.LessThan{T}} |
| 76 | +end |
| 77 | +function GreaterToLessBridge{T, F}(model::MOI.ModelLike, |
| 78 | + g::MOI.AbstractScalarFunction, |
| 79 | + s::MOI.GreaterThan) where {T, F} |
| 80 | + f = MOIU.operate(-, T, g) |
| 81 | + flipped_constraint = MOI.add_constraint(model, f, MOI.LessThan(-s.lower)) |
| 82 | + return GreaterToLessBridge{T, F}(flipped_constraint) |
| 83 | +end |
| 84 | +function concrete_bridge_type(::Type{<:GreaterToLessBridge{T}}, |
| 85 | + G::Type{<:MOI.AbstractScalarFunction}, |
| 86 | + ::Type{MOI.GreaterThan{T}}) where T |
| 87 | + F = MOIU.promote_operation(-, T, G) |
| 88 | + return GreaterToLessBridge{T, F} |
| 89 | +end |
| 90 | +function MOI.set(model::MOI.ModelLike, attr::MOI.ConstraintSet, |
| 91 | + bridge::GreaterToLessBridge, new_set::MOI.GreaterThan) |
| 92 | + MOI.set(model, attr, bridge.flipped_constraint, |
| 93 | + MOI.LessThan(-new_set.lower)) |
| 94 | +end |
| 95 | + |
| 96 | +""" |
| 97 | + LessToGreaterBridge{T, F<:MOI.AbstractScalarFunction} <: |
| 98 | + FlipSignBridge{MOI.LessThan{T}, MOI.GreaterThan{T}, F} |
| 99 | +
|
| 100 | +Transforms a `G`-in-`LessThan{T}` constraint into an `F`-in-`GreaterThan{T}` |
| 101 | +constraint. |
| 102 | +""" |
| 103 | +struct LessToGreaterBridge{T, F<:MOI.AbstractScalarFunction} <: |
| 104 | + FlipSignBridge{MOI.LessThan{T}, MOI.GreaterThan{T}, F} |
| 105 | + flipped_constraint::CI{F, MOI.GreaterThan{T}} |
| 106 | +end |
| 107 | +function LessToGreaterBridge{T, F}(model::MOI.ModelLike, |
| 108 | + g::MOI.AbstractScalarFunction, |
| 109 | + s::MOI.LessThan) where {T, F} |
| 110 | + f = MOIU.operate(-, T, g) |
| 111 | + flipped_constraint = MOI.add_constraint(model, f, MOI.GreaterThan(-s.upper)) |
| 112 | + return LessToGreaterBridge{T, F}(flipped_constraint) |
| 113 | +end |
| 114 | +function concrete_bridge_type(::Type{<:LessToGreaterBridge{T}}, |
| 115 | + G::Type{<:MOI.AbstractScalarFunction}, |
| 116 | + ::Type{MOI.LessThan{T}}) where T |
| 117 | + F = MOIU.promote_operation(-, T, G) |
| 118 | + return LessToGreaterBridge{T, F} |
| 119 | +end |
| 120 | +function MOI.set(model::MOI.ModelLike, attr::MOI.ConstraintSet, |
| 121 | + bridge::LessToGreaterBridge, new_set::MOI.LessThan) |
| 122 | + MOI.set(model, attr, bridge.flipped_constraint, |
| 123 | + MOI.GreaterThan(-new_set.upper)) |
| 124 | +end |
| 125 | + |
| 126 | +""" |
| 127 | + NonnegToNonposBridge{T, F<:MOI.AbstractVectorFunction} |
| 128 | +
|
| 129 | +Transforms a `G`-in-`Nonnegatives` constraint into a `F`-in-`Nonpositives` |
| 130 | +constraint. |
| 131 | +""" |
| 132 | +struct NonnegToNonposBridge{T, F<:MOI.AbstractVectorFunction} <: |
| 133 | + FlipSignBridge{MOI.Nonnegatives, MOI.Nonpositives, F} |
| 134 | + flipped_constraint::CI{F, MOI.Nonpositives} |
| 135 | +end |
| 136 | +function NonnegToNonposBridge{T, F}(model::MOI.ModelLike, |
| 137 | + g::MOI.AbstractVectorFunction, |
| 138 | + s::MOI.Nonnegatives) where {T, F} |
| 139 | + f = MOIU.operate(-, T, g) |
| 140 | + flipped_constraint = MOI.add_constraint(model, f, |
| 141 | + MOI.Nonpositives(s.dimension)) |
| 142 | + return NonnegToNonposBridge{T, F}(flipped_constraint) |
| 143 | +end |
| 144 | +function concrete_bridge_type(::Type{<:NonnegToNonposBridge{T}}, |
| 145 | + G::Type{<:MOI.AbstractVectorFunction}, |
| 146 | + ::Type{MOI.Nonnegatives}) where T |
| 147 | + F = MOIU.promote_operation(-, T, G) |
| 148 | + return NonnegToNonposBridge{T, F} |
| 149 | +end |
| 150 | + |
| 151 | +""" |
| 152 | + NonposToNonnegBridge{T, F<:MOI.AbstractVectorFunction} |
| 153 | +
|
| 154 | +Transforms a `G`-in-`Nonpositives` constraint into a `F`-in-`Nonnegatives` |
| 155 | +constraint. |
| 156 | +""" |
| 157 | +struct NonposToNonnegBridge{T, F<:MOI.AbstractVectorFunction} <: |
| 158 | + FlipSignBridge{MOI.Nonpositives, MOI.Nonnegatives, F} |
| 159 | + flipped_constraint::CI{F, MOI.Nonnegatives} |
| 160 | +end |
| 161 | +function NonposToNonnegBridge{T, F}(model::MOI.ModelLike, |
| 162 | + g::MOI.AbstractVectorFunction, |
| 163 | + s::MOI.Nonpositives) where {T, F} |
| 164 | + f = MOIU.operate(-, T, g) |
| 165 | + flipped_constraint = MOI.add_constraint(model, f, |
| 166 | + MOI.Nonnegatives(s.dimension)) |
| 167 | + return NonposToNonnegBridge{T, F}(flipped_constraint) |
| 168 | +end |
| 169 | +function concrete_bridge_type(::Type{<:NonposToNonnegBridge{T}}, |
| 170 | + G::Type{<:MOI.AbstractVectorFunction}, |
| 171 | + ::Type{MOI.Nonpositives}) where T |
| 172 | + F = MOIU.promote_operation(-, T, G) |
| 173 | + return NonposToNonnegBridge{T, F} |
| 174 | +end |
0 commit comments