-
Notifications
You must be signed in to change notification settings - Fork 119
/
test_utilities.jl
941 lines (831 loc) · 32.1 KB
/
test_utilities.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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
module TestUtilities
using Convex
using Test
import LinearAlgebra
import MathOptInterface as MOI
import SCS
import SparseArrays
function runtests()
for name in names(@__MODULE__; all = true)
if startswith("$name", "test_")
@testset "$name" begin
getfield(@__MODULE__, name)()
end
end
end
return
end
# It's not super easy to capture the output
# I ended up using this pattern from Suppressor:
# https://github.com/JuliaIO/Suppressor.jl/blob/b4ff08f0fe795a2ce9e592734a758c9e6d8e2bc4/src/Suppressor.jl#L124-L152
function solve_and_return_output(problem, solver; kwargs...)
original_stdout = stdout
rd, wr = redirect_stdout()
out_task = @async read(rd, String)
try
solve!(problem, solver; kwargs...)
finally
Base.Libc.flush_cstdio() # https://github.com/JuliaLang/julia/issues/31236
redirect_stdout(original_stdout)
close(wr)
end
return fetch(out_task)
end
function test_solve!_does_not_return_anything()
x = Variable()
p = satisfy(x >= 0)
output = solve!(
p,
MOI.OptimizerWithAttributes(
SCS.Optimizer,
"verbose" => 0,
"eps_abs" => 1e-6,
),
)
@test output === nothing
return
end
function test_silent_solver_works()
x = Variable()
p = satisfy(x >= 0)
output_non_silent = solve_and_return_output(
p,
MOI.OptimizerWithAttributes(SCS.Optimizer, "eps_abs" => 1e-6),
)
@test output_non_silent != ""
output_silent = solve_and_return_output(
p,
MOI.OptimizerWithAttributes(SCS.Optimizer, "eps_abs" => 1e-6),
silent_solver = true,
)
@test output_silent == ""
return
end
function test_solve!_can_take_an_optimizer_directly()
x = Variable()
p = satisfy(x >= 0)
output = solve!(
p,
MOI.OptimizerWithAttributes(
SCS.Optimizer,
"verbose" => 0,
"eps_abs" => 1e-6,
),
)
@test output === nothing
return
end
function test_complex_objective_function_errors()
x = Variable()
@test_throws ErrorException minimize(x + im * x)
return
end
function test_constant_objective()
x = Variable()
for p in [
satisfy(x == 0, x == 1),
satisfy(Constraint[]),
minimize(0, x == 0),
minimize(0, Constraint[]),
maximize(0, x == 0),
maximize(0, Constraint[]),
]
@test isnothing(p.objective)
end
return
end
function test_invalid_head()
p = Problem(:invalid, nothing, Constraint[])
err = ErrorException("Unknown type of problem $(p.head)")
@test_throws err Convex.objective_vexity(p)
return
end
function test_optval_is_nothing_before_solve!()
x = Variable()
p = minimize(x, x >= 0)
@test p.optval === nothing
solve!(p, MOI.OptimizerWithAttributes(SCS.Optimizer, "verbose" => 0))
@test p.optval ≈ 0.0 atol = 1e-3
@test Convex.termination_status(p) == MOI.OPTIMAL
@test Convex.objective_value(p) ≈ 0.0 atol = 1e-3
return
end
function test_default_problem_type_is_Float64()
x = Variable()
p = minimize(x, x >= 0)
@test p isa Convex.Problem{Float64}
return
end
function test_set_value!_doesnt_convert_to_Float64()
x = Variable()
set_value!(x, big"1.0")
@test evaluate(x) isa BigFloat
x = Variable(2)
set_value!(x, big.([1.0, 2.0]))
@test evaluate(x) isa Vector{BigFloat}
x = Variable(2, 2)
set_value!(x, big.([1.0 2.0; 3.0 4.0]))
@test evaluate(x) isa Matrix{BigFloat}
return
end
function test_show()
x = Variable()
@test sprint(show, x) == """
Variable
size: (1, 1)
sign: real
vexity: affine
$(Convex.show_id(x))"""
fix!(x, 1.0)
@test sprint(show, x) == """
Variable
size: (1, 1)
sign: real
vexity: constant
$(Convex.show_id(x))
value: 1.0"""
@test sprint(show, 2 * x) == """
* (constant; real)
├─ $(reshape([2], 1, 1))
└─ real variable (fixed) ($(Convex.show_id(x)))"""
free!(x)
p = maximize(log(x), x >= 1, x <= 3)
@test monotonicity(p) == (Convex.Nonincreasing(),)
@test sign(p) == NoSign()
@test curvature(p) == Convex.ConvexVexity()
@test sprint(show, p) == """
maximize
└─ log (concave; real)
└─ real variable ($(Convex.show_id(x)))
subject to
├─ ≥ constraint (affine)
│ ├─ real variable ($(Convex.show_id(x)))
│ └─ $(reshape([1], 1, 1))
└─ ≤ constraint (affine)
├─ real variable ($(Convex.show_id(x)))
└─ $(reshape([3], 1, 1))
status: `solve!` not called yet"""
x = ComplexVariable(2, 3)
@test sprint(show, x) == """
Variable
size: (2, 3)
sign: complex
vexity: affine
$(Convex.show_id(x))"""
# test `MAXDEPTH`
# We construct a binary tree of depth >= 3
# to make sure it gets truncated appropriately.
x = Variable(2)
y = Variable(2)
level3 = hcat(x, y)
level2 = hcat(level3, level3)
root = hcat(level2, level2)
p = minimize(sum(x), root == root)
@test curvature(p) == Convex.ConstVexity()
@test sprint(show, p) == """
minimize
└─ sum (affine; real)
└─ 2-element real variable ($(Convex.show_id(x)))
subject to
└─ == constraint (affine)
├─ hcat (affine; real)
│ ├─ hcat (affine; real)
│ │ ├─ …
│ │ └─ …
│ └─ hcat (affine; real)
│ ├─ …
│ └─ …
└─ hcat (affine; real)
├─ hcat (affine; real)
│ ├─ …
│ └─ …
└─ hcat (affine; real)
├─ …
└─ …
status: `solve!` not called yet"""
# test `MAXWIDTH`
x = Variable()
p = satisfy([x == i for i in 1:100])
err = ErrorException("Satisfiability problem cannot be used as subproblem")
@test_throws err sign(p)
old_maxwidth = Convex.MAXWIDTH[]
Convex.MAXWIDTH[] = 2
@test sprint(show, p) == """
satisfy
└─ nothing
subject to
├─ == constraint (affine)
│ ├─ real variable ($(Convex.show_id(x)))
│ └─ $(reshape([1], 1, 1))
├─ == constraint (affine)
│ ├─ real variable ($(Convex.show_id(x)))
│ └─ $(reshape([2], 1, 1))
⋮
status: `solve!` not called yet"""
Convex.MAXWIDTH[] = old_maxwidth
# solved problem
x = Variable()
p = satisfy(x >= 0)
output = solve!(
p,
MOI.OptimizerWithAttributes(
SCS.Optimizer,
"verbose" => 0,
"eps_abs" => 1e-6,
),
)
@test sprint(show, p) == """
satisfy
└─ nothing
subject to
└─ ≥ constraint (affine)
├─ real variable ($(Convex.show_id(x)))
└─ $(reshape([0], 1, 1))
termination status: OPTIMAL
primal status: FEASIBLE_POINT
dual status: FEASIBLE_POINT"""
# test small `MAXDIGITS`
x = Variable()
old_maxdigits = Convex.MAXDIGITS[]
Convex.MAXDIGITS[] = 2
@test length(Convex.show_id(x)) == length("id: ") + 5
Convex.MAXDIGITS[] = old_maxdigits
# test large `MAXDIGITS`
x = Variable()
old_maxdigits = Convex.MAXDIGITS[]
Convex.MAXDIGITS[] = 100
@test length(Convex.show_id(x)) ==
length("id: ") + length(string(x.id_hash))
Convex.MAXDIGITS[] = old_maxdigits
return
end
function test_vartype_and_set_vartype()
for x in (Variable(), Variable(1))
@test vartype(x) == ContVar
vartype!(x, BinVar)
@test vartype(x) == BinVar
@test x.vartype == BinVar
vartype!(x, IntVar)
@test vartype(x) == IntVar
@test x.vartype == IntVar
vartype!(x, ContVar)
@test vartype(x) == ContVar
@test x.vartype == ContVar
end
return
end
function test_Constructors()
# Constructors with sign
for sgn in (Positive(), NoSign())
for x in [ # tuple size
Variable((2, 2), sgn),
Variable((2, 2), sgn, BinVar),
Variable((2, 2), sgn, :Bin),
# individual size
Variable(2, 2, sgn),
Variable(2, 2, sgn, BinVar),
Variable(2, 2, sgn, :Bin),
# single dimension
Variable(2, sgn),
Variable(2, sgn, BinVar),
Variable(2, sgn, :Bin),
# no dimension
Variable(sgn),
Variable(sgn, BinVar),
Variable(sgn, :Bin),
]
@test x isa Variable
@test x isa Convex.AbstractVariable
@test sign(x) == sgn
@test x.sign == sgn
end
end
# constructors without sign
for x in [ # tuple size
Variable((2, 2)),
Variable((2, 2), BinVar),
Variable((2, 2), :Bin),
# individual size
Variable(2, 2),
Variable(2, 2, BinVar),
Variable(2, 2, :Bin),
# single dimension
Variable(2),
Variable(2, BinVar),
Variable(2, :Bin),
# no dimension
Variable(),
Variable(BinVar),
Variable(:Bin),
]
@test x isa Variable
@test x isa Convex.AbstractVariable
@test sign(x) == NoSign()
@test x.sign == NoSign()
Convex.sign!(x, Positive())
@test sign(x) == Positive()
@test x.sign == Positive()
end
# ComplexVariable
for x in [ # tuple size
ComplexVariable((2, 2)),
Variable((2, 2), ComplexSign()),
ComplexVariable((2, 2), :Semidefinite),
# individual size
ComplexVariable(2, 2),
Variable(2, 2, ComplexSign()),
ComplexVariable(2, 2, :Semidefinite),
# single dimension
ComplexVariable(2),
Variable(2, ComplexSign()),
# no dimension
ComplexVariable(),
Variable(ComplexSign()),
]
@test x isa ComplexVariable
@test x isa Convex.AbstractVariable
@test sign(x) == ComplexSign()
end
for vt in (BinVar, IntVar),
V in (ComplexVariable, Semidefinite, HermitianSemidefinite)
@test_throws Any V(2; vartype = vt)
end
for vt in (:Bin, :Int),
V in (Semidefinite, HermitianSemidefinite, ComplexVariable)
@test_throws Any V(2, vt)
end
# Semidefinite
for x in [
Variable((2, 2), :Semidefinite),
Variable(2, 2, :Semidefinite),
ComplexVariable((2, 2), :Semidefinite),
ComplexVariable(2, 2, :Semidefinite),
HermitianSemidefinite((2, 2)),
HermitianSemidefinite(2, 2),
HermitianSemidefinite(2),
Semidefinite((2, 2)),
Semidefinite(2, 2),
Semidefinite(2),
]
@test length(get_constraints(x)) == 1
@test get_constraints(x)[] isa Convex.SDPConstraint
end
@test_throws ErrorException HermitianSemidefinite(2, 3)
@test_throws ErrorException Semidefinite(2, 3)
return
end
function test_length_and_size()
x = Variable(2, 3)
@test length(x) == 6
@test size(x) == (2, 3)
@test size(x, 1) == 2
@test size(x, 2) == 3
x = Variable(3)
@test length(x) == 3
@test size(x) == (3, 1)
x = Variable()
@test length(x) == 1
@test size(x) == (1, 1)
return
end
function test_lastindex_and_axes()
x = Variable(2, 3)
@test axes(x) == (Base.OneTo(2), Base.OneTo(3))
@test axes(x, 1) == Base.OneTo(2)
@test lastindex(x) == 6
@test lastindex(x, 2) == 3
y = x[:, end]
@test y isa Convex.AbstractExpr
@test size(y) == (2, 1)
return
end
function test_Cartesian_index()
x = Variable(3, 2)
set_value!(x, rand(3, 2))
context = Convex.Context{Float64}(() -> MOI.Utilities.Model{Float64}())
for ind in CartesianIndices(zeros(3, 2))
L = Convex.conic_form!(context, x[ind])
R = Convex.conic_form!(context, x[ind[1], ind[2]])
@test L == R
@test evaluate(x[ind]) == evaluate(x[ind[1], ind[2]])
end
y = [1.0 2 3; 4 5 6] * x
for ind in CartesianIndices(zeros(2, 2))
L = Convex.conic_form!(context, y[ind])
R = Convex.conic_form!(context, y[ind[1], ind[2]])
@test L == R
@test evaluate(y[ind]) == evaluate(y[ind[1], ind[2]])
end
return
end
function test_parametric_constants()
z = constant([1.0 0.0im; 0.0 1.0])
@test z isa Convex.ComplexConstant{Float64}
# Helper functions
@test Convex.ispos(1)
@test Convex.ispos(0)
@test !Convex.ispos(-1)
@test Convex.ispos([0, 1, 0])
@test !Convex.ispos([0, -1, 0])
@test Convex.isneg(-1)
@test Convex.isneg(0)
@test !Convex.isneg(1)
@test Convex.isneg([0, -1, 0])
@test !Convex.isneg([0, 1, 0])
@test Convex._size(3) == (1, 1)
@test Convex._sign(3) == Positive()
@test Convex._size([-1, 1, 1]) == (3, 1)
@test Convex._sign([-1, 1, 1]) == NoSign()
@test Convex._sign([-1, -1, -1]) == Negative()
@test Convex._size([0 0; 0 0]) == (2, 2)
@test Convex._sign([0 0; 0 0]) == Positive()
@test Convex._size(0 + 1im) == (1, 1)
@test Convex._sign(0 + 1im) == ComplexSign()
return
end
function test_issue_341_evaluate_for_constants()
A = rand(4, 4)
@test evaluate(constant(A)) ≈ copy(A)
@test constant(A).size == (4, 4)
b = rand(4)
@test evaluate(constant(b)) ≈ copy(b)
@test constant(b).size == (4, 1)
c = 1.0
@test evaluate(constant(c)) ≈ c
@test constant(c).size == (1, 1)
@test evaluate(sumlargesteigs(Variable(4, 4), 0)) == 0
@test evaluate(sumlargest(Variable(4), 0)) == 0
@test evaluate(sumsmallest(Variable(4), 0)) == 0
return
end
function test_Base_vect()
# Issue #223: ensure we can make vectors of variables
@test size([Variable(2), Variable(3, 4)]) == (2,)
return
end
function test_Iteration()
x = Variable(2, 3)
s = sum([xi for xi in x])
set_value!(x, [1 2 3; 4 5 6])
# evaluate(s) == [21] (which might be wrong? expected 21)
# but [21][1] === 21[1] === 21
# so this should pass even after "fixing" that
@test evaluate(s)[1] == 21
x = Variable(4)
@test [xi.inds for xi in x] == [1:1, 2:2, 3:3, 4:4]
x = Variable(0)
@test [xi for xi in x] == []
@test iterate(x) == nothing
return
end
# returns [21]; not sure why
# context("iteration") do
# x = Variable(2,3)
# s = sum([xi for xi in x])
# x.value = [1 2 3; 4 5 6]
# @fact evaluate(s) --> 21
# end
function test_DCP_warnings()
x = Variable()
y = Variable()
p = minimize(log(x) + square(y), x >= 0, y >= 0)
@test_throws DCPViolationError solve!(p, SCS.Optimizer)
str = sprint(Base.showerror, DCPViolationError())
@test contains(str, "Expression not DCP compliant")
p = minimize(sqrt(x), x >= 0, x <= 1)
@test_throws DCPViolationError solve!(p, SCS.Optimizer)
return
end
function test_add_constraints!_issue_380()
x = Variable(3, 3)
p = minimize(norm_1(x))
y = randn(3, 3)
c = (norm2(x - y) < 1)
@test length(p.constraints) == 0
add_constraint!(p, c)
@test length(p.constraints) == 1
empty!(p.constraints)
add_constraints!(p, c)
@test length(p.constraints) == 1
empty!(p.constraints)
add_constraint!(p, [c])
@test length(p.constraints) == 1
empty!(p.constraints)
c2 = (norm2(x - rand(3, 3)) < 3)
add_constraints!(p, [c, c2])
@test length(p.constraints) == 2
return
end
function test_diagm_issue_401()
x = Variable(3)
@test diagm(x) isa Convex.AbstractExpr
return
end
function test_is_psd()
_test_is_psd(Float64)
_test_is_psd(Float32)
_test_is_psd(Int)
_test_is_psd(ComplexF64)
_test_is_psd(BigFloat)
_test_is_psd(Rational{BigInt})
_test_is_psd(Complex{Rational{BigInt}})
return
end
function _test_is_psd(T)
A = zeros(T, 3, 3)
A[1, 1] = one(T)
@test Convex.is_psd(A)
@test Convex.is_psd(SparseArrays.sparse(A))
B = A .- one(T) / T(5000)
@test !Convex.is_psd(B)
@test !Convex.is_psd(SparseArrays.sparse(B))
# See https://github.com/jump-dev/Convex.jl/issues/452 for details
C = [
70.12718378756115 0.0 -70.12718378756115 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 103.46633673574595 -103.46633673574595 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
-70.12718378756115 -103.46633673574595 347.1870410466142 -103.46633673574595 0.0 -70.12718378756115 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 -103.46633673574595 103.46633673574595 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 103.46633673574595 -103.46633673574595 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 -70.12718378756115 0.0 -103.46633673574595 347.1870410466142 -103.46633673574595 0.0 -70.12718378756115 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 -103.46633673574595 103.46633673574595 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 103.46633673574595 -103.46633673574595 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 -70.12718378756115 0.0 -103.46633673574595 347.1870410466142 -103.46633673574595 0.0 -70.12718378756115 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -103.46633673574595 103.46633673574595 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 103.46633673574595 -103.46633673574595 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -70.12718378756115 0.0 -103.46633673574595 347.1870410466142 -103.46633673574595 0.0 -70.12718378756115 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -103.46633673574595 103.46633673574595 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 103.46633673574595 -103.46633673574595 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -70.12718378756115 0.0 -103.46633673574595 347.1870410466142 -103.46633673574595 0.0 -70.12718378756115 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -103.46633673574595 103.46633673574595 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 103.46633673574595 -103.46633673574595 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -70.12718378756115 0.0 -103.46633673574595 277.05985725905305 -103.46633673574595 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -103.46633673574595 103.46633673574595 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 70.12718378756115 0.0 -70.12718378756115 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 103.46633673574595 -103.46633673574595 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -70.12718378756115 -103.46633673574595 347.1870410466142 -103.46633673574595 0.0 -70.12718378756115 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -103.46633673574595 103.46633673574595 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 103.46633673574595 -103.46633673574595 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -70.12718378756115 0.0 -103.46633673574595 347.1870410466142 -103.46633673574595 0.0 -70.12718378756115 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -103.46633673574595 103.46633673574595 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 103.46633673574595 -103.46633673574595 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -70.12718378756115 0.0 -103.46633673574595 347.1870410466142 -103.46633673574595 0.0 -70.12718378756115 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -103.46633673574595 103.46633673574595 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 103.46633673574595 -103.46633673574595 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -70.12718378756115 0.0 -103.46633673574595 347.1870410466142 -103.46633673574595 0.0 -70.12718378756115 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -103.46633673574595 103.46633673574595 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 103.46633673574595 -103.46633673574595 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -70.12718378756115 0.0 -103.46633673574595 347.1870410466142 -103.46633673574595 0.0 -70.12718378756115 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -103.46633673574595 103.46633673574595 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 103.46633673574595 -103.46633673574595 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -70.12718378756115 0.0 -103.46633673574595 277.05985725905305 -103.46633673574595
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -103.46633673574595 103.46633673574595
]
@test Convex.is_psd(C)
@test Convex.is_psd(SparseArrays.sparse(C))
return
end
function test_assume_psd_option_for_quadform()
A = [-1 0; 0 1] # neither PSD nor negative semidefinite
x = Variable(2)
@test_throws ErrorException quadform(x, A) # default
@test_throws ErrorException quadform(x, A; assume_psd = false)
@test quadform(x, A; assume_psd = true) isa Convex.AbstractExpr
return
end
function test_logsumexp_stability()
v = Convex.constant([1000, 1000, 1000])
@test Convex.evaluate(Convex.logsumexp(v)) ≈ 1001.098612
return
end
function test_conv_issue_364()
n = 3
m = 11
h = rand(m)
x = rand(n)
hvar = Variable(m)
hvar.value = h
function _conv(h, x)
m = length(h)
n = length(x)
zero_pad_x(i) = 1 <= i <= n ? x[i] : 0
return [sum(h[j] * zero_pad_x(i - j + 1) for j in 1:m) for i in 1:m+n-1]
end
@test evaluate(conv(hvar, x)) ≈ _conv(h, x)
return
end
function test_conj_issue_416()
A = [1 1im; -1im 1]
X = ComplexVariable(2, 2)
p = minimize(real(tr(conj(X))), [X == A])
solve!(
p,
MOI.OptimizerWithAttributes(
SCS.Optimizer,
"verbose" => 1,
"eps_abs" => 1e-6,
),
)
@test evaluate(X) ≈ A atol = 1e-3
return
end
function test_logisticloss_issue_458()
x = Variable()
expr = logisticloss(x)
@test expr isa Convex.AbstractExpr
set_value!(x, 1.5)
@test evaluate(expr) ≈ log(1 + exp(1.5))
return
end
function test_dot_issue_508()
x = [1.0 + 1.0im]
y = [-1.0im]
@test dot(x, y) ≈ evaluate(dot(constant(x), y))
return
end
function _test_sparse_tape(T)
d_in = 5
variables = MOI.VariableIndex.(1:d_in)
input = rand(T, d_in)
A = SparseArrays.sprand(T, d_in, d_in, 0.1)
b = SparseArrays.sprand(T, d_in, 0.8)
A_init = copy(A)
b_init = copy(b)
op = Convex.SparseAffineOperation(A, b)
tape = Convex.SparseTape(op, variables)
collapsed_tape = Convex.SparseAffineOperation(tape)
@test collapsed_tape.matrix * input + collapsed_tape.vector ≈ A * input + b
op2 = Convex.SparseAffineOperation(
SparseArrays.sparse(one(T) * LinearAlgebra.I, d_in, d_in),
-b,
)
tape = Convex.add_operation(tape, op2)
collapsed_tape2 = Convex.SparseAffineOperation(tape)
@test collapsed_tape2.matrix * input + collapsed_tape2.vector ≈ A * input
op3 = Convex.SparseAffineOperation(ones(T, 1, d_in), [zero(T)])
tape = Convex.add_operation(tape, op3)
collapsed_tape3 = Convex.SparseAffineOperation(tape)
@test collapsed_tape3.matrix * input + collapsed_tape3.vector ≈
[sum(A * input)]
@test A_init ≈ A
@test b_init ≈ b
return
end
test_sparse_tape_Float64() = _test_sparse_tape(Float64)
test_sparse_tape_Float32() = _test_sparse_tape(Float32)
test_sparse_tape_BigFloat() = _test_sparse_tape(BigFloat)
module DictVectors
using Convex
# To make sure `Convex` isn't using field access on `AbstractVariable`'s
# we'll use a global dictionary to store information about each instance
# our of mock variable type, `DictVector`.
const global_cache = Dict{UInt64,Any}()
mutable struct DictVector{T} <: Convex.AbstractVariable
head::Symbol
id_hash::UInt64
size::Tuple{Int,Int}
function DictVector{T}(d) where {T}
this = new(:DictVector, 0, (d, 1))
this.id_hash = objectid(this)
global_cache[this.id_hash] = Dict(
:value => nothing,
:sign => T <: Complex ? ComplexSign() : NoSign(),
:vartype => ContVar,
:constraints => Constraint[],
:vexity => Convex.AffineVexity(),
)
return this
end
end
Convex.evaluate(x::DictVector) = global_cache[x.id_hash][:value]
function Convex.set_value!(x::DictVector, v::AbstractArray)
return global_cache[x.id_hash][:value] = v
end
function Convex.set_value!(x::DictVector, v::Number)
return global_cache[x.id_hash][:value] = v
end
Convex.vexity(x::DictVector) = global_cache[x.id_hash][:vexity]
function Convex.vexity!(x::DictVector, v::Convex.Vexity)
return global_cache[x.id_hash][:vexity] = v
end
Convex.sign(x::DictVector) = global_cache[x.id_hash][:sign]
Convex.sign!(x::DictVector, s::Convex.Sign) = global_cache[x.id_hash][:sign] = s
Convex.vartype(x::DictVector) = global_cache[x.id_hash][:vartype]
function Convex.vartype!(x::DictVector, s::Convex.VarType)
return global_cache[x.id_hash][:vartype] = s
end
Convex.get_constraints(x::DictVector) = global_cache[x.id_hash][:constraints]
function Convex.add_constraint!(x::DictVector, s::Convex.Constraint)
return push!(global_cache[x.id_hash][:constraints], s)
end
end # DictVector
function test_DictVectors()
# Let us solve a basic problem from `test_affine.jl`
x = DictVectors.DictVector{BigFloat}(1)
y = DictVectors.DictVector{BigFloat}(1)
p = minimize(x + y, [x >= 3, y >= 2])
@test vexity(p) == Convex.AffineVexity()
solve!(p, MOI.OptimizerWithAttributes(SCS.Optimizer, "verbose" => 0))
@test p.optval ≈ 5 atol = 1e-3
@test evaluate(x + y) ≈ 5 atol = 1e-3
add_constraint!(x, x >= 4)
solve!(p, MOI.OptimizerWithAttributes(SCS.Optimizer, "verbose" => 0))
@test p.optval ≈ 6 atol = 1e-3
@test evaluate(x + y) ≈ 6 atol = 1e-3
@test length(get_constraints(x)) == 1
return
end
module DensityMatricies
using Convex
mutable struct DensityMatrix{T} <: Convex.AbstractVariable
head::Symbol
id_hash::UInt64
size::Tuple{Int,Int}
value::Convex.ValueOrNothing
vexity::Convex.Vexity
function DensityMatrix(d)
this = new{ComplexF64}(
:DensityMatrix,
0,
(d, d),
nothing,
Convex.AffineVexity(),
)
this.id_hash = objectid(this)
return this
end
end
Convex.get_constraints(ρ::DensityMatrix) = [ρ ⪰ 0, tr(ρ) == 1]
Convex.sign(::DensityMatrix) = Convex.ComplexSign()
Convex.vartype(::DensityMatrix) = Convex.ContVar
end # DensityMatricies
function test_DensityMatrix()
X = randn(4, 4) + im * rand(4, 4)
X = X + X'
# `X` is Hermitian and non-degenerate (with probability 1)
# Let us calculate the projection onto the eigenspace associated
# to the maximum eigenvalue
e_vals, e_vecs = LinearAlgebra.eigen(LinearAlgebra.Hermitian(X))
e_val, idx = findmax(e_vals)
e_vec = e_vecs[:, idx]
proj = e_vec * e_vec'
# found it! Now let us do it again via an SDP
ρ = DensityMatricies.DensityMatrix(4)
prob = maximize(real(tr(ρ * X)))
solve!(prob, MOI.OptimizerWithAttributes(SCS.Optimizer, "verbose" => 0))
@test prob.optval ≈ e_val atol = 1e-3
@test evaluate(ρ) ≈ proj atol = 1e-3
ρ2 = real(ρ) + im * imag(ρ)
prob = maximize(real(tr(ρ2 * X)))
solve!(prob, MOI.OptimizerWithAttributes(SCS.Optimizer, "verbose" => 0))
@test prob.optval ≈ e_val atol = 1e-3
@test evaluate(ρ) ≈ proj atol = 1e-3
@test evaluate(ρ) ≈ evaluate(ρ2) atol = 1e-3
return
end
module ProbabilityVectors
using Convex
mutable struct ProbabilityVector <: Convex.AbstractVariable
head::Symbol
id_hash::UInt64
size::Tuple{Int,Int}
value::Convex.ValueOrNothing
vexity::Convex.Vexity
function ProbabilityVector(d)
this =
new(:ProbabilityVector, 0, (d, 1), nothing, Convex.AffineVexity())
this.id_hash = objectid(this)
return this
end
end
Convex.get_constraints(p::ProbabilityVector) = [sum(p) == 1]
Convex.sign(::ProbabilityVector) = Convex.Positive()
Convex.vartype(::ProbabilityVector) = Convex.ContVar
(p::ProbabilityVector)(x) = dot(p, x)
end # ProbabilityVectors
function test_ProbabilityVectors()
p = ProbabilityVectors.ProbabilityVector(3)
x = [1.0, 2.0, 3.0]
@test p(x) isa Convex.AbstractExpr
@test sign(p) == Positive()
prob = minimize(p(x))
solve!(prob, MOI.OptimizerWithAttributes(SCS.Optimizer, "verbose" => 0))
@test prob.optval ≈ 1.0 atol = 1e-3
@test evaluate(p(x)) ≈ 1.0 atol = 1e-3
@test evaluate(p) ≈ [1.0, 0.0, 0.0] atol = 1e-3
return
end
function test_write_to_file()
x = Variable(3)
p = minimize(logsumexp(x))
dir = mktempdir()
filename = joinpath(dir, "test.mof.json")
@test_throws ArgumentError write_to_file(p, filename)
solve!(p, SCS.Optimizer; silent_solver = true)
write_to_file(p, filename)
@test occursin("ExponentialCone", read(filename, String))
p_int = minimize(logsumexp(x); numeric_type = Int)
@test_throws MethodError write_to_file(p_int, filename)
return
end
end # TestUtilities
TestUtilities.runtests()