/
test_atoms.jl
2049 lines (1919 loc) · 53.7 KB
/
test_atoms.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
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 2014: Madeleine Udell and contributors
#
# Use of this source code is governed by a BSD-style license that can be found
# in the LICENSE file or at https://opensource.org/license/bsd-2-clause
module TestAtoms
using Convex
using Test
# Do not use `using LinearAlgebra` to check symbols are re-exported by Convex.
import LinearAlgebra
import MathOptInterface as MOI
function runtests()
for name in names(@__MODULE__; all = true)
if startswith("$name", "test_")
@testset "$name" begin
getfield(@__MODULE__, name)()
end
end
end
return
end
function _to_moi(x::MOI.AbstractVectorFunction)
if MOI.output_dimension(x) > 1
return x
end
return only(MOI.Utilities.scalarize(x))
end
_to_moi(x::Convex.SparseTape) = _to_moi(Convex.to_vaf(x))
_to_moi(v::MOI.AbstractScalarFunction) = v
"""
_test_atom(build_fn::Function, target_string::String; value_type = Float64)
The same arguments and behavior as `_test_reformulation`, but in addition, tests
a number of properties that all atoms must satisfy.
1. the atom is a subtype of `AbstractExpr`
2. the atom is mutable
3. `Convex.head` is implemented and prints a string
4. `Base.sign` is implemented and returns a `Convex.Sign` object
5. `Covnex.monotonicity` is implemented and returns a tuple of
`Convex.Monotonicity` objects, with one element for each child
6. `Convex.curvature` is implemented and returns a `Convex.Vexity` object
## Example
```julia
target = \"\"\"
variables: x
minobjective: 1.0 + 1.0 * x
\"\"\"
_test_atom(target) do context
return Variable() + 1
end
```
"""
function _test_atom(build_fn, target_string::String; value_type = Float64)
context = Convex.Context{value_type}(MOI.Utilities.Model{value_type})
atom = build_fn(context)
# All atoms must be an AbstractExpr
@test atom isa Convex.AbstractExpr
# All atoms must be mutable
@test ismutable(atom)
@test sprint(Convex.head, atom) isa String
@test Base.sign(atom) isa Convex.Sign
N = length(atom.children)
@test Convex.monotonicity(atom) isa NTuple{N,Convex.Monotonicity}
@test Convex.curvature(atom) isa Convex.Vexity
_test_reformulation(build_fn, target_string; value_type)
return
end
"""
_test_reformulation(
build_fn::Function,
target_string::String;
value_type = Float64,
)
Test the reformulation constructed by `build_fn(::Convex.Context)` produces an
optimization problem given by `target_string` when the result returned by
`build_fn` is set as the objective function.
## Arguments
* `build_fn`: a function called with a new `context::Convex.Context` object
that returns an expression for the objective and may optionally modify
`context` as well.
* `target_string`: the string representation of the model as needed by
`MOI.Utilities.loadfromstring!`.
* `value_type`: the numeric value type of the model.
## Example
```julia
target = \"\"\"
variables: x
minobjective: 1.0 + 1.0 * x
\"\"\"
_test_reformulation(target) do context
return Variable() + 1
end
```
"""
function _test_reformulation(
build_fn,
target_string::String;
value_type = Float64,
)
context = Convex.Context{value_type}(MOI.Utilities.Model{value_type})
atom = build_fn(context)
t = Convex.conic_form!(context, atom)
MOI.set(context.model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
obj = _to_moi(t)
@test prod(size(atom)) == MOI.output_dimension(obj)
MOI.set(context.model, MOI.ObjectiveFunction{typeof(obj)}(), obj)
target = MOI.Utilities.Model{value_type}()
MOI.Utilities.loadfromstring!(target, target_string)
# Use the same names for each model
for (x, y) in zip(
MOI.get(context.model, MOI.ListOfVariableIndices()),
MOI.get(target, MOI.ListOfVariableIndices()),
)
name = MOI.get(target, MOI.VariableName(), y)
MOI.set(context.model, MOI.VariableName(), x, name)
end
context_string = sprint(print, context.model)
target_string = sprint(print, target)
if context_string != target_string
@info "Target model\n$target_string"
@info "context.model\n$context_string"
end
@test context_string == target_string
return
end
### affine/AdditionAtom
function test_AdditionAtom()
target = """
variables: x
minobjective: 1.0 + 1.0 * x
"""
_test_atom(target) do context
x = Variable()
return x + 1
end
target = """
variables: x
minobjective: 2.0 + 1.0 * x
"""
_test_atom(target) do context
x = Variable()
return 2.0 + x
end
target = """
variables: x
minobjective: 2.0 + 3.0 * x
"""
_test_atom(target) do context
x = Variable()
return 2.0 + 3.0 * x
end
target = """
variables: x
minobjective: 0.0 + 5.0 * x
"""
_test_atom(target) do context
x = Variable()
return 2.0 * x + 3.0 * x
end
target = """
variables: x1, x2
minobjective: [2.0 + x1, 2.0 + x2]
"""
_test_atom(target) do context
x = Variable(2)
return x + 2
end
target = """
variables: x1, x2
minobjective: [2.0 + x1, 2.0 + x2]
"""
_test_atom(target) do context
x = Variable(2)
return 2 + x
end
target = """
variables: x
minobjective: 2.0 + 3.0 * x
"""
_test_atom(target) do context
x = Variable()
a = x + x
b = 2 + x
return a + b
end
target = """
variables: x1, x2, y
minobjective: [1.0 * x1 + 1.0 * y, 1.0 * x2 + 1.0 * y]
"""
_test_atom(target) do context
x = Variable(2)
y = Variable()
return x + y
end
target = """
variables: y, x1, x2
minobjective: [1.0 * y + 1.0 * x1, 1.0 * y + 1.0 * x2]
"""
_test_atom(target) do context
x = Variable(2)
y = Variable()
return y + x
end
return
end
function test_AdditionAtom_negate()
target = """
variables: x
minobjective: -1.0 + 1.0 * x
"""
_test_atom(target) do context
x = Variable()
return x - 1
end
target = """
variables: x
minobjective: 2.0 + -1.0 * x
"""
_test_atom(target) do context
x = Variable()
return 2.0 - x
end
target = """
variables: x
minobjective: 2.0 + -3.0 * x
"""
_test_atom(target) do context
x = Variable()
return 2.0 - 3.0 * x
end
target = """
variables: x
minobjective: 0.0 + -1.0 * x
"""
_test_atom(target) do context
x = Variable()
return 2.0 * x - 3.0 * x
end
return
end
function test_AdditionAtom_errors()
x = Variable(2, 2)
y = Variable(2, 3)
@test_throws(
ErrorException(
"[AdditionAtom] cannot add expressions of sizes $(x.size) and $(y.size)",
),
x + y,
)
return
end
### affine/ConjugateAtom
function test_ConjugateAtom()
x = Variable()
y = constant(2.0)
@test isequal(conj(x), x)
@test isequal(conj(y), y)
z = conj(im * x)
@test z isa Convex.ConjugateAtom
@test sprint(Convex.head, z) == "conj"
@test Base.sign(z) == Convex.ComplexSign()
@test Convex.monotonicity(z) == (Convex.Nondecreasing(),)
@test Convex.curvature(z) == Convex.ConstVexity()
@test Convex.evaluate(conj(Convex.ComplexConstant(y, y))) == 2.0 - 2.0im
x.value = [-2.0]
@test evaluate(conj(x)) ≈ -2.0
@test evaluate(conj(im * x)) ≈ 0 + 2.0im
return
end
### affine/DiagAtom
function test_DiagAtom()
target = """
variables: x11, x21, x21, x22
minobjective: [1.0 * x11, 1.0 * x22]
"""
_test_atom(target) do context
return diag(Variable(2, 2))
end
target = """
variables: x11, x21, x12
minobjective: 0.0 + 1.0 * x12
"""
_test_atom(target) do context
return diag(Variable(2, 2), 1)
end
target = """
variables: x11, x21
minobjective: 1.0 * x21
"""
_test_atom(target) do context
return diag(Variable(2, 2), -1)
end
@test_throws(
ErrorException(
"[DiagAtom] bounds error in calling diag. Got 3 but it must be in -2..2",
),
diag(Variable(2, 2), 3),
)
@test_throws(
ErrorException(
"[DiagAtom] bounds error in calling diag. Got -5 but it must be in -3..3",
),
diag(Variable(3, 4), -5),
)
return
end
### affine/DiagMatrixAtom
function test_DiagMatrixAtom()
target = """
variables: x, y
minobjective: [1.0 * x, 0.0, 0.0, 1.0 * y]
"""
_test_atom(target) do context
return diagm(Variable(2))
end
_test_atom(target) do context
return diagm(0 => Variable(2))
end
_test_atom(target) do context
return Diagonal(Variable(2))
end
_test_atom(target) do context
return Diagonal(Variable(1, 2))
end
_test_atom(target) do context
return Diagonal(Variable(2, 1))
end
@test_throws(
ArgumentError(
"[DiagMatrixAtom] only vectors are allowed for `diagm(x)` and `Diagonal(x). Did you mean to use `diag(x, 0)`?",
),
diagm(Variable(2, 2)),
)
@test_throws(
ArgumentError(
"[DiagMatrixAtom] only the main diagonal is supported. Got `d=1`",
),
diagm(1 => Variable(2)),
)
return
end
### affine/HcatAtom
function test_HcatAtom()
target = """
variables: x
minobjective: [1.0 * x, 1.0 * x]
"""
_test_atom(target) do context
x = Variable()
return hcat(x, x)
end
_test_atom(target) do context
x = Variable()
return vcat(x, x)
end
target = """
variables: x1, x2
minobjective: [1.0 * x1, 1.0 * x2, 2.0]
"""
_test_atom(target) do context
x = Variable(2)
y = constant(2)
return vcat(x, y)
end
_test_atom(target) do context
x = Variable(2)
return vcat(x, 2)
end
_test_atom(target) do context
x = Variable(1, 2)
return hcat(x, 2)
end
@test_throws(
DimensionMismatch(
"[HcatAtom] cannot stack expressions of incompatible size. Got 1 expected 2.",
),
hcat(Variable(2), constant(2)),
)
@test_throws(
DimensionMismatch(
"[HcatAtom] cannot stack expressions of incompatible size. Got 2 expected 1.",
),
vcat(Variable(2, 1), Variable(1, 2)),
)
return
end
### affine/ImaginaryAtom
function test_ImaginaryAtom()
target = """
variables: x
minobjective: 1.0 * x
"""
_test_atom(target) do context
return imag(im * Variable())
end
target = """
variables: x
minobjective: 1.0 * x + 3.0
"""
_test_atom(target) do context
y = constant(2 + 3im)
return Variable() + imag(y)
end
target = """
variables: x
minobjective: 1.0 * x
"""
_test_atom(target) do context
y = constant(2)
return Variable() + imag(y)
end
return
end
### affine/IndexAtom
function test_IndexAtom()
target = """
variables: x1, x2
minobjective: [1.0 * x1, 1.0 * x2]
"""
_test_atom(target) do context
return Variable(2)[:, 1]
end
_test_atom(target) do context
return Variable(2)[:, :]
end
target = """
variables: x1, x2, x3
minobjective: [1.0 * x1, 1.0 * x3]
"""
_test_atom(target) do context
return Variable(3)[[1, 3]]
end
target = """
variables: x1, x2, x3
minobjective: [1.0 * x1, 1.0 * x3]
"""
_test_atom(target) do context
return Variable(2, 2)[1, :]
end
target = """
variables: x1, x2, x3, x4
minobjective: [1.0 * x2, 1.0 * x4]
"""
_test_atom(target) do context
return Variable(2, 2)[2, :]
end
_test_atom(target) do context
return Variable(2, 2)[2:2, :]
end
target = """
variables: x1, x2
minobjective: [1.0 * x1, 1.0 * x2]
"""
_test_atom(target) do context
return Variable(2, 2)[:, 1]
end
target = """
variables: x1, x2, x3, x4
minobjective: [1.0 * x3, 1.0 * x4]
"""
_test_atom(target) do context
return Variable(2, 2)[:, 2]
end
y = [true, false, true]
x = Variable(3)
@test string(x[y]) == string([x[1], x[3]])
return
end
### affine/MultiplyAtom
function test_MultiplyAtom()
target = """
variables: x
minobjective: 2.0 * x
"""
_test_atom(target) do context
return 2 * Variable()
end
_test_atom(target) do context
return Variable() * 2
end
target = """
variables: x, y
minobjective: [2.0 * x, 2.0 * y]
"""
_test_atom(target) do context
return 2 * Variable(2)
end
_test_atom(target) do context
return Variable(2) * 2
end
target = """
variables: x11, x21, x12, x22
minobjective: [2.0 * x11 + 3.0 * x12, 2.0 * x21 + 3.0 * x22]
"""
_test_atom(target) do context
return Variable(2, 2) * [2, 3]
end
target = """
variables: x11, x21, x12, x22
minobjective: [2.0 * x11 + 3.0 * x21, 2.0 * x12 + 3.0 * x22]
"""
_test_atom(target) do context
return [2, 3]' * Variable(2, 2)
end
target = """
variables: t, x
minobjective: 1.0 * t
[t, 0.5, x] in RotatedSecondOrderCone(3)
"""
_test_atom(target) do context
x = Variable()
return x * x
end
target = """
variables: x
minobjective: 0.5 * x
"""
_test_atom(target) do context
return Variable() / 2
end
target = """
variables: x1, x2
minobjective: [0.25 * x1, 0.25 * x2]
"""
_test_atom(target) do context
return Variable(2) / 4
end
_test_atom(target) do context
return 0.25 .* Variable(2)
end
_test_atom(target) do context
return Variable(2) .* 0.25
end
_test_atom(target) do context
return Variable(2) ./ 4
end
target = """
variables: x1, x2
minobjective: [0.5 * x1, 0.25 * x2]
"""
_test_atom(target) do context
return Variable(2) ./ [2, 4]
end
@test_throws(
ErrorException(
"[MultiplyAtom] cannot multiply two expressions of sizes (2, 2) and (3, 3)",
),
Variable(2, 2) * Variable(3, 3)
)
@test_throws(
ErrorException(
"[MultiplyAtom] multiplication of two non-constant expressions is not DCP compliant",
),
_test_atom(_ -> Variable(2)' * Variable(2), ""),
)
@test_throws(
ErrorException(
"[MultiplyAtom] multiplication of two non-constant expressions is not DCP compliant",
),
_test_atom(_ -> Variable(2) .* Variable(2), ""),
)
@test_throws(
ErrorException(
"[MultiplyAtom] multiplication of two non-constant expressions is not DCP compliant",
),
_test_atom(_ -> Variable() * Variable(), ""),
)
return
end
### affine/NegateAtom
function test_NegateAtom()
target = """
variables: x
minobjective: -1.0 + -1.0 * x
"""
_test_atom(target) do context
return -(1 + Variable())
end
target = """
variables: x
minobjective: 1.0 * x + -1.0
"""
_test_atom(target) do context
return Variable() + -constant(1.0)
end
return
end
### affine/RealAtom
function test_RealAtom()
target = """
variables: x
minobjective: 1.0 * x
"""
_test_atom(target) do context
return real(Variable())
end
_test_atom(target) do context
return real(Variable() + im * Variable())
end
target = """
variables: x
minobjective: 1.0 * x + 2.0
"""
_test_atom(target) do context
y = constant(2 + 3im)
return Variable() + real(y)
end
target = """
variables: x
minobjective: 1.0 * x + 2.0
"""
_test_atom(target) do context
y = constant(2)
return Variable() + real(y)
end
return
end
### affine/ReshapeAtom
function test_ReshapeAtom()
target = """
variables: x1, x2, x3, x4
minobjective: [1.0 * x1, 1.0 * x2, 1.0 * x3, 1.0 * x4]
"""
_test_atom(target) do context
return reshape(Variable(4), 2, 2)
end
target = """
variables: x1, x2, x3, x4
minobjective: [1.0 * x1 + 2.0 * x3, 1.0 * x2 + 2.0 * x4]
[-1.0 + x1, -2.0 + x2, -3.0 + x3, -4.0 + x4] in Nonnegatives(4)
"""
_test_atom(target) do context
x = Variable(4)
add_constraint!(context, x - [1, 2, 3, 4] >= 0)
return reshape(x, 2, 2) * [1, 2]
end
@test_throws(
ErrorException(
"[ReshapeAtom] cannot reshape expression of size (4, 1) to (2, 3)",
),
reshape(Variable(4), 2, 3),
)
x = Variable(4)
x.value = [1, 2, 3, 4]
atom = reshape(x, 2, 2)
@test evaluate(atom) == [1 3; 2 4]
x = Variable()
x.value = 2
atom = reshape(x, 1, 1)
@test evaluate(atom) == 2
return
end
### affine/SumAtom
function test_SumAtom()
target = """
variables: x1, x2
minobjective: 1.0 * x1 + 1.0 * x2
"""
_test_atom(target) do context
return sum(Variable(2))
end
target = """
variables: x1, x2, x3, x4
minobjective: 1.0 * x1 + 1.0 * x2 + 1.0 * x3 + 1.0 * x4
"""
_test_atom(target) do context
return sum(Variable(2, 2))
end
target = """
variables: x1, x2, x3, x4
minobjective: [1.0 * x1 + 1.0 * x2, 1.0 * x3 + 1.0 * x4]
"""
_test_atom(target) do context
return sum(Variable(2, 2); dims = 1)
end
target = """
variables: x1, x2, x3, x4
minobjective: [1.0 * x1 + 1.0 * x3, 1.0 * x2 + 1.0 * x4]
"""
_test_atom(target) do context
return sum(Variable(2, 2); dims = 2)
end
@test_throws(
ErrorException("[SumAtom] sum not implemented for `dims=3`"),
sum(Variable(2, 2); dims = 3),
)
return
end
### exp_+_sdp_cone/LogDetAtom
function test_LogDetAtom()
target = """
variables: t, x11, x12, x21, x22
minobjective: 1.0 * t + 0.0
[1.0*t, 1.0, 1.0*x11, 1.0 *x12, 1.0*x21, 1.0*x22] in LogDetConeSquare(2)
"""
_test_atom(target) do context
return logdet(Variable(2, 2))
end
return
end
### exp_cone/EntropyAtom
function test_EntropyAtom()
target = """
variables: t1, t2, x1, x2
minobjective: 1.0 * t1 + 1.0 * t2
[1.0 * t1, 1.0 * x1, 1.0] in ExponentialCone()
[1.0 * t2, 1.0 * x2, 1.0] in ExponentialCone()
"""
_test_atom(target) do context
return entropy(Variable(2))
end
@test_throws(
ErrorException(
"[EntropyAtom] the argument should be real but it's instead complex",
),
entropy(im * Variable(2)),
)
x = Variable(2)
atom = entropy(x)
x.value = [1.0 2.0]
@test evaluate(atom) ≈ -sum(xi * log(xi) for xi in x.value)
return
end
function test_EntropyAtom_elementwise()
target = """
variables: t1, t2, x1, x2
minobjective: [1.0 * t1, + 1.0 * t2]
[1.0 * t1, 1.0 * x1, 1.0] in ExponentialCone()
[1.0 * t2, 1.0 * x2, 1.0] in ExponentialCone()
"""
_test_atom(target) do context
return entropy_elementwise(Variable(2))
end
x = Variable(2)
atom = entropy_elementwise(x)
x.value = [1.0, 2.0]
@test evaluate(atom) ≈ -x.value .* log.(x.value)
return
end
### exp_cone/ExpAtom
function test_ExpAtom()
target = """
variables: x, z
minobjective: 1.0 * z + 0.0
[1.0 * x, 1.0, 1.0 * z] in ExponentialCone()
"""
_test_atom(target) do context
return exp(Variable())
end
target = """
variables: x1, x2, z1, z2
minobjective: [1.0 * z1, 1.0 * z2]
[1.0 * x1, 1.0, 1.0 * z1] in ExponentialCone()
[1.0 * x2, 1.0, 1.0 * z2] in ExponentialCone()
"""
_test_atom(target) do context
return exp(Variable(2))
end
@test_throws(
ErrorException(
"[ExpAtom] the argument should be real but it's instead complex",
),
exp(im * Variable()),
)
x = Variable(2)
atom = exp(x)
x.value = [1.0, 2.0]
@test evaluate(atom) ≈ exp.([1.0, 2.0])
return
end
### exp_cone/LogAtom
function test_LogAtom()
target = """
variables: x, z
minobjective: 1.0 * x + 0.0
[1.0 * x, 1.0, 1.0 * z] in ExponentialCone()
"""
_test_atom(target) do context
return log(Variable())
end
target = """
variables: x1, x2, z1, z2
minobjective: [1.0 * x1, 1.0 * x2]
[1.0 * x1, 1.0, 1.0 * z1] in ExponentialCone()
[1.0 * x2, 1.0, 1.0 * z2] in ExponentialCone()
"""
_test_atom(target) do context
return log(Variable(2))
end
@test_throws(
ErrorException(
"[LogAtom] the argument should be real but it's instead complex",
),
log(im * Variable()),
)
x = Variable(2)
atom = log(x)
x.value = [1.0, 2.0]
@test evaluate(atom) ≈ log.([1.0, 2.0])
return
end
### exp_cone/LogSumExp
function test_LogSumExpAtom()
target = """
variables: x1, x2, t, z1, z2
minobjective: 1.0 * t
[1.0 * x1 + -1.0 * t, 1.0, 1.0 * z1] in ExponentialCone()
[1.0 * x2 + -1.0 * t, 1.0, 1.0 * z2] in ExponentialCone()
[1.0 + -1.0*z1 + -1.0*z2] in Nonnegatives(1)
"""
_test_atom(target) do context
return logsumexp(Variable(2))
end
target = """
variables: x1, t, z1, z2
minobjective: 1.0 * t
[1.0 * x1 + -1.0 * t, 1.0, 1.0 * z1] in ExponentialCone()
[-1.0 * t, 1.0, 1.0 * z2] in ExponentialCone()
[1.0 + -1.0*z1 + -1.0*z2] in Nonnegatives(1)
"""
_test_atom(target) do context
return logisticloss(Variable())
end
target = """
variables: x1, x1_, t, z1, z2, t_, z1_, z2_
minobjective: 1.0 * t + 1.0 * t_
[1.0 * x1 + -1.0 * t, 1.0, 1.0 * z1] in ExponentialCone()
[-1.0 * t, 1.0, 1.0 * z2] in ExponentialCone()
[1.0 * x1_ + -1.0 * t_, 1.0, 1.0 * z1_] in ExponentialCone()
[-1.0 * t_, 1.0, 1.0 * z2_] in ExponentialCone()
[1.0 + -1.0*z1 + -1.0*z2] in Nonnegatives(1)
[1.0 + -1.0*z1_ + -1.0*z2_] in Nonnegatives(1)
"""
_test_atom(target) do context
return logisticloss(Variable(2))
end
@test_throws(
ErrorException(
"[LogSumExpAtom] the argument should be real but it's instead complex",
),
logsumexp(im * Variable()),
)
x = Variable(2)
atom = logsumexp(x)
x.value = [1.0 1_000.0]
@test evaluate(atom) ≈ 1_000.0
return
end
### exp_cone/RelativeEntropyAtom
function test_RelativeEntropyAtom()
target = """
variables: w1, w2, v1, v2, u
minobjective: 1.0 * u + 0.0
[1.0*u, 1.0*v1, 1.0*v2, 1.0*w1, 1.0*w2] in RelativeEntropyCone(5)
"""
_test_atom(target) do context
x = Variable(2)
y = Variable(2)
return relative_entropy(x, y)
end
target = """
variables: w1, w2, v1, v2, u
minobjective: -1.0 * u + 1.0
[1.0*u, 1.0*v1, 1.0*v2, 1.0*w1, 1.0*w2] in RelativeEntropyCone(5)
"""
_test_atom(target) do context
x = Variable(2)
y = Variable(2)
return 1.0 + log_perspective(x, y)
end
x, y = Variable(2), im * Variable(2)
@test_throws(
ErrorException(
"[RelativeEntropyAtom] both the arguments should be real but these are instead $(sign(x)) and $(sign(y))",
),
relative_entropy(x, y),
)
@test_throws(
ErrorException(
"[RelativeEntropyAtom] both the arguments should be real but these are instead $(sign(y)) and $(sign(x))",
),
relative_entropy(y, x),
)
x = Variable(2)
y = Variable(2)
atom = relative_entropy(x, y)
x.value = [1.0, 1_000.0]
y.value = [1.0, NaN]
@test evaluate(atom) ≈ Inf
x.value = [0.0, 1.0]
y.value = [1.0, 2.0]
@test evaluate(atom) ≈ log(0.5)
x.value = [5.0, 1.0]
y.value = [3.0, 2.0]
@test evaluate(atom) ≈ 5 * log(5 / 3) + log(0.5)
return
end
### lp_cone/AbsAtom
function test_AbsAtom()
target = """
variables: t, x
minobjective: 1.0 * t
[1.0 * t + -1.0 * x] in Nonnegatives(1)
[1.0 * t + 1.0 * x] in Nonnegatives(1)
"""
_test_atom(target) do context
return abs(Variable())
end
target = """
variables: w, t, x
minobjective: 1.0 * w
[1.0 * t + -1.0 * x] in Nonnegatives(1)
[1.0 * t + 1.0 * x] in Nonnegatives(1)
[w, 0.5, t] in RotatedSecondOrderCone(3)
"""
_test_atom(target) do context
return abs2(Variable())
end
target = """
variables: t1, t2, x1, x2, w1, w2
minobjective: [1.0 * t1, 1.0*t2]
[1.0 * t1 + -1.0 * x1] in Nonnegatives(1)
[1.0 * t2 + -1.0 * w2] in Nonnegatives(1)
[1.0 * x1, 1.0 * x2, 2.0] in SecondOrderCone(3)
[1.0 * w2, 1.0 * w1, 2.0] in SecondOrderCone(3)
"""
_test_atom(target) do context
return abs(Variable(2) + 2im)
end
return
end
### lp_cone/DotSortAtom
function test_DotSortAtom()
target = """
variables: v1, v2, u1, u2, x1, x2
minobjective: 1.0 * u1 + u2 + v1 + v2
[v1+u1, v1+u2, v2+u1+-1.0*x1, v2+u2+-1.0*x2] in Nonnegatives(4)
"""
_test_atom(target) do context