/
constraint.jl
242 lines (201 loc) · 8.6 KB
/
constraint.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
export certificate_monomials, gram_matrix, lagrangian_multipliers
export NonnegPolyInnerCone, DSOSCone, SDSOSCone, SOSCone
export PSDMatrixInnerCone, SOSMatrixCone
export ConvexPolyInnerCone, SOSConvexCone
"""
struct NonnegPolyInnerCone{MCT <: MOI.AbstractVectorSet}
end
Inner approximation of the cone of nonnegative polynomials defined by the set
of polynomials of the form
```julia
X^\\top Q X
```
where `X` is a vector of monomials and `Q` is a symmetric matrix that belongs to
the cone `psd_inner`.
"""
struct NonnegPolyInnerCone{MCT <: MOI.AbstractVectorSet} <: SOSLikeCone
end
matrix_cone_type(::Type{NonnegPolyInnerCone{MCT}}) where {MCT} = MCT
_wrap(::Type{JuMP.REPLMode}, s) = s
_wrap(::Type{JuMP.IJuliaMode}, s) = "\\text{ " * s * "}"
"""
const SOSCone = NonnegPolyInnerCone{MOI.PositiveSemidefiniteConeTriangle}
Sum-of-squares cone; see [`NonnegPolyInnerCone`](@ref).
"""
const SOSCone = NonnegPolyInnerCone{MOI.PositiveSemidefiniteConeTriangle}
function JuMP.in_set_string(print_mode, ::SOSCone)
return _wrap(print_mode, "is SOS")
end
"""
const SDSOSCone = NonnegPolyInnerCone{ScaledDiagonallyDominantConeTriangle}
Scaled-diagonally-dominant-sum-of-squares cone; see [Definition 2, AM17] and
[`NonnegPolyInnerCone`](@ref).
[AM17] Ahmadi, A. A. & Majumdar, A.
*DSOS and SDSOS Optimization: More Tractable Alternatives to Sum of Squares and Semidefinite Optimization*
ArXiv e-prints, **2017**.
"""
const SDSOSCone = NonnegPolyInnerCone{ScaledDiagonallyDominantConeTriangle}
function JuMP.in_set_string(print_mode, ::SDSOSCone)
return _wrap(print_mode, "is SDSOS")
end
"""
const DSOSCone = NonnegPolyInnerCone{DiagonallyDominantConeTriangle}
Diagonally-dominant-sum-of-squares cone; see [Definition 2, AM17] and
[`NonnegPolyInnerCone`](@ref).
[AM17] Ahmadi, A. A. & Majumdar, A.
*DSOS and SDSOS Optimization: More Tractable Alternatives to Sum of Squares and Semidefinite Optimization*
ArXiv e-prints, **2017**.
"""
const DSOSCone = NonnegPolyInnerCone{DiagonallyDominantConeTriangle}
function JuMP.in_set_string(print_mode, ::DSOSCone)
return _wrap(print_mode, "is DSOS")
end
function JuMP.reshape_set(set::SOSPolynomialSet, ::PolyJuMP.PolynomialShape)
return set.certificate.cone
end
function JuMP.moi_set(cone::SOSLikeCone,
monos::AbstractVector{<:MP.AbstractMonomial};
domain::AbstractSemialgebraicSet=FullSpace(),
basis=MonomialBasis,
newton_polytope::Tuple=tuple(),
mindegree=MP.mindegree(monos),
maxdegree=MP.maxdegree(monos))
ideal_certificate = Certificate.Remainder(cone, basis, newton_polytope)
if domain isa AbstractAlgebraicSet
certificate = ideal_certificate
else
certificate = Certificate.Putinar(
ideal_certificate, cone, basis, mindegree, maxdegree)
end
return SOSPolynomialSet(domain, monos, certificate)
end
function PolyJuMP.bridges(::Type{<:MOI.AbstractVectorFunction},
::Type{EmptyCone})
return [Bridges.Constraint.EmptyBridge]
end
function PolyJuMP.bridges(::Type{<:MOI.AbstractVectorFunction},
::Type{PositiveSemidefinite2x2ConeTriangle})
return [Bridges.Constraint.PositiveSemidefinite2x2Bridge]
end
function PolyJuMP.bridges(::Type{<:MOI.AbstractVectorFunction},
::Type{<:DiagonallyDominantConeTriangle})
return [Bridges.Constraint.DiagonallyDominantBridge]
end
function PolyJuMP.bridges(::Type{<:MOI.AbstractVectorFunction},
::Type{<:ScaledDiagonallyDominantConeTriangle})
return [Bridges.Constraint.ScaledDiagonallyDominantBridge]
end
function PolyJuMP.bridges(::Type{<:MOI.AbstractVectorFunction},
::Type{<:SOSPolynomialSet{<:AbstractAlgebraicSet}})
return [Bridges.Constraint.SOSPolynomialBridge]
end
function PolyJuMP.bridges(::Type{<:MOI.AbstractVectorFunction},
::Type{<:SOSPolynomialSet{<:BasicSemialgebraicSet}})
return [Bridges.Constraint.SOSPolynomialInSemialgebraicSetBridge]
end
function JuMP.build_constraint(_error::Function, p, cone::SOSLikeCone; kws...)
coefs = PolyJuMP.non_constant_coefficients(p)
monos = MP.monomials(p)
set = JuMP.moi_set(cone, monos; kws...)
shape = PolyJuMP.PolynomialShape(monos)
return PolyJuMP.bridgeable(JuMP.VectorConstraint(coefs, set, shape),
JuMP.moi_function_type(typeof(coefs)),
typeof(set))
end
struct ValueNotSupported <: Exception end
function Base.showerror(io::IO, ::ValueNotSupported)
print(io, "`value` is no supported for Sum-of-Squares constraints, use",
" `gram_matrix` instead.")
end
struct DualNotSupported <: Exception end
function Base.showerror(io::IO, ::DualNotSupported)
print(io, "`dual` is no supported for Sum-of-Squares constraints in a",
" domain, use `moment_matrix` instead.")
end
"""
gram_matrix(cref::JuMP.ConstraintRef)
Return the [`GramMatrixAttribute`](@ref) of `cref`.
"""
function gram_matrix(cref::JuMP.ConstraintRef)
return MOI.get(cref.model, GramMatrixAttribute(), cref)
end
"""
moment_matrix(cref::JuMP.ConstraintRef)
Return the [`MomentMatrixAttribute`](@ref) of `cref`.
"""
function MultivariateMoments.moment_matrix(cref::JuMP.ConstraintRef)
return MOI.get(cref.model, MomentMatrixAttribute(), cref)
end
# Equivalent but more efficient than moment_matrix(cref).x as it does not need
# to query any result from the solver
"""
certificate_monomials(cref::JuMP.ConstraintRef)
Return the [`CertificateMonomials`](@ref) of `cref`.
"""
function certificate_monomials(cref::JuMP.ConstraintRef)
return MOI.get(cref.model, CertificateMonomials(), cref)
end
"""
lagrangian_multipliers(cref::JuMP.ConstraintRef)
Return the [`LagrangianMultipliers`](@ref) of `cref`.
"""
function lagrangian_multipliers(cref::JuMP.ConstraintRef)
return MOI.get(cref.model, LagrangianMultipliers(), cref)
end
"""
struct PSDMatrixInnerCone{MCT <: MOI.AbstractVectorSet} <: PolyJuMP.PolynomialSet
end
Inner approximation of the cone of polynomial matrices `P(x)` that are positive
semidefinite for any `x` defined by the set of ``n \\times n`` polynomial
matrices such that the polynomial ``y^\\top P(x) y`` belongs to
`NonnegPolyInnerCone{MCT}` where `y` is a vector of ``n`` auxiliary polynomial
variables.
"""
struct PSDMatrixInnerCone{MCT <: MOI.AbstractVectorSet} <: PolyJuMP.PolynomialSet
end
"""
const SOSMatrixCone = PSDMatrixInnerCone{MOI.PositiveSemidefiniteConeTriangle}
Sum-of-squares matrices cone; see [Section 3.3.2, BPT12] and
[`PSDMatrixInnerCone`](@ref).
[BPT12] Blekherman, G.; Parrilo, P. A. & Thomas, R. R.
*Semidefinite Optimization and Convex Algebraic Geometry*.
Society for Industrial and Applied Mathematics, 2012.
"""
const SOSMatrixCone = PSDMatrixInnerCone{MOI.PositiveSemidefiniteConeTriangle}
function JuMP.build_constraint(_error::Function, P::Matrix{<:MP.APL},
::PSDMatrixInnerCone{MCT};
newton_polytope::Tuple = tuple(),
kws...) where MCT
n = LinearAlgebra.checksquare(P)
if !issymmetric(P)
_error("The polynomial matrix constrained to be SOS must be symmetric.")
end
y = [MP.similarvariable(eltype(P), gensym()) for i in 1:n]
p = dot(y, P * y)
# TODO Newton_polytope=(y,) may not be the best idea if exact newton
# polytope computation is used.
# See "Sum-of-Squares Matrices" notebook
JuMP.build_constraint(_error, p, NonnegPolyInnerCone{MCT}();
newton_polytope=(y, newton_polytope...), kws...)
end
"""
struct ConvexPolyInnerCone{MCT} <: PolyJuMP.PolynomialSet end
Inner approximation of the set of convex polynomials defined by the set of
polynomials such that their hessian belongs to to the set
`PSDMatrixInnerCone{MCT}()`
"""
struct ConvexPolyInnerCone{MCT} <: PolyJuMP.PolynomialSet end
"""
const SOSConvexCone = ConvexPolyInnerCone{MOI.PositiveSemidefiniteConeTriangle}
Sum-of-squares convex polynomials cone; see [Section 3.3.3, BPT12] and
[`ConvexPolyInnerCone`](@ref).
[BPT12] Blekherman, G.; Parrilo, P. A. & Thomas, R. R.
*Semidefinite Optimization and Convex Algebraic Geometry*.
Society for Industrial and Applied Mathematics, 2012.
"""
const SOSConvexCone = ConvexPolyInnerCone{MOI.PositiveSemidefiniteConeTriangle}
function JuMP.build_constraint(_error::Function, p::MP.APL,
::ConvexPolyInnerCone{MCT}; kws...) where MCT
hessian = MP.differentiate(p, MP.variables(p), 2)
JuMP.build_constraint(_error, hessian, PSDMatrixInnerCone{MCT}(); kws...)
end