|
| 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 | + SplitComplexIndicatorEqualToBridge{T,F,G,A} <: |
| 9 | + Bridges.Constraint.AbstractBridge |
| 10 | +
|
| 11 | +`SplitComplexIndicatorEqualToBridge` implements the following reformulation: |
| 12 | +
|
| 13 | + * ``z \\implies f(x) + g(x) * im = a + b * im`` into ``z \\implies f(x) = a`` |
| 14 | + and ``z \\implies g(x) = b`` |
| 15 | +
|
| 16 | +## Source node |
| 17 | +
|
| 18 | +`SplitComplexIndicatorEqualToBridge` supports: |
| 19 | +
|
| 20 | + * `G` in `MOI.Indicator{A,MOI.EqualTo{Complex{T}}` |
| 21 | +
|
| 22 | +where `G` is a function with `Complex` coefficients. |
| 23 | +
|
| 24 | +## Target nodes |
| 25 | +
|
| 26 | +`SplitComplexIndicatorEqualToBridge` creates: |
| 27 | +
|
| 28 | + * `F` in `MOI.Indicator{A,MOI.EqualTo{T}}` |
| 29 | +
|
| 30 | +where `F` is the type of the real/imaginary part of `G`. |
| 31 | +""" |
| 32 | +struct SplitComplexIndicatorEqualToBridge{ |
| 33 | + T, |
| 34 | + F<:MOI.Utilities.TypedVectorLike{T}, |
| 35 | + G<:MOI.Utilities.TypedVectorLike{Complex{T}}, |
| 36 | + A, |
| 37 | +} <: AbstractBridge |
| 38 | + real_constraint::Union{ |
| 39 | + Nothing, |
| 40 | + MOI.ConstraintIndex{F,MOI.Indicator{A,MOI.EqualTo{T}}}, |
| 41 | + } |
| 42 | + imag_constraint::Union{ |
| 43 | + Nothing, |
| 44 | + MOI.ConstraintIndex{F,MOI.Indicator{A,MOI.EqualTo{T}}}, |
| 45 | + } |
| 46 | +end |
| 47 | + |
| 48 | +const SplitComplexIndicatorEqualTo{T,OT<:MOI.ModelLike} = |
| 49 | + SingleBridgeOptimizer{SplitComplexIndicatorEqualToBridge{T},OT} |
| 50 | + |
| 51 | +function _add_constraint_if_nonzero(model, A, f, x, rhs::T) where {T} |
| 52 | + if iszero(f) && iszero(rhs) |
| 53 | + return nothing |
| 54 | + end |
| 55 | + g = MOI.Utilities.operate(vcat, T, x, f) |
| 56 | + return MOI.add_constraint(model, g, MOI.Indicator{A}(MOI.EqualTo(rhs))) |
| 57 | +end |
| 58 | + |
| 59 | +function bridge_constraint( |
| 60 | + ::Type{SplitComplexIndicatorEqualToBridge{T,F,G,A}}, |
| 61 | + model::MOI.ModelLike, |
| 62 | + func::G, |
| 63 | + set::MOI.Indicator{A,MOI.EqualTo{Complex{T}}}, |
| 64 | +) where {T,F,G,A} |
| 65 | + @assert MOI.output_dimension(func) == 2 |
| 66 | + scalars = MOI.Utilities.scalarize(func) |
| 67 | + x, f = convert(MOI.VariableIndex, scalars[1]), scalars[2] |
| 68 | + rhs = set.set.value |
| 69 | + real_ci = _add_constraint_if_nonzero(model, A, real(f), x, real(rhs)) |
| 70 | + imag_f = MOI.Utilities.operate(imag, T, f) |
| 71 | + imag_ci = _add_constraint_if_nonzero(model, A, imag_f, x, imag(rhs)) |
| 72 | + return SplitComplexIndicatorEqualToBridge{T,F,G,A}(real_ci, imag_ci) |
| 73 | +end |
| 74 | + |
| 75 | +function MOI.supports_constraint( |
| 76 | + ::Type{<:SplitComplexIndicatorEqualToBridge{T}}, |
| 77 | + ::Type{<:MOI.Utilities.TypedVectorLike{Complex{T}}}, |
| 78 | + ::Type{MOI.Indicator{A,MOI.EqualTo{Complex{T}}}}, |
| 79 | +) where {T,A} |
| 80 | + return true |
| 81 | +end |
| 82 | + |
| 83 | +function MOI.Bridges.added_constrained_variable_types( |
| 84 | + ::Type{<:SplitComplexIndicatorEqualToBridge}, |
| 85 | +) |
| 86 | + return Tuple{Type}[] |
| 87 | +end |
| 88 | + |
| 89 | +function MOI.Bridges.added_constraint_types( |
| 90 | + ::Type{SplitComplexIndicatorEqualToBridge{T,F,G,A}}, |
| 91 | +) where {T,F,G,A} |
| 92 | + return Tuple{Type,Type}[(F, MOI.Indicator{A,MOI.EqualTo{T}})] |
| 93 | +end |
| 94 | + |
| 95 | +function concrete_bridge_type( |
| 96 | + ::Type{<:SplitComplexIndicatorEqualToBridge{T}}, |
| 97 | + G::Type{<:MOI.Utilities.TypedLike}, |
| 98 | + ::Type{MOI.Indicator{A,MOI.EqualTo{Complex{T}}}}, |
| 99 | +) where {T,A} |
| 100 | + F = MA.promote_operation(imag, G) |
| 101 | + return SplitComplexIndicatorEqualToBridge{T,F,G,A} |
| 102 | +end |
| 103 | + |
| 104 | +function MOI.get( |
| 105 | + model::MOI.ModelLike, |
| 106 | + attr::MOI.ConstraintFunction, |
| 107 | + bridge::SplitComplexIndicatorEqualToBridge{T,F,G,A}, |
| 108 | +) where {T,F,G,A} |
| 109 | + H = MOI.Utilities.scalar_type(G) |
| 110 | + h = zero(H) |
| 111 | + x = nothing |
| 112 | + if bridge.real_constraint !== nothing |
| 113 | + f = MOI.get(model, attr, bridge.real_constraint) |
| 114 | + f_scalars = MOI.Utilities.scalarize(f) |
| 115 | + x = convert(MOI.VariableIndex, f_scalars[1]) |
| 116 | + MOI.Utilities.operate!(+, Complex{T}, h, convert(H, f_scalars[2])) |
| 117 | + end |
| 118 | + if bridge.imag_constraint !== nothing |
| 119 | + f = MOI.get(model, attr, bridge.imag_constraint) |
| 120 | + f_scalars = MOI.Utilities.scalarize(f) |
| 121 | + x = convert(MOI.VariableIndex, f_scalars[1]) |
| 122 | + MOI.Utilities.operate!(+, Complex{T}, h, im * f_scalars[2]) |
| 123 | + end |
| 124 | + return MOI.Utilities.operate(vcat, Complex{T}, x, h) |
| 125 | +end |
| 126 | + |
| 127 | +function MOI.get( |
| 128 | + model::MOI.ModelLike, |
| 129 | + ::MOI.ConstraintSet, |
| 130 | + bridge::SplitComplexIndicatorEqualToBridge{T,F,G,A}, |
| 131 | +) where {T,F,G,A} |
| 132 | + rhs = zero(T) + zero(T) * im |
| 133 | + if bridge.real_constraint !== nothing |
| 134 | + set = MOI.get(model, MOI.ConstraintSet(), bridge.real_constraint) |
| 135 | + rhs += set.set.value |
| 136 | + end |
| 137 | + if bridge.imag_constraint !== nothing |
| 138 | + set = MOI.get(model, MOI.ConstraintSet(), bridge.imag_constraint) |
| 139 | + rhs += set.set.value * im |
| 140 | + end |
| 141 | + return MOI.Indicator{A}(MOI.EqualTo(rhs)) |
| 142 | +end |
| 143 | + |
| 144 | +function MOI.get( |
| 145 | + bridge::SplitComplexIndicatorEqualToBridge{T,F,G,A}, |
| 146 | + ::MOI.NumberOfConstraints{F,MOI.Indicator{A,MOI.EqualTo{T}}}, |
| 147 | +)::Int64 where {T,F,G,A} |
| 148 | + return Int64(bridge.real_constraint !== nothing) + |
| 149 | + Int64(bridge.imag_constraint !== nothing) |
| 150 | +end |
| 151 | + |
| 152 | +function MOI.get( |
| 153 | + bridge::SplitComplexIndicatorEqualToBridge{T,F,G,A}, |
| 154 | + ::MOI.ListOfConstraintIndices{F,MOI.Indicator{A,MOI.EqualTo{T}}}, |
| 155 | +) where {T,F,G,A} |
| 156 | + list = MOI.ConstraintIndex{F,MOI.Indicator{A,MOI.EqualTo{T}}}[] |
| 157 | + if bridge.real_constraint !== nothing |
| 158 | + push!(list, bridge.real_constraint) |
| 159 | + end |
| 160 | + if bridge.imag_constraint !== nothing |
| 161 | + push!(list, bridge.imag_constraint) |
| 162 | + end |
| 163 | + return list |
| 164 | +end |
| 165 | + |
| 166 | +function MOI.delete( |
| 167 | + model::MOI.ModelLike, |
| 168 | + bridge::SplitComplexIndicatorEqualToBridge, |
| 169 | +) |
| 170 | + if bridge.real_constraint !== nothing |
| 171 | + MOI.delete(model, bridge.real_constraint) |
| 172 | + end |
| 173 | + if bridge.imag_constraint !== nothing |
| 174 | + MOI.delete(model, bridge.imag_constraint) |
| 175 | + end |
| 176 | + return |
| 177 | +end |
| 178 | + |
| 179 | +function MOI.supports( |
| 180 | + model::MOI.ModelLike, |
| 181 | + attr::MOI.ConstraintPrimalStart, |
| 182 | + ::Type{SplitComplexIndicatorEqualToBridge{T,F,G,A}}, |
| 183 | +) where {T,F,G,A} |
| 184 | + return MOI.supports( |
| 185 | + model, |
| 186 | + attr, |
| 187 | + MOI.ConstraintIndex{F,MOI.Indicator{A,MOI.EqualTo{T}}}, |
| 188 | + ) |
| 189 | +end |
| 190 | + |
| 191 | +function MOI.get( |
| 192 | + model::MOI.ModelLike, |
| 193 | + attr::Union{MOI.ConstraintPrimal,MOI.ConstraintPrimalStart}, |
| 194 | + bridge::SplitComplexIndicatorEqualToBridge{T}, |
| 195 | +) where {T} |
| 196 | + ret = zeros(Complex{T}, 2) |
| 197 | + if bridge.real_constraint !== nothing |
| 198 | + value = MOI.get(model, attr, bridge.real_constraint) |
| 199 | + if value == nothing |
| 200 | + return nothing |
| 201 | + end |
| 202 | + ret[1] = value[1] |
| 203 | + ret[2] += value[2] |
| 204 | + end |
| 205 | + if bridge.imag_constraint !== nothing |
| 206 | + value = MOI.get(model, attr, bridge.imag_constraint) |
| 207 | + if value == nothing |
| 208 | + return nothing |
| 209 | + end |
| 210 | + ret[1] = value[1] |
| 211 | + ret[2] += value[2] * im |
| 212 | + end |
| 213 | + return ret |
| 214 | +end |
| 215 | + |
| 216 | +_pass_nothing(f::Function, x::AbstractVector) = [x[1], f(x[2])] |
| 217 | +_pass_nothing(::Any, ::Nothing) = nothing |
| 218 | + |
| 219 | +function MOI.set( |
| 220 | + model::MOI.ModelLike, |
| 221 | + attr::MOI.ConstraintPrimalStart, |
| 222 | + bridge::SplitComplexIndicatorEqualToBridge{T}, |
| 223 | + value, |
| 224 | +) where {T} |
| 225 | + if bridge.real_constraint !== nothing |
| 226 | + MOI.set(model, attr, bridge.real_constraint, _pass_nothing(real, value)) |
| 227 | + end |
| 228 | + if bridge.imag_constraint !== nothing |
| 229 | + MOI.set(model, attr, bridge.imag_constraint, _pass_nothing(imag, value)) |
| 230 | + end |
| 231 | + return |
| 232 | +end |
0 commit comments