|
| 1 | +# Copyright (c) 2017: Miles Lubin and contributors |
| 2 | +# Copyright (c) 2017: Google Inc. |
| 3 | +# |
| 4 | +# Use of this source code is governed by an MIT-style license that can be found |
| 5 | +# in the LICENSE.md file or at https://opensource.org/licenses/MIT. |
| 6 | + |
| 7 | +""" |
| 8 | + InequalityToComplementsBridge{T,F,S,G} <: Bridges.Constraint.AbstractBridge |
| 9 | +
|
| 10 | +`InequalityToComplementsBridge` implements the following reformulations: |
| 11 | +
|
| 12 | + * ``f(x) \\ge b`` into ``\\exists y`` such that ``f(x) - b \\perp y \\ge 0`` |
| 13 | + * ``f(x) \\le b`` into ``f(x) - b \\perp y \\le 0`` |
| 14 | + * ``f(x) = b`` into ``f(x) - b \\perp y`` |
| 15 | +
|
| 16 | +## Source node |
| 17 | +
|
| 18 | +`InequalityToComplementsBridge` supports: |
| 19 | +
|
| 20 | + * `F` in [`MOI.GreaterThan{T}`](@ref) |
| 21 | + * `F` in [`MOI.LessThan{T}`](@ref) |
| 22 | + * `F` in [`MOI.EqualTo`](@ref) |
| 23 | +
|
| 24 | +## Target nodes |
| 25 | +
|
| 26 | +`InequalityToComplementsBridge` creates: |
| 27 | +
|
| 28 | + * [`MOI.VariableIndex`](@ref) in [`MOI.LessThan{T}`](@ref) |
| 29 | + * [`MOI.VariableIndex`](@ref) in [`MOI.GreaterThan{T}`](@ref) |
| 30 | + * `G` in [`MOI.Complements`](@ref) |
| 31 | +""" |
| 32 | +mutable struct InequalityToComplementsBridge{ |
| 33 | + T, |
| 34 | + F<:MOI.AbstractScalarFunction, |
| 35 | + S<:Union{MOI.GreaterThan{T},MOI.LessThan{T},MOI.EqualTo{T}}, |
| 36 | + G<:MOI.AbstractVectorFunction, |
| 37 | +} <: AbstractBridge |
| 38 | + y::MOI.VariableIndex |
| 39 | + set::S |
| 40 | + ci::MOI.ConstraintIndex{G,MOI.Complements} |
| 41 | +end |
| 42 | + |
| 43 | +const InequalityToComplements{T,OT<:MOI.ModelLike} = |
| 44 | + SingleBridgeOptimizer{InequalityToComplementsBridge{T},OT} |
| 45 | + |
| 46 | +function _add_y_variable(model, ::MOI.GreaterThan{T}) where {T} |
| 47 | + return MOI.add_constrained_variable(model, MOI.GreaterThan(zero(T)))[1] |
| 48 | +end |
| 49 | + |
| 50 | +function _add_y_variable(model, ::MOI.LessThan{T}) where {T} |
| 51 | + return MOI.add_constrained_variable(model, MOI.LessThan(zero(T)))[1] |
| 52 | +end |
| 53 | + |
| 54 | +_add_y_variable(model, ::MOI.EqualTo) = MOI.add_variable(model) |
| 55 | + |
| 56 | +function bridge_constraint( |
| 57 | + ::Type{InequalityToComplementsBridge{T,F,S,G}}, |
| 58 | + model::MOI.ModelLike, |
| 59 | + f::F, |
| 60 | + set::S, |
| 61 | +) where {T,F,S<:Union{MOI.GreaterThan{T},MOI.LessThan{T},MOI.EqualTo{T}},G} |
| 62 | + y = _add_y_variable(model, set) |
| 63 | + f_set = MOI.Utilities.operate(-, T, f, MOI.constant(set)) |
| 64 | + g = MOI.Utilities.operate(vcat, T, f_set, y) |
| 65 | + ci = MOI.add_constraint(model, g, MOI.Complements(2)) |
| 66 | + return InequalityToComplementsBridge{T,F,S,G}(y, set, ci) |
| 67 | +end |
| 68 | + |
| 69 | +function MOI.supports_constraint( |
| 70 | + ::Type{<:InequalityToComplementsBridge{T}}, |
| 71 | + ::Type{F}, |
| 72 | + ::Type{<:Union{MOI.GreaterThan{T},MOI.LessThan{T},MOI.EqualTo{T}}}, |
| 73 | +) where {T,F<:MOI.AbstractScalarFunction} |
| 74 | + return !MOI.Utilities.is_complex(F) |
| 75 | +end |
| 76 | + |
| 77 | +function MOI.Bridges.added_constrained_variable_types( |
| 78 | + ::Type{InequalityToComplementsBridge{T,F,S,G}}, |
| 79 | +) where {T,F,S<:Union{MOI.GreaterThan{T},MOI.LessThan{T}},G} |
| 80 | + return Tuple{Type}[(S,)] |
| 81 | +end |
| 82 | + |
| 83 | +function MOI.Bridges.added_constrained_variable_types( |
| 84 | + ::Type{InequalityToComplementsBridge{T,F,MOI.EqualTo{T},G}}, |
| 85 | +) where {T,F,G} |
| 86 | + return Tuple{Type}[(MOI.Reals,)] |
| 87 | +end |
| 88 | + |
| 89 | +function MOI.Bridges.added_constraint_types( |
| 90 | + ::Type{InequalityToComplementsBridge{T,F,S,G}}, |
| 91 | +) where {T,F,S,G} |
| 92 | + return Tuple{Type,Type}[(G, MOI.Complements)] |
| 93 | +end |
| 94 | + |
| 95 | +function concrete_bridge_type( |
| 96 | + ::Type{<:InequalityToComplementsBridge}, |
| 97 | + F::Type{<:MOI.AbstractScalarFunction}, |
| 98 | + S::Type{<:Union{MOI.GreaterThan{T},MOI.LessThan{T},MOI.EqualTo{T}}}, |
| 99 | +) where {T} |
| 100 | + G = MOI.Utilities.promote_operation(vcat, T, F, MOI.VariableIndex) |
| 101 | + return InequalityToComplementsBridge{T,F,S,G} |
| 102 | +end |
| 103 | + |
| 104 | +function MOI.get(::InequalityToComplementsBridge, ::MOI.NumberOfVariables) |
| 105 | + return Int64(1) |
| 106 | +end |
| 107 | + |
| 108 | +function MOI.get( |
| 109 | + bridge::InequalityToComplementsBridge, |
| 110 | + ::MOI.ListOfVariableIndices, |
| 111 | +) |
| 112 | + return [bridge.y] |
| 113 | +end |
| 114 | + |
| 115 | +function MOI.get( |
| 116 | + ::InequalityToComplementsBridge{T,F,S,G}, |
| 117 | + ::MOI.NumberOfConstraints{G,MOI.Complements}, |
| 118 | +)::Int64 where {T,F,S,G} |
| 119 | + return 1 |
| 120 | +end |
| 121 | + |
| 122 | +function MOI.get( |
| 123 | + bridge::InequalityToComplementsBridge{T,F,S,G}, |
| 124 | + ::MOI.ListOfConstraintIndices{G,MOI.Complements}, |
| 125 | +) where {T,F,S,G} |
| 126 | + return [bridge.ci] |
| 127 | +end |
| 128 | + |
| 129 | +function MOI.delete(model::MOI.ModelLike, bridge::InequalityToComplementsBridge) |
| 130 | + MOI.delete(model, bridge.y) |
| 131 | + MOI.delete(model, bridge.ci) |
| 132 | + return |
| 133 | +end |
| 134 | + |
| 135 | +function MOI.get( |
| 136 | + model::MOI.ModelLike, |
| 137 | + attr::MOI.ConstraintFunction, |
| 138 | + bridge::InequalityToComplementsBridge{T}, |
| 139 | +) where {T} |
| 140 | + g = MOI.get(model, attr, bridge.ci) |
| 141 | + f_set = first(MOI.Utilities.scalarize(g)) |
| 142 | + return MOI.Utilities.operate(+, T, f_set, MOI.constant(bridge.set)) |
| 143 | +end |
| 144 | + |
| 145 | +function MOI.get( |
| 146 | + ::MOI.ModelLike, |
| 147 | + ::MOI.ConstraintSet, |
| 148 | + bridge::InequalityToComplementsBridge, |
| 149 | +) |
| 150 | + return bridge.set |
| 151 | +end |
0 commit comments