-
-
Notifications
You must be signed in to change notification settings - Fork 401
/
Copy pathsd.jl
659 lines (565 loc) · 18.4 KB
/
sd.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
# Copyright 2017, Iain Dunning, Joey Huchette, Miles Lubin, and contributors
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#############################################################################
# JuMP
# An algebraic modeling language for Julia
# See https://github.com/jump-dev/JuMP.jl
#############################################################################
"""
vectorize(matrix::AbstractMatrix, ::Shape)
Convert the `matrix` into a vector according to `Shape`.
"""
function vectorize end
"""
SymmetricMatrixSpace()
Use in the [`@variable`](@ref) macro to constrain a matrix of variables to be
symmetric.
## Example
```jldoctest
julia> model = Model();
julia> @variable(model, Q[1:2, 1:2] in SymmetricMatrixSpace())
2×2 LinearAlgebra.Symmetric{VariableRef, Matrix{VariableRef}}:
Q[1,1] Q[1,2]
Q[1,2] Q[2,2]
```
"""
struct SymmetricMatrixSpace end
"""
SkewSymmetricMatrixSpace()
Use in the [`@variable`](@ref) macro to constrain a matrix of variables to be
skew-symmetric.
## Example
```jldoctest
julia> model = Model();
julia> @variable(model, Q[1:2, 1:2] in SkewSymmetricMatrixSpace())
2×2 Matrix{AffExpr}:
0 Q[1,2]
-Q[1,2] 0
```
"""
struct SkewSymmetricMatrixSpace end
"""
HermitianMatrixSpace()
Use in the [`@variable`](@ref) macro to constrain a matrix of variables to be
hermitian.
## Example
```jldoctest
julia> model = Model();
julia> @variable(model, Q[1:2, 1:2] in HermitianMatrixSpace())
2×2 LinearAlgebra.Hermitian{GenericAffExpr{ComplexF64, VariableRef}, Matrix{GenericAffExpr{ComplexF64, VariableRef}}}:
real(Q[1,1]) real(Q[1,2]) + imag(Q[1,2]) im
real(Q[1,2]) - imag(Q[1,2]) im real(Q[2,2])
```
"""
struct HermitianMatrixSpace end
"""
PSDCone
Positive semidefinite cone object that can be used to constrain a square matrix
to be positive semidefinite in the [`@constraint`](@ref) macro.
If the matrix has type `Symmetric` then the columns vectorization (the vector
obtained by concatenating the columns) of its upper triangular part is
constrained to belong to the [`MOI.PositiveSemidefiniteConeTriangle`](@ref) set,
otherwise its column vectorization is constrained to belong to the
[`MOI.PositiveSemidefiniteConeSquare`](@ref) set.
## Example
Non-symmetric case:
```jldoctest
julia> model = Model();
julia> @variable(model, x);
julia> a = [x 2x; 2x x];
julia> b = [1 2; 2 4];
julia> cref = @constraint(model, a >= b, PSDCone())
[x - 1 2 x - 2
2 x - 2 x - 4] ∈ PSDCone()
julia> jump_function(constraint_object(cref))
4-element Vector{AffExpr}:
x - 1
2 x - 2
2 x - 2
x - 4
julia> moi_set(constraint_object(cref))
MathOptInterface.PositiveSemidefiniteConeSquare(2)
```
Symmetric case:
```jldoctest PSDCone
julia> using LinearAlgebra # For Symmetric
julia> model = Model();
julia> @variable(model, x);
julia> a = [x 2x; 2x x];
julia> b = [1 2; 2 4];
julia> cref = @constraint(model, Symmetric(a - b) in PSDCone())
[x - 1 2 x - 2
2 x - 2 x - 4] ∈ PSDCone()
julia> jump_function(constraint_object(cref))
3-element Vector{AffExpr}:
x - 1
2 x - 2
x - 4
julia> moi_set(constraint_object(cref))
MathOptInterface.PositiveSemidefiniteConeTriangle(2)
```
"""
struct PSDCone end
"""
SymmetricMatrixShape
Shape object for a symmetric square matrix of `side_dimension` rows and columns.
The vectorized form contains the entries of the upper-right triangular part of
the matrix given column by column (or equivalently, the entries of the
lower-left triangular part given row by row).
"""
struct SymmetricMatrixShape <: AbstractShape
side_dimension::Int
end
function reshape_vector(
vectorized_form::Vector{T},
shape::SymmetricMatrixShape,
) where {T}
matrix = Matrix{T}(undef, shape.side_dimension, shape.side_dimension)
k = 0
for j in 1:shape.side_dimension
for i in 1:j
k += 1
matrix[j, i] = matrix[i, j] = vectorized_form[k]
end
end
return LinearAlgebra.Symmetric(matrix)
end
function reshape_set(
::MOI.PositiveSemidefiniteConeTriangle,
::SymmetricMatrixShape,
)
return PSDCone()
end
"""
triangle_vec(matrix::Matrix)
Return the upper triangle of a matrix concatenated into a vector in the order
required by JuMP and MathOptInterface for `Triangle` sets.
## Example
```jldoctest
julia> model = Model();
julia> @variable(model, X[1:3, 1:3], Symmetric);
julia> @variable(model, t)
t
julia> @constraint(model, [t; triangle_vec(X)] in MOI.RootDetConeTriangle(3))
[t, X[1,1], X[1,2], X[2,2], X[1,3], X[2,3], X[3,3]] ∈ MathOptInterface.RootDetConeTriangle(3)
```
"""
function triangle_vec(matrix::AbstractMatrix)
n = LinearAlgebra.checksquare(matrix)
return [matrix[i, j] for j in 1:n for i in 1:j]
end
vectorize(matrix, ::SymmetricMatrixShape) = triangle_vec(matrix)
"""
SkewSymmetricMatrixShape
Shape object for a skew symmetric square matrix of `side_dimension` rows and
columns. The vectorized form contains the entries of the upper-right triangular
part of the matrix (without the diagonal) given column by column (or
equivalently, the entries of the lower-left triangular part given row by row).
The diagonal is zero.
"""
struct SkewSymmetricMatrixShape <: AbstractShape
side_dimension::Int
end
function reshape_vector(
vectorized_form::Vector{T},
shape::SkewSymmetricMatrixShape,
) where {T}
NewType = Base.promote_type(
T,
_MA.promote_operation(-, T),
_MA.promote_operation(zero, T),
)
matrix = Matrix{NewType}(undef, shape.side_dimension, shape.side_dimension)
k = 0
for j in 1:shape.side_dimension
for i in 1:(j-1)
k += 1
matrix[i, j] = vectorized_form[k]
matrix[j, i] = -vectorized_form[k]
end
matrix[j, j] = zero(NewType)
end
return matrix
end
function vectorize(matrix, ::SkewSymmetricMatrixShape)
n = LinearAlgebra.checksquare(matrix)
return [matrix[i, j] for j in 1:n for i in 1:j-1]
end
"""
SquareMatrixShape
Shape object for a square matrix of `side_dimension` rows and columns. The
vectorized form contains the entries of the matrix given column by column
(or equivalently, the entries of the lower-left triangular part given row by
row).
"""
struct SquareMatrixShape <: AbstractShape
side_dimension::Int
end
function reshape_vector(
vectorized_form::Vector{T},
shape::SquareMatrixShape,
) where {T}
return reshape(vectorized_form, shape.side_dimension, shape.side_dimension)
end
function reshape_set(::MOI.PositiveSemidefiniteConeSquare, ::SquareMatrixShape)
return PSDCone()
end
vectorize(matrix::Matrix, ::SquareMatrixShape) = vec(matrix)
function vectorize(matrix, shape::SquareMatrixShape)
return vectorize(Matrix(matrix), shape)
end
# This is a special method because calling `Matrix(matrix)` accesses an undef
# reference.
function vectorize(matrix::LinearAlgebra.UpperTriangular, ::SquareMatrixShape)
n = LinearAlgebra.checksquare(matrix)
return [matrix[i, j] for j in 1:n for i in 1:n]
end
# This is a special method because calling `Matrix(matrix)` accesses an undef
# reference.
function vectorize(matrix::LinearAlgebra.LowerTriangular, ::SquareMatrixShape)
n = LinearAlgebra.checksquare(matrix)
return [matrix[i, j] for j in 1:n for i in 1:n]
end
function _square_side(error_fn::Function, variables::Matrix)
n, m = size(variables)
if n != m
error_fn("Symmetric variables must be square. Got size ($n, $m).")
end
return n
end
function _vectorize_variables(error_fn::Function, matrix::Matrix)
n = LinearAlgebra.checksquare(matrix)
for j in 1:n
for i in 1:j
if matrix[i, j] != matrix[j, i]
error_fn(
"Non-symmetric bounds, integrality or starting values for symmetric variable.",
)
end
end
end
return vectorize(matrix, SymmetricMatrixShape(n))
end
function build_variable(
error_fn::Function,
variables::Matrix{<:AbstractVariable},
::SymmetricMatrixSpace,
)
n = _square_side(error_fn, variables)
set = MOI.Reals(MOI.dimension(MOI.PositiveSemidefiniteConeTriangle(n)))
shape = SymmetricMatrixShape(n)
return VariablesConstrainedOnCreation(
_vectorize_variables(error_fn, variables),
set,
shape,
)
end
function build_variable(
error_fn::Function,
variables::Matrix{<:AbstractVariable},
::SkewSymmetricMatrixSpace,
)
n = _square_side(error_fn, variables)
set = MOI.Reals(div(n^2 - n, 2))
shape = SkewSymmetricMatrixShape(n)
return VariablesConstrainedOnCreation(
vectorize(variables, SkewSymmetricMatrixShape(n)),
set,
shape,
)
end
function build_variable(
error_fn::Function,
variables::Matrix{<:AbstractVariable},
::HermitianMatrixSpace,
)
n = _square_side(error_fn, variables)
set = MOI.Reals(
MOI.dimension(MOI.HermitianPositiveSemidefiniteConeTriangle(n)),
)
shape = HermitianMatrixShape(n)
return VariablesConstrainedOnCreation(
_vectorize_complex_variables(error_fn, variables),
set,
shape,
)
end
function build_variable(
error_fn::Function,
variables::Matrix{<:AbstractVariable},
::PSDCone,
)
n = _square_side(error_fn, variables)
set = MOI.PositiveSemidefiniteConeTriangle(n)
return build_variable(error_fn, variables, set)
end
function value(
Q::LinearAlgebra.Symmetric{V,Matrix{V}},
) where {V<:AbstractVariableRef}
return LinearAlgebra.Symmetric(
value.(LinearAlgebra.parent(Q)),
LinearAlgebra.sym_uplo(Q.uplo),
)
end
function build_constraint(
error_fn::Function,
Q::LinearAlgebra.Symmetric{V,M},
::PSDCone,
) where {V<:AbstractJuMPScalar,M<:AbstractMatrix{V}}
n = LinearAlgebra.checksquare(Q)
return build_constraint(
error_fn,
Q,
MOI.PositiveSemidefiniteConeTriangle(n),
)
end
function build_constraint(
error_fn::Function,
Q::AbstractMatrix{<:AbstractJuMPScalar},
::PSDCone,
)
n = LinearAlgebra.checksquare(Q)
return build_constraint(error_fn, Q, MOI.PositiveSemidefiniteConeSquare(n))
end
"""
HermitianPSDCone
Hermitian positive semidefinite cone object that can be used to create a
Hermitian positive semidefinite square matrix in the [`@variable`](@ref)
and [`@constraint`](@ref) macros.
## Example
Consider the following example:
```jldoctest
julia> model = Model();
julia> @variable(model, H[1:3, 1:3] in HermitianPSDCone())
3×3 LinearAlgebra.Hermitian{GenericAffExpr{ComplexF64, VariableRef}, Matrix{GenericAffExpr{ComplexF64, VariableRef}}}:
real(H[1,1]) … real(H[1,3]) + imag(H[1,3]) im
real(H[1,2]) - imag(H[1,2]) im real(H[2,3]) + imag(H[2,3]) im
real(H[1,3]) - imag(H[1,3]) im real(H[3,3])
julia> all_variables(model)
9-element Vector{VariableRef}:
real(H[1,1])
real(H[1,2])
real(H[2,2])
real(H[1,3])
real(H[2,3])
real(H[3,3])
imag(H[1,2])
imag(H[1,3])
imag(H[2,3])
julia> all_constraints(model, Vector{VariableRef}, MOI.HermitianPositiveSemidefiniteConeTriangle)
1-element Vector{ConstraintRef{Model, MathOptInterface.ConstraintIndex{MathOptInterface.VectorOfVariables, MathOptInterface.HermitianPositiveSemidefiniteConeTriangle}}}:
[real(H[1,1]), real(H[1,2]), real(H[2,2]), real(H[1,3]), real(H[2,3]), real(H[3,3]), imag(H[1,2]), imag(H[1,3]), imag(H[2,3])] ∈ MathOptInterface.HermitianPositiveSemidefiniteConeTriangle(3)
```
We see in the output of the last commands that 9 real variables were created.
The matrix `H` constrains affine expressions in terms of these 9 variables that
parametrize a Hermitian matrix.
"""
struct HermitianPSDCone end
"""
HermitianMatrixShape
Shape object for a Hermitian square matrix of `side_dimension` rows and
columns. The vectorized form corresponds to
[`MOI.HermitianPositiveSemidefiniteConeTriangle`](@ref).
"""
struct HermitianMatrixShape <: AbstractShape
side_dimension::Int
end
function vectorize(matrix, ::HermitianMatrixShape)
n = LinearAlgebra.checksquare(matrix)
return vcat(
vectorize(_real.(matrix), SymmetricMatrixShape(n)),
vectorize(
_imag.(matrix[1:(end-1), 2:end]),
SymmetricMatrixShape(n - 1),
),
)
end
function reshape_set(
::MOI.HermitianPositiveSemidefiniteConeTriangle,
::HermitianMatrixShape,
)
return HermitianPSDCone()
end
function reshape_vector(v::Vector{T}, shape::HermitianMatrixShape) where {T}
NewType = _MA.promote_operation(_MA.add_mul, T, Complex{Bool}, T)
n = shape.side_dimension
matrix = Matrix{NewType}(undef, n, n)
real_k = 0
imag_k = MOI.dimension(MOI.PositiveSemidefiniteConeTriangle(n))
for j in 1:n
for i in 1:(j-1)
real_k += 1
imag_k += 1
matrix[i, j] = v[real_k] + im * v[imag_k]
matrix[j, i] = v[real_k] - im * v[imag_k]
end
real_k += 1
matrix[j, j] = v[real_k]
end
return LinearAlgebra.Hermitian(matrix)
end
function _vectorize_complex_variables(error_fn::Function, matrix::Matrix)
if any(_is_binary, matrix) || any(_is_integer, matrix)
# We would then need to fix the imaginary value to zero. Let's wait to
# see if there is need for such complication first.
error_fn(
"Binary or integer variables in a Hermitian matrix are not supported.",
)
end
n = LinearAlgebra.checksquare(matrix)
for j in 1:n
if !_isreal(matrix[j, j])
error_fn(
"Non-real bounds or starting values for diagonal of Hermitian variable.",
)
end
for i in 1:j
if matrix[i, j] != _conj(matrix[j, i])
error_fn(
"Non-conjugate bounds or starting values for Hermitian variable.",
)
end
end
end
return vectorize(matrix, HermitianMatrixShape(n))
end
function build_variable(
error_fn::Function,
variables::Matrix{<:AbstractVariable},
::HermitianPSDCone,
)
n = _square_side(error_fn, variables)
set = MOI.HermitianPositiveSemidefiniteConeTriangle(n)
shape = HermitianMatrixShape(n)
return VariablesConstrainedOnCreation(
_vectorize_complex_variables(error_fn, variables),
set,
shape,
)
end
function build_constraint(
::Function,
Q::LinearAlgebra.Hermitian{V,M},
::HermitianPSDCone,
) where {V<:AbstractJuMPScalar,M<:AbstractMatrix{V}}
n = LinearAlgebra.checksquare(Q)
shape = HermitianMatrixShape(n)
return VectorConstraint(
vectorize(Q, shape),
MOI.HermitianPositiveSemidefiniteConeTriangle(n),
shape,
)
end
function build_constraint(
error_fn::Function,
Q::AbstractMatrix{<:AbstractJuMPScalar},
cone::HermitianPSDCone,
)
return error_fn(
"Unable to add matrix in HermitianPSDCone because the matrix is " *
"not a subtype of `LinearAlgebra.Hermitian`. To fix, wrap the matrix " *
"`H` in `LinearAlgebra.Hermitian(H)`.",
)
end
function build_constraint(
error_fn::Function,
H::LinearAlgebra.Hermitian,
::Zeros,
)
n = LinearAlgebra.checksquare(H)
shape = HermitianMatrixShape(n)
x = vectorize(H, shape)
return VectorConstraint(x, MOI.Zeros(length(x)), shape)
end
reshape_set(s::MOI.Zeros, ::HermitianMatrixShape) = Zeros()
function build_constraint(
error_fn::Function,
f::LinearAlgebra.Symmetric,
::Zeros,
)
n = LinearAlgebra.checksquare(f)
shape = SymmetricMatrixShape(n)
x = vectorize(f, shape)
return VectorConstraint(x, MOI.Zeros(length(x)), shape)
end
reshape_set(::MOI.Zeros, ::SymmetricMatrixShape) = Zeros()
function build_constraint(error_fn::Function, ::AbstractMatrix, ::Nonnegatives)
return error_fn(
"Unsupported matrix in vector-valued set. Did you mean to use the " *
"broadcasting syntax `.>=` instead? Alternatively, perhaps you are " *
"missing a set argument like `@constraint(model, X >= 0, PSDCone())` " *
"or `@constraint(model, X >= 0, HermmitianPSDCone())`.",
)
end
function build_constraint(error_fn::Function, ::AbstractMatrix, ::Nonpositives)
return error_fn(
"Unsupported matrix in vector-valued set. Did you mean to use the " *
"broadcasting syntax `.<=` instead? Alternatively, perhaps you are " *
"missing a set argument like `@constraint(model, X <= 0, PSDCone())` " *
"or `@constraint(model, X <= 0, HermmitianPSDCone())`.",
)
end
function build_constraint(error_fn::Function, ::AbstractMatrix, ::Zeros)
return error_fn(
"Unsupported matrix in vector-valued set. Did you mean to use the " *
"broadcasting syntax `.==` for element-wise equality? Alternatively, " *
"this syntax is supported in the special case that the matrices are " *
"`LinearAlgebra.Symmetric` or `LinearAlgebra.Hermitian`.",
)
end
function build_constraint(
error_fn::Function,
Q::LinearAlgebra.Symmetric{V,M},
set::MOI.AbstractSymmetricMatrixSetTriangle,
) where {V<:AbstractJuMPScalar,M<:AbstractMatrix{V}}
n = LinearAlgebra.checksquare(Q)
shape = SymmetricMatrixShape(n)
return VectorConstraint(vectorize(Q, shape), set, shape)
end
function build_constraint(
error_fn::Function,
Q::AbstractMatrix{<:AbstractJuMPScalar},
set::MOI.AbstractSymmetricMatrixSetSquare,
)
n = LinearAlgebra.checksquare(Q)
shape = SquareMatrixShape(n)
return VectorConstraint(vectorize(Q, shape), set, shape)
end
function build_constraint(
error_fn::Function,
f::AbstractMatrix{<:AbstractJuMPScalar},
::Nonnegatives,
extra::Union{
MOI.AbstractSymmetricMatrixSetTriangle,
MOI.AbstractSymmetricMatrixSetSquare,
PSDCone,
HermitianPSDCone,
},
)
return build_constraint(error_fn, f, extra)
end
function build_constraint(
error_fn::Function,
f::AbstractMatrix{<:AbstractJuMPScalar},
::Nonpositives,
extra::Union{
MOI.AbstractSymmetricMatrixSetTriangle,
MOI.AbstractSymmetricMatrixSetSquare,
PSDCone,
HermitianPSDCone,
},
)
new_f = _MA.operate!!(*, -1, f)
return build_constraint(error_fn, new_f, extra)
end
function build_variable(
error_fn::Function,
variables::Matrix{<:AbstractVariable},
set::MOI.AbstractSymmetricMatrixSetTriangle,
)
n = _square_side(error_fn, variables)
x = _vectorize_variables(error_fn, variables)
return VariablesConstrainedOnCreation(x, set, SymmetricMatrixShape(n))
end