|
| 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 | + DualGeoMeanBridge{T,G,H} <: Bridges.Constraint.AbstractBridge |
| 9 | +
|
| 10 | +`DualGeoMeanBridge` implements the following reformulation: |
| 11 | +
|
| 12 | + * ``(u, v) \\in DualGeometricMeanCone`` into |
| 13 | + ``(-u/length(v), v) \\in GeometricMeanCone`` and ``u \\le 0`` |
| 14 | +
|
| 15 | +## Source node |
| 16 | +
|
| 17 | +`DualGeoMeanBridge` supports: |
| 18 | +
|
| 19 | + * `H` in [`MOI.DualGeometricMeanCone`](@ref) |
| 20 | +
|
| 21 | +## Target nodes |
| 22 | +
|
| 23 | +`DualGeoMeanBridge` creates: |
| 24 | +
|
| 25 | + * `G` in [`MOI.GeometricMeanCone`](@ref) |
| 26 | + * `G` in [`MOI.Nonnegatives`](@ref) |
| 27 | +""" |
| 28 | +struct DualGeoMeanBridge{T,G,H} <: AbstractBridge |
| 29 | + nn_index::MOI.ConstraintIndex{G,MOI.Nonnegatives} |
| 30 | + geomean_index::MOI.ConstraintIndex{G,MOI.GeometricMeanCone} |
| 31 | +end |
| 32 | + |
| 33 | +const DualGeoMean{T,OT<:MOI.ModelLike} = |
| 34 | + SingleBridgeOptimizer{DualGeoMeanBridge{T},OT} |
| 35 | + |
| 36 | +function bridge_constraint( |
| 37 | + ::Type{DualGeoMeanBridge{T,G,H}}, |
| 38 | + model::MOI.ModelLike, |
| 39 | + f::H, |
| 40 | + s::MOI.DualGeometricMeanCone, |
| 41 | +) where {T,G,H} |
| 42 | + f_scalars = MOI.Utilities.eachscalar(f) |
| 43 | + u_neg = MOI.Utilities.vectorize([MOI.Utilities.operate(-, T, f_scalars[1])]) |
| 44 | + nn_index = MOI.add_constraint(model, u_neg, MOI.Nonnegatives(1)) |
| 45 | + u_n = MOI.Utilities.operate(/, T, f_scalars[1], -T(MOI.dimension(s) - 1)) |
| 46 | + ci = MOI.add_constraint( |
| 47 | + model, |
| 48 | + MOI.Utilities.operate(vcat, T, u_n, f_scalars[2:end]), |
| 49 | + MOI.GeometricMeanCone(MOI.dimension(s)), |
| 50 | + ) |
| 51 | + return DualGeoMeanBridge{T,G,H}(nn_index, ci) |
| 52 | +end |
| 53 | + |
| 54 | +function MOI.supports_constraint( |
| 55 | + ::Type{<:DualGeoMeanBridge{T}}, |
| 56 | + ::Type{<:MOI.AbstractVectorFunction}, |
| 57 | + ::Type{MOI.DualGeometricMeanCone}, |
| 58 | +) where {T} |
| 59 | + return true |
| 60 | +end |
| 61 | + |
| 62 | +function MOI.Bridges.added_constrained_variable_types( |
| 63 | + ::Type{<:DualGeoMeanBridge}, |
| 64 | +) |
| 65 | + return Tuple{Type}[] |
| 66 | +end |
| 67 | + |
| 68 | +function MOI.Bridges.added_constraint_types( |
| 69 | + ::Type{<:DualGeoMeanBridge{T,G}}, |
| 70 | +) where {T,G} |
| 71 | + return Tuple{Type,Type}[(G, MOI.Nonnegatives), (G, MOI.GeometricMeanCone)] |
| 72 | +end |
| 73 | + |
| 74 | +function concrete_bridge_type( |
| 75 | + ::Type{<:DualGeoMeanBridge{T}}, |
| 76 | + ::Type{H}, |
| 77 | + ::Type{MOI.DualGeometricMeanCone}, |
| 78 | +) where {T,H<:MOI.AbstractVectorFunction} |
| 79 | + S = MOI.Utilities.scalar_type(H) |
| 80 | + TS = MOI.Utilities.promote_operation(+, T, S, MOI.VariableIndex) |
| 81 | + G = MOI.Utilities.promote_operation(vcat, T, T, S, TS) |
| 82 | + return DualGeoMeanBridge{T,G,H} |
| 83 | +end |
| 84 | + |
| 85 | +function MOI.get( |
| 86 | + ::DualGeoMeanBridge{T,G}, |
| 87 | + ::MOI.NumberOfConstraints{G,MOI.Nonnegatives}, |
| 88 | +)::Int64 where {T,G} |
| 89 | + return 1 |
| 90 | +end |
| 91 | + |
| 92 | +function MOI.get( |
| 93 | + bridge::DualGeoMeanBridge{T,G}, |
| 94 | + ::MOI.ListOfConstraintIndices{G,MOI.Nonnegatives}, |
| 95 | +) where {T,G} |
| 96 | + return [bridge.nn_index] |
| 97 | +end |
| 98 | + |
| 99 | +function MOI.get( |
| 100 | + ::DualGeoMeanBridge{T,G}, |
| 101 | + ::MOI.NumberOfConstraints{G,MOI.GeometricMeanCone}, |
| 102 | +)::Int64 where {T,G} |
| 103 | + return 1 |
| 104 | +end |
| 105 | + |
| 106 | +function MOI.get( |
| 107 | + bridge::DualGeoMeanBridge{T,G}, |
| 108 | + ::MOI.ListOfConstraintIndices{G,MOI.GeometricMeanCone}, |
| 109 | +) where {T,G} |
| 110 | + return [bridge.geomean_index] |
| 111 | +end |
| 112 | + |
| 113 | +function MOI.delete(model::MOI.ModelLike, bridge::DualGeoMeanBridge) |
| 114 | + MOI.delete(model, bridge.geomean_index) |
| 115 | + MOI.delete(model, bridge.nn_index) |
| 116 | + return |
| 117 | +end |
| 118 | + |
| 119 | +function MOI.get( |
| 120 | + model::MOI.ModelLike, |
| 121 | + ::MOI.ConstraintFunction, |
| 122 | + bridge::DualGeoMeanBridge{T,G,H}, |
| 123 | +) where {T,G,H} |
| 124 | + g = MOI.get(model, MOI.ConstraintFunction(), bridge.geomean_index) |
| 125 | + scalars = MOI.Utilities.eachscalar(g) |
| 126 | + n = -T(length(scalars) - 1) |
| 127 | + u = MOI.Utilities.operate(*, T, scalars[1], n) |
| 128 | + f = MOI.Utilities.operate(vcat, T, u, scalars[2:end]) |
| 129 | + return MOI.Utilities.convert_approx(H, f) |
| 130 | +end |
| 131 | + |
| 132 | +function MOI.get( |
| 133 | + model::MOI.ModelLike, |
| 134 | + ::MOI.ConstraintSet, |
| 135 | + bridge::DualGeoMeanBridge, |
| 136 | +) |
| 137 | + set = MOI.get(model, MOI.ConstraintSet(), bridge.geomean_index) |
| 138 | + return MOI.DualGeometricMeanCone(MOI.dimension(set)) |
| 139 | +end |
| 140 | + |
| 141 | +function MOI.get( |
| 142 | + model::MOI.ModelLike, |
| 143 | + attr::MOI.ConstraintPrimal, |
| 144 | + bridge::DualGeoMeanBridge, |
| 145 | +) |
| 146 | + primal = MOI.get(model, attr, bridge.geomean_index) |
| 147 | + primal[1] *= -(length(primal) - 1) |
| 148 | + return primal |
| 149 | +end |
| 150 | + |
| 151 | +function MOI.get( |
| 152 | + model::MOI.ModelLike, |
| 153 | + attr::MOI.ConstraintDual, |
| 154 | + bridge::DualGeoMeanBridge, |
| 155 | +) |
| 156 | + dual = MOI.get(model, attr, bridge.geomean_index) |
| 157 | + dual[1] /= -(length(dual) - 1) |
| 158 | + return dual |
| 159 | +end |
0 commit comments