forked from jump-dev/JuMP.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_variable.jl
1442 lines (1332 loc) · 42.8 KB
/
test_variable.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 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
#############################################################################
# test/variable.jl
# Testing for VariableRef
#############################################################################
module TestVariable
using JuMP
using Test
import LinearAlgebra
include(joinpath(@__DIR__, "utilities.jl"))
function _test_variable_name_util(variable, s_name)
@test s_name == @inferred name(variable)
@test variable == variable_by_name(owner_model(variable), s_name)
return
end
# Slices three-dimensional DenseAxisArray x[I,J,K]
# I,J,K can be singletons, ranges, colons, etc.
function _sliceof_util(VariableRefType, x, I, J, K)
y = Array{VariableRefType}(undef, length(I), length(J), length(K))
ii = 1
for i in I
jj = 1
for j in J
kk = 1
for k in K
y[ii, jj, kk] = x[i, j, k]
kk += 1
end
jj += 1
end
ii += 1
end
idx = [length(I) == 1, length(J) == 1, length(K) == 1]
return dropdims(y; dims = tuple(findall(idx)...))
end
function test_extension_variable_no_bound(
ModelType = Model,
VariableRefType = VariableRef,
)
model = ModelType()
@variable(model, nobounds)
@test !has_lower_bound(nobounds)
@test !has_upper_bound(nobounds)
@test !is_fixed(nobounds)
_test_variable_name_util(nobounds, "nobounds")
@test zero(nobounds) isa GenericAffExpr{Float64,VariableRefType}
@test one(nobounds) isa GenericAffExpr{Float64,VariableRefType}
return
end
function test_extension_variable_lower_bound_rhs(
ModelType = Model,
VariableRefType = VariableRef,
)
model = ModelType()
@variable(model, lbonly >= 0, Bin)
@test has_lower_bound(lbonly)
@test 0.0 == @inferred lower_bound(lbonly)
@test !has_upper_bound(lbonly)
@test !is_fixed(lbonly)
@test is_binary(lbonly)
@test !is_integer(lbonly)
@test isequal(model[:lbonly], lbonly)
delete_lower_bound(lbonly)
@test !has_lower_bound(lbonly)
# Name already used
@test_throws ErrorException @variable(model, lbonly)
return
end
function test_extension_variable_lower_bound_lhs(
ModelType = Model,
VariableRefType = VariableRef,
)
model = ModelType()
@variable(model, 0 <= lblhs, Bin)
@test has_lower_bound(lblhs)
@test 0.0 == @inferred lower_bound(lblhs)
@test !has_upper_bound(lblhs)
@test !is_fixed(lblhs)
@test is_binary(lblhs)
@test !is_integer(lblhs)
@test isequal(model[:lblhs], lblhs)
return
end
function test_extension_variable_upper_bound_rhs(
ModelType = Model,
VariableRefType = VariableRef,
)
model = ModelType()
@variable(model, ubonly <= 1, Int)
@test !has_lower_bound(ubonly)
@test has_upper_bound(ubonly)
@test 1.0 == @inferred upper_bound(ubonly)
@test !is_fixed(ubonly)
@test !is_binary(ubonly)
@test is_integer(ubonly)
@test isequal(model[:ubonly], ubonly)
delete_upper_bound(ubonly)
@test !has_upper_bound(ubonly)
return
end
function test_extension_variable_upper_bound_lhs(
ModelType = Model,
VariableRefType = VariableRef,
)
model = ModelType()
@variable(model, 1 >= ublhs, Int)
@test !has_lower_bound(ublhs)
@test has_upper_bound(ublhs)
@test 1.0 == @inferred upper_bound(ublhs)
@test !is_fixed(ublhs)
@test !is_binary(ublhs)
@test is_integer(ublhs)
@test isequal(model[:ublhs], ublhs)
return
end
function _has_bounds(var, lb, ub)
@test has_lower_bound(var)
@test lb == @inferred lower_bound(var)
@test has_upper_bound(var)
@test ub == @inferred upper_bound(var)
@test !is_fixed(var)
return
end
function test_extension_variable_interval(
ModelType = Model,
VariableRefType = VariableRef,
)
model = ModelType()
@variable(model, 0 <= bothb1 <= 1)
_has_bounds(bothb1, 0.0, 1.0)
@variable(model, 0 ≤ bothb2 ≤ 1)
_has_bounds(bothb2, 0.0, 1.0)
@variable(model, 1 >= bothb3 >= 0)
_has_bounds(bothb3, 0.0, 1.0)
@variable(model, 1 ≥ bothb4 ≥ 0)
_has_bounds(bothb4, 0.0, 1.0)
@test_macro_throws ErrorException @variable(model, 1 ≥ bothb5 ≤ 0)
@test_macro_throws ErrorException @variable(model, 1 ≤ bothb6 ≥ 0)
return
end
function test_extension_variable_fix(
ModelType = Model,
VariableRefType = VariableRef,
)
model = ModelType()
@variable(model, fixed == 1.0)
@test !has_lower_bound(fixed)
@test !has_upper_bound(fixed)
@test is_fixed(fixed)
@test 1.0 == @inferred fix_value(fixed)
unfix(fixed)
@test !is_fixed(fixed)
set_lower_bound(fixed, 0.0)
@test_throws Exception fix(fixed, 1.0)
fix(fixed, 1.0; force = true)
@test !has_lower_bound(fixed)
@test !has_upper_bound(fixed)
@test is_fixed(fixed)
@test 1.0 == @inferred fix_value(fixed)
return
end
function test_extension_variable_custom_index_sets(
ModelType = Model,
VariableRefType = VariableRef,
)
model = ModelType()
@variable(model, onerangeub[-7:1] <= 10, Int)
@variable(model, manyrangelb[0:1, 10:20, 1:1] >= 2)
@test has_lower_bound(manyrangelb[0, 15, 1])
@test 2 == @inferred lower_bound(manyrangelb[0, 15, 1])
@test !has_upper_bound(manyrangelb[0, 15, 1])
s = ["Green", "Blue"]
@variable(model, x[i = -10:10, s] <= 5.5, Int, start = i + 1)
@test 5.5 == @inferred upper_bound(x[-4, "Green"])
_test_variable_name_util(x[-10, "Green"], "x[-10,Green]")
@test start_value(x[-3, "Blue"]) == -2
@test isequal(model[:onerangeub][-7], onerangeub[-7])
@test_throws KeyError model[:foo]
return
end
function test_extension_variable_anonymous(
ModelType = Model,
VariableRefType = VariableRef,
)
model = ModelType()
@test_throws ErrorException @variable(model, [(0, 0)]) # #922
x = @variable(model, [(0, 2)])
@test "" == @inferred name(x[0])
@test "" == @inferred name(x[2])
return
end
function test_extension_variable_is_valid_delete(
ModelType = Model,
VariableRefType = VariableRef,
)
model = ModelType()
@variable(model, x)
@test is_valid(model, x)
delete(model, x)
@test !is_valid(model, x)
second_model = ModelType()
@test_throws Exception delete(second_model, x)
return
end
function test_extension_variable_bounds_set_get(
ModelType = Model,
VariableRefType = VariableRef,
)
model = ModelType()
@variable(model, 0 <= x <= 2)
@test 0 == @inferred lower_bound(x)
@test 2 == @inferred upper_bound(x)
set_lower_bound(x, 1)
@test 1 == @inferred lower_bound(x)
set_upper_bound(x, 3)
@test 3 == @inferred upper_bound(x)
@variable(model, q, Bin)
@test !has_lower_bound(q)
@test !has_upper_bound(q)
@variable(model, 0 <= y <= 1, Bin)
@test 0 == @inferred lower_bound(y)
@test 1 == @inferred upper_bound(y)
@variable(model, fixedvar == 2)
@test 2.0 == @inferred fix_value(fixedvar)
fix(fixedvar, 5)
@test 5 == @inferred fix_value(fixedvar)
@test_throws Exception lower_bound(fixedvar)
@test_throws Exception upper_bound(fixedvar)
return
end
function test_extension_variable_starts_set_get(
ModelType = Model,
VariableRefType = VariableRef,
)
model = ModelType()
@variable(model, x[1:3])
x0 = collect(1:3)
set_start_value.(x, x0)
@test start_value.(x) == x0
@test start_value.([x[1], x[2], x[3]]) == x0
@test has_start_value(x[1]) == true
@variable(model, y[1:3, 1:2])
@test has_start_value(y[1, 1]) == false
@test_throws DimensionMismatch set_start_value.(y, collect(1:6))
return
end
function test_extension_variable_integrality_set_get(
ModelType = Model,
VariableRefType = VariableRef,
)
model = ModelType()
@variable(model, x[1:3])
set_integer(x[2])
set_integer(x[2]) # test duplicated call
@test is_integer(x[2])
unset_integer(x[2])
@test !is_integer(x[2])
set_binary(x[1])
set_binary(x[1]) # test duplicated call
@test is_binary(x[1])
@test_throws Exception set_integer(x[1])
unset_binary(x[1])
@test !is_binary(x[1])
@variable(model, y, binary = true)
@test is_binary(y)
@test_throws Exception set_integer(y)
unset_binary(y)
@test !is_binary(y)
@variable(model, z, integer = true)
@test is_integer(z)
@test_throws Exception set_binary(z)
unset_integer(z)
@test !is_integer(z)
return
end
function test_extension_variable_repeated_elements(
ModelType = Model,
VariableRefType = VariableRef,
)
# Tests repeated elements in index set throw error (JuMP issue #199).
model = ModelType()
index_set = [:x, :x, :y]
@test_throws ErrorException @variable(
model,
unused_variable[index_set],
container = DenseAxisArray
)
@test_throws ErrorException @variable(
model,
unused_variable[index_set],
container = SparseAxisArray
)
@test_throws ErrorException @variable(
model,
unused_variable[index_set, [1]],
container = DenseAxisArray
)
@test_throws ErrorException @variable(
model,
unused_variable[index_set, [1]],
container = SparseAxisArray
)
return
end
function test_extension_variable_oneto_index_set(
ModelType = Model,
VariableRefType = VariableRef,
)
# Tests that Base.OneTo can be used in index set (JuMP issue #933).
model = ModelType()
auto_var = @variable(model, [Base.OneTo(3), 1:2], container = Auto)
@test auto_var isa Matrix{VariableRefType}
@test (3, 2) == @inferred size(auto_var)
array_var = @variable(model, [Base.OneTo(3), 1:2], container = Array)
@test array_var isa Matrix{VariableRefType}
@test (3, 2) == @inferred size(array_var)
denseaxisarray_var =
@variable(model, [Base.OneTo(3), 1:2], container = DenseAxisArray)
@test denseaxisarray_var isa Containers.DenseAxisArray{VariableRefType}
@test length.(axes(denseaxisarray_var)) == (3, 2)
return
end
function test_extension_variable_base_name_in_macro(
ModelType = Model,
VariableRefType = VariableRef,
)
model = ModelType()
@variable(model, normal_var)
_test_variable_name_util(normal_var, "normal_var")
no_indices = @variable(model, base_name = "foo")
_test_variable_name_util(no_indices, "foo")
# Note that `z` will be ignored in name.
indices = @variable(model, z[i = 2:3], base_name = "t")
_test_variable_name_util(indices[2], "t[2]")
_test_variable_name_util(indices[3], "t[3]")
return
end
function test_extension_variable_name(
ModelType = Model,
VariableRefType = VariableRef,
)
model = ModelType()
@variable(model, x)
_test_variable_name_util(x, "x")
set_name(x, "y")
@test variable_by_name(model, "x") isa Nothing
_test_variable_name_util(x, "y")
y = @variable(model, base_name = "y")
err(name) = ErrorException("Multiple variables have the name $name.")
@test_throws err("y") variable_by_name(model, "y")
set_name(y, "x")
_test_variable_name_util(x, "y")
_test_variable_name_util(y, "x")
set_name(x, "x")
@test_throws err("x") variable_by_name(model, "x")
@test variable_by_name(model, "y") isa Nothing
set_name(y, "y")
_test_variable_name_util(x, "x")
_test_variable_name_util(y, "y")
return
end
function test_extension_variable_condition_in_indexing(
ModelType = Model,
VariableRefType = VariableRef,
)
function test_one_dim(x)
@test 5 == @inferred length(x)
for i in 1:10
@test haskey(x, i) == iseven(i)
end
return
end
function test_two_dim(y)
@test 15 == @inferred length(y)
for j in 1:10, k in 3:2:9
@test haskey(y, (j, k)) == (isodd(j + k) && k <= 8)
end
return
end
model = ModelType()
# Parses as ref on 0.7.
@variable(model, named_one_dim[i = 1:10; iseven(i)])
test_one_dim(named_one_dim)
# Parses as vcat on 0.7.
anon_one_dim = @variable(model, [i = 1:10; iseven(i)])
test_one_dim(anon_one_dim)
# Parses as typed_vcat on 0.7.
@variable(model, named_two_dim[j = 1:10, k = 3:2:9; isodd(j + k) && k <= 8])
test_two_dim(named_two_dim)
# Parses as vect on 0.7.
anon_two_dim =
@variable(model, [j = 1:10, k = 3:2:9; isodd(j + k) && k <= 8])
test_two_dim(anon_two_dim)
return
end
function test_extension_variable_macro_return_type(
ModelType = Model,
VariableRefType = VariableRef,
)
model = ModelType()
@variable(model, x[1:3, 1:4, 1:2], start = 0.0)
@test typeof(x) == Array{VariableRefType,3}
@test typeof(start_value.(x)) == Array{Float64,3}
@variable(model, y[1:0], start = 0.0)
@test typeof(y) == Vector{VariableRefType}
# No type to infer for an empty collection.
@test typeof(start_value.(y)) == Vector{Union{Nothing,Float64}}
@variable(model, z[1:4], start = 0.0)
@test typeof(z) == Vector{VariableRefType}
@test typeof(start_value.(z)) == Vector{Float64}
return
end
function test_extension_variable_start_value_on_empty(
ModelType = Model,
VariableRefType = VariableRef,
)
model = ModelType()
@variable(model, x[1:4, 1:0, 1:3], start = 0) # Array{VariableRef}
@variable(model, y[1:4, 2:1, 1:3], start = 0) # DenseAxisArray
@variable(model, z[1:4, Set(), 1:3], start = 0) # SparseAxisArray
@test start_value.(x) == Array{Float64}(undef, 4, 0, 3)
# TODO: Decide what to do here. I don't know if we still need to test this
# given broadcast syntax.
# @test typeof(start_value(y)) <: DenseAxisArray{Float64}
# @test size(start_value(y)) == (4,0,3)
# @test typeof(start_value(z)) ==
# DenseAxisArray{Float64,3,Tuple{UnitRange{Int},Set{Any},UnitRange{Int}}}
# @test length(start_value(z)) == 0
return
end
function test_extension_variable_denseaxisarray_slices(
ModelType = Model,
VariableRefType = VariableRef,
)
# Test slicing DenseAxisArrays (JuMP issue #684).
model = ModelType()
@variable(model, x[1:3, 1:4, 1:2], container = DenseAxisArray)
@variable(model, y[1:3, -1:2, 3:4])
@variable(model, z[1:3, -1:2:4, 3:4])
@variable(model, w[1:3, -1:2, [:red, "blue"]])
#@test x[:] == vec(_sliceof_util(VariableRefType, x, 1:3, 1:4, 1:2))
@test x isa Containers.DenseAxisArray
@test x[:, :, :].data == _sliceof_util(VariableRefType, x, 1:3, 1:4, 1:2)
@test x[1, :, :].data == _sliceof_util(VariableRefType, x, 1, 1:4, 1:2)
@test x[1, :, 2].data == _sliceof_util(VariableRefType, x, 1, 1:4, 2)
@test_throws KeyError x[1, :, 3]
#@test x[1:2,:,:].data == _sliceof_util(VariableRefType, x, 1:2, 1:4, 1:2)
#@test x[1:2,:,2].data == _sliceof_util(VariableRefType, x, 1:2, 1:4, 2)
#@test x[1:2,:,1:2].data == _sliceof_util(VariableRefType, x, 1:2, 1:4, 1:2)
@test_throws KeyError x[1:2, :, 1:3]
#@test y[:] == vec(_sliceof_util(VariableRefType, y, 1:3, -1:2, 3:4))
@test y[:, :, :].data == _sliceof_util(VariableRefType, y, 1:3, -1:2, 3:4)
@test y[1, :, :].data == _sliceof_util(VariableRefType, y, 1, -1:2, 3:4)
@test y[1, :, 4].data == _sliceof_util(VariableRefType, y, 1, -1:2, 4)
@test_throws KeyError y[1, :, 5]
# @test y[1:2,:,:] == _sliceof_util(VariableRefType, y, 1:2, -1:2, 3:4)
# @test y[1:2,:,4] == _sliceof_util(VariableRefType, y, 1:2, -1:2, 4)
# @test y[1:2,:,3:4] == _sliceof_util(VariableRefType, y, 1:2, -1:2, 3:4)
# @test_throws BoundsError y[1:2,:,1:3]
#@test z[:] == vec(_sliceof_util(VariableRefType, z, 1:3, -1:2:4, 3:4))
@test z[:, 1, :].data == _sliceof_util(VariableRefType, z, 1:3, 1, 3:4)
@test z[1, 1, :].data == _sliceof_util(VariableRefType, z, 1, 1, 3:4)
@test_throws KeyError z[:, 5, 3]
# @test z[1:2,1,:] == _sliceof_util(VariableRefType, z, 1:2, 1, 3:4)
# @test z[1:2,1,4] == _sliceof_util(VariableRefType, z, 1:2, 1, 4)
# @test z[1:2,1,3:4] == _sliceof_util(VariableRefType, z, 1:2, 1, 3:4)
# @test_throws BoundsError z[1:2,1,1:3]
#@test w[:] == vec(_sliceof_util(VariableRefType, w, 1:3, -1:2, [:red,"blue"]))
@test w[:, :, :] == w
@test w[1, :, "blue"].data ==
_sliceof_util(VariableRefType, w, 1, -1:2, ["blue"])
@test w[1, :, :red].data ==
_sliceof_util(VariableRefType, w, 1, -1:2, [:red])
@test_throws KeyError w[1, :, "green"]
# @test w[1:2,:,"blue"] == _sliceof_util(VariableRefType, w, 1:2, -1:2, ["blue"])
# @test_throws ErrorException w[1:2,:,[:red,"blue"]]
return
end
function test_extension_variable_end_indexing(
ModelType = Model,
VariableRefType = VariableRef,
)
model = ModelType()
@variable(model, x[0:2, 1:4])
@variable(model, z[0:2])
@test x[end, 1] == x[2, 1]
@test x[0, end-1] == x[0, 3]
@test z[end] == z[2]
# TODO: It is redirected to x[11] as it is the 11th element but linear
# indexing is not supported
@test_throws BoundsError x[end-1]
return
end
function test_extension_variable_unsigned_index(
ModelType = Model,
VariableRefType = VariableRef,
)
# Tests unsigned int can be used to construct index set (JuMP issue #857).
model = ModelType()
t = UInt(4)
@variable(model, x[1:t])
@test 4 == @inferred num_variables(model)
return
end
function test_extension_variable_symmetric(
ModelType = Model,
VariableRefType = VariableRef,
)
model = ModelType()
@variable(model, x[1:2, 1:2], Symmetric)
@test x isa LinearAlgebra.Symmetric
@test x[1, 2] === x[2, 1]
@test model[:x] === x
y = @variable(model, [1:2, 1:2], Symmetric)
@test y isa LinearAlgebra.Symmetric
@test y[1, 2] === y[2, 1]
return
end
function test_extension_variable_skewsymmetric(
ModelType = Model,
VariableRefType = VariableRef,
)
model = ModelType()
@variable(model, x[1:2, 1:2] in SkewSymmetricMatrixSpace())
@test x[1, 2] == -x[2, 1]
@test iszero(x[1, 1])
@test iszero(x[2, 2])
@test model[:x] === x
@test num_variables(model) == 1
@variable(model, z[1:3, 1:3] in SkewSymmetricMatrixSpace())
@test z[1, 2] == -z[2, 1]
@test z[1, 3] == -z[3, 1]
@test z[2, 3] == -z[3, 2]
@test iszero(z[1, 1])
@test iszero(z[2, 2])
@test iszero(z[3, 3])
@test model[:z] === z
@test num_variables(model) == 4
y = @variable(model, [1:3, 1:3] in SkewSymmetricMatrixSpace())
@test y[1, 2] == -y[2, 1]
@test y[2, 3] == -y[3, 2]
@test iszero(y[3, 3])
@test num_variables(model) == 7
return
end
function test_extension_variables_constrained_on_creation_errors(
ModelType = Model,
VariableRefType = VariableRef,
)
model = ModelType()
@test_macro_throws(
ErrorException(
"In `@variable(model, x[1:2] in SecondOrderCone(), set = PSDCone())`: " *
"Cannot use set keyword because the variable is already " *
"constrained to `$(Expr(:escape, :(SecondOrderCone())))`.",
),
@variable(model, x[1:2] in SecondOrderCone(), set = PSDCone()),
)
@test_macro_throws(
ErrorException(
"In `@variable(model, x[1:2] in SecondOrderCone(), PSD)`: " *
"Cannot pass `PSD` as a positional argument because the variable " *
"is already constrained to `$(Expr(:escape, :(SecondOrderCone())))`.",
),
@variable(model, x[1:2] in SecondOrderCone(), PSD),
)
@test_macro_throws(
ErrorException(
"In `@variable(model, x[1:2, 1:2], PSD, Symmetric)`: " *
"Cannot pass `Symmetric` as a positional argument because the " *
"variable is already constrained to `$(PSDCone())`.",
),
@variable(model, x[1:2, 1:2], PSD, Symmetric),
)
@test_macro_throws(
ErrorException(
"In `@variable(model, x[1:2], set = SecondOrderCone(), set = PSDCone())`: " *
"`set` keyword argument was given 2 times.",
),
@variable(model, x[1:2], set = SecondOrderCone(), set = PSDCone()),
)
return
end
function test_extension_variables_constrained_on_creation(
ModelType = Model,
VariableRefType = VariableRef,
)
model = ModelType()
@variable(model, x[1:2] in SecondOrderCone())
@test num_constraints(model, typeof(x), MOI.SecondOrderCone) == 1
@test name(x[1]) == "x[1]"
@test name(x[2]) == "x[2]"
@variable(model, [1:2] in SecondOrderCone())
@test num_constraints(model, typeof(x), MOI.SecondOrderCone) == 2
@variable(model, [1:3] ∈ MOI.SecondOrderCone(3))
@test num_constraints(model, typeof(x), MOI.SecondOrderCone) == 3
z = @variable(model, z ∈ MOI.Semiinteger(1.0, 2.0))
@test num_constraints(model, typeof(z), MOI.Semiinteger{Float64}) == 1
@variable(model, set = MOI.Semiinteger(1.0, 2.0))
@test num_constraints(model, typeof(z), MOI.Semiinteger{Float64}) == 2
X = @variable(model, [1:3, 1:3] in PSDCone())
@test X isa LinearAlgebra.Symmetric
@test num_constraints(
model,
typeof(x),
MOI.PositiveSemidefiniteConeTriangle,
) == 1
return
end
function test_extension_batch_delete_variables(
ModelType = Model,
VariableRefType = VariableRef,
)
model = ModelType()
@variable(model, x[1:3] >= 1)
@objective(model, Min, sum([1, 2, 3] .* x))
@test all(is_valid.(model, x))
delete(model, x[[1, 3]])
@test all((!is_valid).(model, x[[1, 3]]))
@test is_valid(model, x[2])
second_model = ModelType()
@test_throws Exception delete(second_model, x[2])
@test_throws Exception delete(second_model, x[[1, 3]])
return
end
function test_all_variable()
model = Model()
@variable(model, x)
@variable(model, y)
@test [x, y] == @inferred all_variables(model)
return
end
function test_macro_variables()
model = Model()
@variables model begin
0 ≤ x[i = 1:2] ≤ i
y ≥ 2, Int, (start = 0.7)
z ≤ 3, (start = 10)
q, (Bin, start = 0.5)
end
@test "x[1]" == @inferred name(x[1])
@test 0 == @inferred lower_bound(x[1])
@test 1 == @inferred upper_bound(x[1])
@test !is_binary(x[1])
@test !is_integer(x[1])
@test start_value(x[1]) === nothing
@test "x[2]" == @inferred name(x[2])
@test 0 == @inferred lower_bound(x[2])
@test 2 == @inferred upper_bound(x[2])
@test !is_binary(x[2])
@test !is_integer(x[2])
@test start_value(x[2]) === nothing
@test "y" == @inferred name(y)
@test 2 == @inferred lower_bound(y)
@test !has_upper_bound(y)
@test !is_binary(y)
@test is_integer(y)
@test start_value(y) === 0.7
@test "z" == @inferred name(z)
@test !has_lower_bound(z)
@test 3 == @inferred upper_bound(z)
@test !is_binary(z)
@test !is_integer(z)
@test start_value(z) === 10.0
@test "q" == @inferred name(q)
@test !has_lower_bound(q)
@test !has_upper_bound(q)
@test is_binary(q)
@test !is_integer(q)
@test start_value(q) === 0.5
return
end
function test_dual_variable()
model = Model()
@variable(model, x == 0)
exception = ErrorException(
"To query the dual variables associated with a variable bound, first " *
"obtain a constraint reference using one of `UpperBoundRef`, `LowerBoundRef`, " *
"or `FixRef`, and then call `dual` on the returned constraint reference.\nFor " *
"example, if `x <= 1`, instead of `dual(x)`, call `dual(UpperBoundRef(x))`.",
)
@test_throws exception dual(x)
return
end
function test_value_containers()
model = Model()
@variable(model, x[1:2])
exception = ErrorException(
"`JuMP.value` is not defined for collections of JuMP types. Use " *
"Julia's broadcast syntax instead: `JuMP.value.(x)`.",
)
@test_throws exception value(x)
return
end
function test_get_variable_coefficient()
m = Model()
x = @variable(m, x)
y = @variable(m, y)
@test coefficient(x, x) == 1.0
@test coefficient(x, y) == 0.0
@test coefficient(x, x, x) == 0.0
@test coefficient(x, y, x) == coefficient(x, x, y) == 0.0
@test coefficient(x, y, y) == 0.0
return
end
function _mock_reduced_cost_util(
obj_sense::OptimizationSense,
#obj_value::Float64,
var_obj_coeff::Float64,
#var_value,
var_bound_type::Symbol,
var_bounds_dual = nothing,
has_duals::Bool = var_bounds_dual !== nothing,
)
mockoptimizer =
MOIU.MockOptimizer(MOIU.Model{Float64}(); eval_objective_value = false)
m = direct_model(mockoptimizer)
if var_bound_type === :lower
@variable(m, x >= 0)
if has_duals
@assert isa(var_bounds_dual, Float64)
has_duals && MOI.set(
mockoptimizer,
MOI.ConstraintDual(),
optimizer_index(LowerBoundRef(x)),
var_bounds_dual,
)
end
elseif var_bound_type === :upper
@variable(m, x <= 10)
if has_duals
@assert isa(var_bounds_dual, Float64)
has_duals && MOI.set(
mockoptimizer,
MOI.ConstraintDual(),
optimizer_index(UpperBoundRef(x)),
var_bounds_dual,
)
end
elseif var_bound_type === :fixed
@variable(m, x == 10)
if has_duals
@assert isa(var_bounds_dual, Float64)
MOI.set(
mockoptimizer,
MOI.ConstraintDual(),
optimizer_index(FixRef(x)),
var_bounds_dual,
)
end
elseif var_bound_type === :both
@variable(m, 0 <= x <= 10)
if has_duals
@assert length(var_bounds_dual) == 2
@assert eltype(var_bounds_dual) == Float64
lb_dual, ub_dual = var_bounds_dual
MOI.set(
mockoptimizer,
MOI.ConstraintDual(),
optimizer_index(LowerBoundRef(x)),
lb_dual,
)
MOI.set(
mockoptimizer,
MOI.ConstraintDual(),
optimizer_index(UpperBoundRef(x)),
ub_dual,
)
end
elseif var_bound_type === :none
@variable(m, x)
@assert var_bounds_dual === nothing
else
error("unrecognized bound type")
end
@objective(m, obj_sense, var_obj_coeff * x)
if has_duals
MOI.set(mockoptimizer, MOI.TerminationStatus(), MOI.OPTIMAL)
MOI.set(mockoptimizer, MOI.ResultCount(), 1)
MOI.set(mockoptimizer, MOI.PrimalStatus(), MOI.FEASIBLE_POINT)
MOI.set(mockoptimizer, MOI.DualStatus(), MOI.FEASIBLE_POINT)
end
optimize!(m)
return x
end
function test_reduced_cost()
Min = MIN_SENSE
Max = MAX_SENSE
# The method should always fail if duals are not available.
x = _mock_reduced_cost_util(Min, 1.0, :none)
@test_throws ErrorException reduced_cost(x)
x = _mock_reduced_cost_util(Min, 1.0, :fixed)
@test_throws ErrorException reduced_cost(x)
x = _mock_reduced_cost_util(Min, 1.0, :lower)
@test_throws ErrorException reduced_cost(x)
x = _mock_reduced_cost_util(Min, 1.0, :upper)
@test_throws ErrorException reduced_cost(x)
x = _mock_reduced_cost_util(Min, 1.0, :both)
@test_throws ErrorException reduced_cost(x)
# My reimplementation of the tests suggested by @odow.
# Note that the floating point values are compared by equality because
# there is no risk of the solver messing this up (mocks are being used).
# First the fixed variable tests.
x = _mock_reduced_cost_util(Min, 1.0, :none, nothing, true) # free var
@test reduced_cost(x) == 0.0
x = _mock_reduced_cost_util(Min, 1.0, :fixed, 1.0) # min x, x == 10
@test reduced_cost(x) == 1.0
x = _mock_reduced_cost_util(Max, 1.0, :fixed, -1.0) # max x, x == 10
@test reduced_cost(x) == 1.0
x = _mock_reduced_cost_util(Min, -1.0, :fixed, -1.0) # min -x, x == 10
@test reduced_cost(x) == -1.0
x = _mock_reduced_cost_util(Max, -1.0, :fixed, 1.0) # max -x, x == 10
@test reduced_cost(x) == -1.0
# Then the double bounded variables.
#x = _mock_reduced_cost_util(Min, 1.0, :both, (0.0, 1.0)) # min x, 0 <= x <= 10
#@test reduced_cost(x) == 1.0
#x = _mock_reduced_cost_util(Max, 1.0, :both, (-1.0, 0.0)) # max x, 0 <= x <= 10
#@test reduced_cost(x) == 1.0
#x = _mock_reduced_cost_util(Min, -1.0, :both, (-1.0, 0.0)) # min -x, 0 <= x <= 10
#@test reduced_cost(x) == -1.0
#x = _mock_reduced_cost_util(Max, -1.0, :both, (0.0, 1.0)) # max -x, 0 <= x <= 10
#@test reduced_cost(x) == -1.0
# Test for a single upper bound and a single lower bound.
x = _mock_reduced_cost_util(Min, 1.0, :lower, 1.0) # min x, 0 <= x
@test reduced_cost(x) == 1.0
x = _mock_reduced_cost_util(Max, 1.0, :upper, 1.0) # max x, x <= 10
@test reduced_cost(x) == 1.0
return
end
function test_value()
@test value(1) === 1
@test value(1.0) === 1.0
@test value(JuMP._MA.Zero()) === 0.0
return
end
function test_value_var()
model = Model()
@variable(model, x[1:2])
vals = Dict(x[1] => 1.0, x[2] => 2.0)
f = vidx -> vals[vidx]
@test value(f, x[1]) === 1.0
@test value(f, x[2]) === 2.0
return
end
function test_relax_integrality()
model = Model()
@variable(model, x, Bin)
@variable(model, -1 <= y <= 2, Bin)
@variable(model, 0.1 <= z <= 0.6, Bin)
@variable(model, a, Int)
@variable(model, -1 <= b <= 2, Int)
unrelax = relax_integrality(model)
@test !is_binary(x)
@test lower_bound(x) == 0.0
@test upper_bound(x) == 1.0
@test !is_binary(y)
@test lower_bound(y) == 0.0
@test upper_bound(y) == 1.0
@test !is_binary(z)
@test lower_bound(z) == 0.1
@test upper_bound(z) == 0.6
@test !is_integer(a)
@test !has_lower_bound(a)
@test !has_upper_bound(a)
@test !is_integer(b)
@test lower_bound(b) == -1.0
@test upper_bound(b) == 2.0
unrelax()
@test is_binary(x)
@test !has_lower_bound(x)
@test !has_upper_bound(x)
@test is_binary(y)
@test lower_bound(y) == -1.0
@test upper_bound(y) == 2.0
@test is_binary(z)
@test lower_bound(z) == 0.1
@test upper_bound(z) == 0.6
@test is_integer(a)
@test !has_lower_bound(a)
@test !has_upper_bound(a)
@test is_integer(b)
@test lower_bound(b) == -1.0
@test upper_bound(b) == 2.0
fix(x, 1)
unrelax = relax_integrality(model)
@test !is_binary(x)
@test is_fixed(x)
unrelax()
@test is_binary(x)
@test is_fixed(x)
fix(x, 0)
unrelax = relax_integrality(model)
@test !is_binary(x)
@test is_fixed(x)
unrelax()
@test is_binary(x)
@test is_fixed(x)
fix(a, 1)
unrelax = relax_integrality(model)
@test !is_integer(a)
@test is_fixed(a)
unrelax()
@test is_integer(a)
@test is_fixed(a)
return
end
function test_relax_integrality_error_cases()
model = Model()
@variable(model, x)
@constraint(model, x in MOI.Semicontinuous(1.0, 2.0))
err = ErrorException(
"Support for relaxing semicontinuous constraints " *
"is not yet implemented.",
)
@test_throws err relax_integrality(model)
model = Model()