|
| 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 | + SymmetricMatrixScalingBridge{T,F,G} <: Bridges.Constraint.AbstractBridge |
| 9 | +
|
| 10 | +`SymmetricMatrixScalingBridge` implements the reformulation from constraints |
| 11 | +in the `MOI.PositiveSemidefiniteConeTriangle` to constraints |
| 12 | +in the `MOI.ScaledPositiveSemidefiniteConeTriangle`. |
| 13 | +
|
| 14 | +## Source node |
| 15 | +
|
| 16 | +`SymmetricMatrixScalingBridge` supports: |
| 17 | +
|
| 18 | + * `G` in [`MOI.PositiveSemidefiniteConeTriangle`](@ref) |
| 19 | +
|
| 20 | +## Target node |
| 21 | +
|
| 22 | +`SymmetricMatrixScalingBridge` creates: |
| 23 | +
|
| 24 | + * `F` in [`MOI.ScaledPositiveSemidefiniteConeTriangle`](@ref) |
| 25 | +""" |
| 26 | +struct SymmetricMatrixScalingBridge{T,F,G} <: SetMapBridge{ |
| 27 | + T, |
| 28 | + MOI.ScaledPositiveSemidefiniteConeTriangle, |
| 29 | + MOI.PositiveSemidefiniteConeTriangle, |
| 30 | + F, |
| 31 | + G, |
| 32 | +} |
| 33 | + constraint::MOI.ConstraintIndex{ |
| 34 | + F, |
| 35 | + MOI.ScaledPositiveSemidefiniteConeTriangle, |
| 36 | + } |
| 37 | +end |
| 38 | + |
| 39 | +const SymmetricMatrixScaling{T,OT<:MOI.ModelLike} = |
| 40 | + SingleBridgeOptimizer{SymmetricMatrixScalingBridge{T},OT} |
| 41 | + |
| 42 | +function concrete_bridge_type( |
| 43 | + ::Type{<:SymmetricMatrixScalingBridge{T}}, |
| 44 | + G::Type{<:MOI.AbstractVectorFunction}, |
| 45 | + ::Type{MOI.PositiveSemidefiniteConeTriangle}, |
| 46 | +) where {T} |
| 47 | + F = MOI.Utilities.promote_operation( |
| 48 | + *, |
| 49 | + T, |
| 50 | + LinearAlgebra.Diagonal{T,Vector{T}}, |
| 51 | + G, |
| 52 | + ) |
| 53 | + return SymmetricMatrixScalingBridge{T,F,G} |
| 54 | +end |
| 55 | + |
| 56 | +function MOI.Bridges.map_set( |
| 57 | + ::Type{<:SymmetricMatrixScalingBridge}, |
| 58 | + set::MOI.PositiveSemidefiniteConeTriangle, |
| 59 | +) |
| 60 | + return MOI.ScaledPositiveSemidefiniteConeTriangle(set.side_dimension) |
| 61 | +end |
| 62 | + |
| 63 | +function MOI.Bridges.inverse_map_set( |
| 64 | + ::Type{<:SymmetricMatrixScalingBridge}, |
| 65 | + set::MOI.ScaledPositiveSemidefiniteConeTriangle, |
| 66 | +) |
| 67 | + return MOI.PositiveSemidefiniteConeTriangle(set.side_dimension) |
| 68 | +end |
| 69 | + |
| 70 | +_length(f::MOI.AbstractVectorFunction) = MOI.output_dimension(f) |
| 71 | +_length(f::AbstractVector) = length(f) |
| 72 | + |
| 73 | +function MOI.Bridges.map_function( |
| 74 | + ::Type{<:SymmetricMatrixScalingBridge{T}}, |
| 75 | + func, |
| 76 | +) where {T} |
| 77 | + scale = MOI.Utilities.symmetric_matrix_scaling_vector(T, _length(func)) |
| 78 | + return MOI.Utilities.operate(*, T, LinearAlgebra.Diagonal(scale), func) |
| 79 | +end |
| 80 | + |
| 81 | +function MOI.Bridges.inverse_map_function( |
| 82 | + ::Type{<:SymmetricMatrixScalingBridge{T}}, |
| 83 | + func, |
| 84 | +) where {T} |
| 85 | + N = _length(func) |
| 86 | + scale = MOI.Utilities.symmetric_matrix_inverse_scaling_vector(T, N) |
| 87 | + return MOI.Utilities.operate(*, T, LinearAlgebra.Diagonal(scale), func) |
| 88 | +end |
| 89 | + |
| 90 | +# Since the map is a diagonal matrix `D`, it is symmetric so one would initially |
| 91 | +# expect `adjoint_map_function` to be the same as `map_function`. However, the |
| 92 | +# scalar product for the scaled PSD cone is `<x, y>_2 = x'y` but the scalar |
| 93 | +# product for the PSD cone additionally scales the offdiagonal entries by `2` |
| 94 | +# hence by `D^2` so `<x, y>_1 = x'D^2y`. |
| 95 | +# So `<Dx, y>_2 = <x, D^(-1)y>_1` hence the adjoint of `D` is its inverse! |
| 96 | +function MOI.Bridges.adjoint_map_function( |
| 97 | + BT::Type{<:SymmetricMatrixScalingBridge}, |
| 98 | + func, |
| 99 | +) |
| 100 | + return MOI.Bridges.inverse_map_function(BT, func) |
| 101 | +end |
| 102 | + |
| 103 | +function MOI.Bridges.inverse_adjoint_map_function( |
| 104 | + BT::Type{<:SymmetricMatrixScalingBridge}, |
| 105 | + func, |
| 106 | +) |
| 107 | + return MOI.Bridges.map_function(BT, func) |
| 108 | +end |
| 109 | + |
| 110 | +""" |
| 111 | + SymmetricMatrixInverseScalingBridge{T,F,G} <: Bridges.Constraint.AbstractBridge |
| 112 | +
|
| 113 | +`SymmetricMatrixInverseScalingBridge` implements the reformulation from constraints |
| 114 | +in the `MOI.ScaledPositiveSemidefiniteConeTriangle` to constraints |
| 115 | +in the `MOI.PositiveSemidefiniteConeTriangle`. |
| 116 | +
|
| 117 | +## Source node |
| 118 | +
|
| 119 | +`SymmetricMatrixInverseScalingBridge` supports: |
| 120 | +
|
| 121 | + * `G` in [`MOI.ScaledPositiveSemidefiniteConeTriangle`](@ref) |
| 122 | +
|
| 123 | +## Target node |
| 124 | +
|
| 125 | +`SymmetricMatrixInverseScalingBridge` creates: |
| 126 | +
|
| 127 | + * `F` in [`MOI.PositiveSemidefiniteConeTriangle`](@ref) |
| 128 | +""" |
| 129 | +struct SymmetricMatrixInverseScalingBridge{T,F,G} <: SetMapBridge{ |
| 130 | + T, |
| 131 | + MOI.PositiveSemidefiniteConeTriangle, |
| 132 | + MOI.ScaledPositiveSemidefiniteConeTriangle, |
| 133 | + F, |
| 134 | + G, |
| 135 | +} |
| 136 | + constraint::MOI.ConstraintIndex{F,MOI.PositiveSemidefiniteConeTriangle} |
| 137 | +end |
| 138 | + |
| 139 | +const SymmetricMatrixInverseScaling{T,OT<:MOI.ModelLike} = |
| 140 | + SingleBridgeOptimizer{SymmetricMatrixInverseScalingBridge{T},OT} |
| 141 | + |
| 142 | +function concrete_bridge_type( |
| 143 | + ::Type{<:SymmetricMatrixInverseScalingBridge{T}}, |
| 144 | + G::Type{<:MOI.AbstractVectorFunction}, |
| 145 | + ::Type{MOI.ScaledPositiveSemidefiniteConeTriangle}, |
| 146 | +) where {T} |
| 147 | + F = MOI.Utilities.promote_operation( |
| 148 | + *, |
| 149 | + T, |
| 150 | + LinearAlgebra.Diagonal{T,Vector{T}}, |
| 151 | + G, |
| 152 | + ) |
| 153 | + return SymmetricMatrixInverseScalingBridge{T,F,G} |
| 154 | +end |
| 155 | + |
| 156 | +function MOI.Bridges.map_set( |
| 157 | + ::Type{<:SymmetricMatrixInverseScalingBridge}, |
| 158 | + set::MOI.ScaledPositiveSemidefiniteConeTriangle, |
| 159 | +) |
| 160 | + return MOI.PositiveSemidefiniteConeTriangle(set.side_dimension) |
| 161 | +end |
| 162 | + |
| 163 | +function MOI.Bridges.inverse_map_set( |
| 164 | + ::Type{<:SymmetricMatrixInverseScalingBridge}, |
| 165 | + set::MOI.PositiveSemidefiniteConeTriangle, |
| 166 | +) |
| 167 | + return MOI.ScaledPositiveSemidefiniteConeTriangle(set.side_dimension) |
| 168 | +end |
| 169 | + |
| 170 | +function MOI.Bridges.map_function( |
| 171 | + ::Type{<:SymmetricMatrixInverseScalingBridge{T}}, |
| 172 | + func, |
| 173 | +) where {T} |
| 174 | + N = _length(func) |
| 175 | + scale = MOI.Utilities.symmetric_matrix_inverse_scaling_vector(T, N) |
| 176 | + return MOI.Utilities.operate(*, T, LinearAlgebra.Diagonal(scale), func) |
| 177 | +end |
| 178 | + |
| 179 | +function MOI.Bridges.inverse_map_function( |
| 180 | + ::Type{<:SymmetricMatrixInverseScalingBridge{T}}, |
| 181 | + func, |
| 182 | +) where {T} |
| 183 | + N = _length(func) |
| 184 | + scale = MOI.Utilities.symmetric_matrix_scaling_vector(T, N) |
| 185 | + return MOI.Utilities.operate(*, T, LinearAlgebra.Diagonal(scale), func) |
| 186 | +end |
| 187 | + |
| 188 | +function MOI.Bridges.adjoint_map_function( |
| 189 | + BT::Type{<:SymmetricMatrixInverseScalingBridge}, |
| 190 | + func, |
| 191 | +) |
| 192 | + return MOI.Bridges.inverse_map_function(BT, func) |
| 193 | +end |
| 194 | + |
| 195 | +function MOI.Bridges.inverse_adjoint_map_function( |
| 196 | + BT::Type{<:SymmetricMatrixInverseScalingBridge}, |
| 197 | + func, |
| 198 | +) |
| 199 | + return MOI.Bridges.map_function(BT, func) |
| 200 | +end |
0 commit comments