forked from jump-dev/JuMP.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_nlp.jl
1566 lines (1435 loc) · 45.7 KB
/
test_nlp.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
#############################################################################
module TestNLP
using JuMP
using Test
import LinearAlgebra
import SparseArrays
include(joinpath(@__DIR__, "utilities.jl"))
function test_univariate_error()
model = Model()
@variable(model, x >= 0)
@test_throws ErrorException @NLobjective(model, Min, g_doesnotexist(x))
end
function test_univariate_error_existing()
model = Model()
@variable(model, x >= 0)
@NLexpression(model, ex, x^2)
@test_throws ErrorException @NLobjective(model, Min, g_doestnotexist(ex))
end
function test_univariate()
model = Model()
@variable(model, x >= 0)
g(x) = x^2
@test_logs (:warn,) @NLobjective(model, Min, g(x))
d = JuMP.NLPEvaluator(model)
MOI.initialize(d, Symbol[])
x = [2.0]
@test MOI.eval_objective(d, x) == 4.0
end
function test_univariate_register_twice()
model = Model()
@variable(model, x >= 0)
g(x) = x^2
@test_logs (:warn,) @NLobjective(model, Min, g(x))
@test_logs @NLconstraint(model, g(x) <= 1)
d = JuMP.NLPEvaluator(model)
MOI.initialize(d, Symbol[])
x = [2.0]
y = [NaN]
MOI.eval_constraint(d, y, x)
@test y == [3.0]
end
function test_univariate_register_twice_error()
model = Model()
@variable(model, x >= 0)
g(x) = x^2
g(x, y) = x^2 + x^2
@test_logs (:warn,) @NLobjective(model, Min, g(x))
@test_throws ErrorException @NLconstraint(model, g(x, x) <= 1)
end
function test_univariate_existing_nlpdata()
model = Model()
@variable(model, x >= 0)
@NLexpression(model, ex, x^2)
g(x) = x^2
@test_logs (:warn,) @NLobjective(model, Min, g(ex))
d = JuMP.NLPEvaluator(model)
MOI.initialize(d, Symbol[])
x = [2.0]
@test MOI.eval_objective(d, x) == 16.0
end
function test_univariate_redefine()
model = Model()
@variable(model, x >= 0)
g = (x) -> x^2
@test_logs (:warn,) @NLobjective(model, Min, g(x))
d = JuMP.NLPEvaluator(model)
MOI.initialize(d, Symbol[])
x = [2.0]
@test MOI.eval_objective(d, x) == 4.0
g = (x) -> 2x^2
@test MOI.eval_objective(d, x) == 4.0
end
function test_multivariate_error()
model = Model()
@variable(model, x >= 0)
@test_throws ErrorException @NLobjective(model, Min, g_doesnotexist(x, x))
end
function test_multivariate_error_existing()
model = Model()
@variable(model, x >= 0)
@NLexpression(model, ex, x^2)
@test_throws ErrorException @NLobjective(model, Min, g_doestnotexist(ex, x))
end
function test_multivariate()
model = Model()
@variable(model, x >= 0)
g(x, y) = x^2 + y^2
@test_logs (:warn,) @NLobjective(model, Min, g(x, x))
d = JuMP.NLPEvaluator(model)
MOI.initialize(d, Symbol[])
x = [2.0]
@test MOI.eval_objective(d, x) == 8.0
end
function test_multivariate_register_warn()
model = Model()
g(x, y) = x^2 + y^2
function ∇g(g::Vector{T}, x::T, y::T) where {T<:Real}
g[1] = y
g[2] = x
return
end
@test_logs (:warn,) register(model, :g, 2, g, ∇g; autodiff = true)
end
function test_multivariate_existing_nlpdata()
model = Model()
@variable(model, x >= 0)
@NLexpression(model, ex, x^2)
g(x, y) = x^2 + y^2
@test_logs (:warn,) @NLobjective(model, Min, g(ex, x))
d = JuMP.NLPEvaluator(model)
MOI.initialize(d, Symbol[])
x = [2.0]
@test MOI.eval_objective(d, x) == 20.0
end
function test_multivariate_redefine()
model = Model()
@variable(model, x >= 0)
@NLexpression(model, ex, x^2)
g = (x, y) -> x^2 + y^2
@test_logs (:warn,) @NLobjective(model, Min, g(ex, x))
d = JuMP.NLPEvaluator(model)
MOI.initialize(d, Symbol[])
x = [2.0]
@test MOI.eval_objective(d, x) == 20.0
g = (x, y) -> x^2 + y
@test MOI.eval_objective(d, x) == 20.0
end
function test_multivariate_register_splat()
model = Model()
@variable(model, x[1:2])
f(x, y) = x + y
err = ErrorException("""
Unrecognized function "f" used in nonlinear expression.
You must register it as a user-defined function before building
the model. For example, replacing `N` with the appropriate number
of arguments, do:
```julia
model = Model()
register(model, :f, N, f, autodiff=true)
# ... variables and constraints ...
```
""")
@test_throws err @NLexpression(model, ex, f(x...))
end
function test_multivariate_register_splat_existing()
model = Model()
@variable(model, x[1:2])
f(x, y) = x + y
@NLconstraint(model, x[1]^2 <= 1)
err = ErrorException("""
Unrecognized function "f" used in nonlinear expression.
You must register it as a user-defined function before building
the model. For example, replacing `N` with the appropriate number
of arguments, do:
```julia
model = Model()
register(model, :f, N, f, autodiff=true)
# ... variables and constraints ...
```
""")
@test_throws err @NLexpression(model, ex, f(x...))
end
function test_multivariate_max()
m = Model()
@variable(m, x)
@NLobjective(m, Min, max(0, x))
nlp = NLPEvaluator(m)
MOI.initialize(nlp, [:ExprGraph])
@test MOI.eval_objective(nlp, [-1.0]) == 0.0
@test MOI.eval_objective(nlp, [1.0]) == 1.0
@test MOI.objective_expr(nlp) == :(max(0.0, x[$(index(x))]))
return
end
function test_multivariate_min()
m = Model()
@variable(m, x)
@NLobjective(m, Max, min(0, x))
nlp = NLPEvaluator(m)
MOI.initialize(nlp, [:ExprGraph])
@test MOI.eval_objective(nlp, [-1.0]) == -1.0
@test MOI.eval_objective(nlp, [1.0]) == 0.0
@test MOI.objective_expr(nlp) == :(min(0.0, x[$(index(x))]))
return
end
function test_register_check_forwarddiff_univariate_f()
model = Model()
f(x::Float64) = log(x)
@test_throws(ErrorException, register(model, :f, 1, f; autodiff = true))
return
end
function test_register_check_forwarddiff_univariate_gradf()
model = Model()
f(x) = log(x)
# This is a common case, where user's type their arguments
∇f(x::Float64) = 1 / x
@test_throws(ErrorException, register(model, :f, 1, f, ∇f; autodiff = true))
return
end
function test_register_check_forwarddiff_multivariate()
model = Model()
function f(x...)
# This is a common case, where user's preallocate a Float64 storage.
y = zeros(length(x))
for i in eachindex(x)
y[i] = log(x[i])
end
return sum(y)
end
@test_throws(ErrorException, register(model, :f, 3, f; autodiff = true))
return
end
"""
test_register_check_forwarddiff_multivariate_gradf()
Because we disable Hessians, the functions in the multivariate case do not need
to be differentiable.
"""
function test_register_check_forwarddiff_multivariate_gradf()
model = Model()
function f(x...)
# This is a common case, where user's preallocate a Float64 storage.
y = zeros(length(x))
for i in eachindex(x)
y[i] = log(x[i])
end
return sum(y)
end
function ∇f(x...)
# This is a common case, where user's preallocate a Float64 storage.
y = zeros(length(x))
for i in eachindex(x)
y[i] = 1 / x[i]
end
return sum(y)
end
register(model, :f, 3, f, ∇f)
return
end
function test_all_nonlinear_constraints()
model = Model()
@variable(model, x)
@NLconstraint(model, c1, x^2 <= 1)
c2 = @NLconstraint(model, [i = 1:2], x^i >= -1)
@test all_nonlinear_constraints(model) == [c1; c2]
return
end
function test_parse_plus_binary()
m = Model()
@variable(m, x)
@variable(m, y)
ex = @NLexpression(m, x + y)
@test sprint(show, ex) == "subexpression[1]: x + y"
return
end
function test_parse_plus_ternary()
m = Model()
@variable(m, x)
@variable(m, y)
@variable(m, z)
ex = @NLexpression(m, x + y + z)
@test sprint(show, ex) == "subexpression[1]: x + y + z"
return
end
function test_parse_mult_binary()
m = Model()
@variable(m, x)
@variable(m, y)
ex = @NLexpression(m, x * y)
@test sprint(show, ex) == "subexpression[1]: x * y"
return
end
function test_parse_mult_ternary()
m = Model()
@variable(m, x)
@variable(m, y)
@variable(m, z)
ex = @NLexpression(m, x * y * z)
@test sprint(show, ex) == "subexpression[1]: x * y * z"
return
end
function test_parse_exp_binary()
m = Model()
@variable(m, x)
ex = @NLexpression(m, x^3)
@test sprint(show, ex) == "subexpression[1]: x ^ 3.0"
return
end
function test_parse_sin()
m = Model()
@variable(m, x)
ex = @NLexpression(m, sin(x))
@test sprint(show, ex) == "subexpression[1]: sin(x)"
return
end
function test_parse_ifelse()
m = Model()
@variable(m, x)
ex = @NLexpression(m, ifelse(1 == 2 || 3 == 4 && 5 == 6, x, 0.0))
@test sprint(show, ex) ==
"subexpression[1]: ifelse(1.0 == 2.0 || 3.0 == 4.0 && 5.0 == 6.0, x, 0.0)"
return
end
function test_parse_ifelse_comparison()
m = Model()
@variable(m, x)
ex = @NLexpression(m, ifelse(1 <= 2 <= 3, x, 0.0))
@test sprint(show, ex) ==
"subexpression[1]: ifelse(1.0 <= 2.0 <= 3.0, x, 0.0)"
return
end
function test_parse_sum()
m = Model()
@variable(m, x[1:2])
ex = @NLexpression(m, sum(x[i] for i in 1:2))
@test sprint(show, ex) == "subexpression[1]: x[1] + x[2]"
return
end
function test_parse_prod()
m = Model()
@variable(m, x[1:2])
ex = @NLexpression(m, prod(x[i] for i in 1:2))
@test sprint(show, ex) == "subexpression[1]: x[1] * x[2]"
return
end
function test_parse_subexpressions()
m = Model()
@variable(m, x)
@NLexpression(m, ex, x^2)
ex2 = @NLexpression(m, ex + 1)
@test sprint(show, ex2) == "subexpression[2]: subexpression[1] + 1.0"
return
end
function test_parse_parameters()
m = Model()
@NLparameter(m, param == 10)
ex = @NLexpression(m, param + 1)
@test sprint(show, ex) == "subexpression[1]: param + 1.0"
return
end
function test_parse_user_defined_function_univariate()
model = Model()
@variable(model, x)
user_function = x -> x
JuMP.register(model, :f, 1, user_function; autodiff = true)
ex = @NLexpression(model, f(x))
@test sprint(show, ex) == "subexpression[1]: f(x)"
return
end
function test_parse_user_defined_function_multivariate()
model = Model()
@variable(model, x)
@variable(model, y)
user_function = (x, y) -> x
JuMP.register(model, :f, 2, user_function; autodiff = true)
ex = @NLexpression(model, f(x, y))
@test sprint(show, ex) == "subexpression[1]: f(x, y)"
return
end
function test_parse_splatting()
model = Model()
@variable(model, x[1:2])
user_function = (x, y) -> x
JuMP.register(model, :f, 2, user_function; autodiff = true)
ex = @NLexpression(model, f(x...))
@test sprint(show, ex) == "subexpression[1]: f(x[1], x[2])"
return
end
function test_parse_mixed_splatting()
model = Model()
@variable(model, x[1:2])
@variable(model, y)
@variable(model, z[1:1])
ex = @NLexpression(model, *(x..., y, z...))
@test sprint(show, ex) == "subexpression[1]: x[1] * x[2] * y * z[1]"
return
end
function test_error_splatting_non_symbols()
model = Model()
@variable(model, x[1:2])
@test_throws ErrorException @NLexpression(model, (*)((x / 2)...))
return
end
function test_error_on_begin_end()
model = Model()
@variable(model, x)
err = ErrorException(
"`begin...end` blocks are not supported in nonlinear macros. The " *
"nonlinear expression must be a single statement.",
)
@test_macro_throws(err, @NLobjective(model, Max, begin
sin(x) + 1
end))
return
end
function test_error_on_unexpected_splatting()
model = Model()
@variable(model, x[1:2])
@test_throws(ErrorException, @NLexpression(model, x...))
return
end
function test_error_on_getfield_in_expression()
model = Model()
@variable(model, x[1:2])
@test_macro_throws(
ErrorException,
@NLexpression(model, sum(foo.bar(i) * x[i] for i in 1:2))
)
return
end
function test_error_on_sum()
m = Model()
x = [1, 2, 3]
@test_throws ErrorException @NLexpression(m, sum(x))
return
end
function test_error_on_non_scalar_expression()
m = Model()
x = [1, 2, 3]
@test_throws ErrorException @NLexpression(m, x + 1)
return
end
# Converts the lower-triangular sparse Hessian in MOI format into a dense
# matrix.
function _dense_hessian(hessian_sparsity, V, n)
I = [i for (i, j) in hessian_sparsity]
J = [j for (i, j) in hessian_sparsity]
raw = SparseArrays.sparse(I, J, V, n, n)
return Matrix(
raw + raw' -
SparseArrays.sparse(LinearAlgebra.diagm(0 => LinearAlgebra.diag(raw))),
)
end
# Converts the sparse Jacobian in MOI format into a dense matrix.
function _dense_jacobian(jacobian_sparsity, V, m, n)
I = [i for (i, j) in jacobian_sparsity]
J = [j for (i, j) in jacobian_sparsity]
raw = SparseArrays.sparse(I, J, V, m, n)
return Matrix(raw)
end
function test_hessian_evaluation_issue_435()
m = Model()
@variable(m, a)
@variable(m, b)
@variable(m, c)
@NLexpression(m, foo, a * b + c^2)
@NLobjective(m, Min, foo)
d = JuMP.NLPEvaluator(m)
MOI.initialize(d, [:Hess])
hessian_sparsity = MOI.hessian_lagrangian_structure(d)
V = zeros(length(hessian_sparsity))
values = [1.0, 2.0, 3.0] # Values for a, b, and c, respectively.
MOI.eval_hessian_lagrangian(d, V, values, 1.0, Float64[])
@test _dense_hessian(hessian_sparsity, V, 3) ≈
[0.0 1.0 0.0; 1.0 0.0 0.0; 0.0 0.0 2.0]
# make sure we don't get NaNs in this case
@NLobjective(m, Min, a * b + 3 * c^2)
d = JuMP.NLPEvaluator(m)
MOI.initialize(d, [:Hess])
values = [1.0, 2.0, -1.0]
V = zeros(length(hessian_sparsity))
MOI.eval_hessian_lagrangian(d, V, values, 1.0, Float64[])
@test _dense_hessian(hessian_sparsity, V, 3) ≈
[0.0 1.0 0.0; 1.0 0.0 0.0; 0.0 0.0 6.0]
# Initialize again
MOI.initialize(d, [:Hess])
V = zeros(length(hessian_sparsity))
MOI.eval_hessian_lagrangian(d, V, values, 1.0, Float64[])
@test _dense_hessian(hessian_sparsity, V, 3) ≈
[0.0 1.0 0.0; 1.0 0.0 0.0; 0.0 0.0 6.0]
return
end
function test_NaN_corner_case_issue_695()
m = Model()
x0 = 0.0
y0 = 0.0
@variable(m, x)
@variable(m, y)
@NLobjective(m, Min, (x - x0) / (sqrt(y0) + sqrt(y)))
d = JuMP.NLPEvaluator(m)
MOI.initialize(d, [:HessVec])
h = ones(2)
v = [2.4, 3.5]
values = [1.0, 2.0] # For x and y.
MOI.eval_hessian_lagrangian_product(d, h, values, v, 1.0, Float64[])
correct = [0.0 -1/(2*2^(3/2)); -1/(2*2^(3/2)) 3/(4*2^(5/2))] * v
@test h ≈ correct
return
end
function test_NaN_corner_case_issue_1205()
m = Model()
@variable(m, x)
@NLobjective(m, Min, x^1.0)
d = JuMP.NLPEvaluator(m)
MOI.initialize(d, [:Hess])
hessian_sparsity = MOI.hessian_lagrangian_structure(d)
V = zeros(length(hessian_sparsity))
values = zeros(1)
MOI.eval_hessian_lagrangian(d, V, values, 1.0, Float64[])
@test _dense_hessian(hessian_sparsity, V, 1) ≈ [0.0]
return
end
function test_NaN_corner_case_ifelse_issue_1205()
m = Model()
@variable(m, x)
@NLobjective(m, Min, ifelse(true, x, x^1.0))
d = JuMP.NLPEvaluator(m)
MOI.initialize(d, [:Hess])
hessian_sparsity = MOI.hessian_lagrangian_structure(d)
V = zeros(length(hessian_sparsity))
values = zeros(1)
MOI.eval_hessian_lagrangian(d, V, values, 1.0, Float64[])
@test _dense_hessian(hessian_sparsity, V, 1) ≈ [0.0]
return
end
function test_prod_corner_case_issue_1181()
model = Model()
@variable(model, x[1:2])
@NLobjective(model, Min, x[1] * x[2])
d = JuMP.NLPEvaluator(model)
MOI.initialize(d, [:Hess])
hessian_sparsity = MOI.hessian_lagrangian_structure(d)
V = zeros(length(hessian_sparsity))
MOI.eval_hessian_lagrangian(d, V, [0.659, 0.702], 1.0, Float64[])
@test V == [0.0, 0.0, 1.0]
return
end
function test_constant_ifelse_issue_2115()
model = Model()
@variable(model, x)
@NLobjective(model, Min, ifelse(x >= 1, 1, 0))
d = JuMP.NLPEvaluator(model)
MOI.initialize(d, [:Hess])
hessian_sparsity = MOI.hessian_lagrangian_structure(d)
@test length(hessian_sparsity) == 0
return
end
function test_hessians_and_hessvec()
m = Model()
@variable(m, a)
@variable(m, b)
@variable(m, c)
@NLobjective(m, Min, a * b + c^2)
@NLconstraint(m, c * b <= 1)
@NLconstraint(m, a^2 / 2 <= 1)
d = JuMP.NLPEvaluator(m)
MOI.initialize(d, [:HessVec, :Hess])
values = [1.0, 2.0, 3.0] # For a, b, c.
hessian_sparsity = MOI.hessian_lagrangian_structure(d)
V = zeros(length(hessian_sparsity))
MOI.eval_hessian_lagrangian(d, V, values, 1.0, [2.0, 3.0])
correct_hessian = [3.0 1.0 0.0; 1.0 0.0 2.0; 0.0 2.0 2.0]
@test _dense_hessian(hessian_sparsity, V, 3) ≈ correct_hessian
h = ones(3) # The input values should be overwritten.
v = [2.4, 3.5, 1.2]
MOI.eval_hessian_lagrangian_product(d, h, values, v, 1.0, [2.0, 3.0])
@test h ≈ correct_hessian * v
return
end
function test_hessians_and_hessvec_with_subexpressions()
m = Model()
@variable(m, a)
@variable(m, b)
@variable(m, c)
@NLexpression(m, ab, a * b)
@NLobjective(m, Min, ab + c^2)
@NLconstraint(m, c * b <= 1)
@NLconstraint(m, a^2 / 2 <= 1)
d = JuMP.NLPEvaluator(m)
MOI.initialize(d, [:HessVec, :Hess])
values = [1.0, 2.0, 3.0] # For a, b, c.
hessian_sparsity = MOI.hessian_lagrangian_structure(d)
V = zeros(length(hessian_sparsity))
MOI.eval_hessian_lagrangian(d, V, values, 1.0, [2.0, 3.0])
correct_hessian = [3.0 1.0 0.0; 1.0 0.0 2.0; 0.0 2.0 2.0]
@test _dense_hessian(hessian_sparsity, V, 3) ≈ correct_hessian
h = ones(3) # The input values should be overwritten.
v = [2.4, 3.5, 1.2]
MOI.eval_hessian_lagrangian_product(d, h, values, v, 1.0, [2.0, 3.0])
@test h ≈ correct_hessian * v
return
end
function test_jacobians_and_jacvec()
m = Model()
@variable(m, a)
@variable(m, b)
@variable(m, c)
@NLobjective(m, Min, a * b + c^2)
@NLconstraint(m, c * b <= 1)
@NLconstraint(m, a^2 / 2 <= 1)
d = JuMP.NLPEvaluator(m)
MOI.initialize(d, [:JacVec, :Jac])
values = [1.0, 2.0, 3.0] # For a, b, c.
jacobian_sparsity = MOI.jacobian_structure(d)
V = zeros(length(jacobian_sparsity))
MOI.eval_constraint_jacobian(d, V, values)
correct_jacobian = [0.0 3.0 2.0; 1.0 0.0 0.0]
@test _dense_jacobian(jacobian_sparsity, V, 2, 3) ≈ correct_jacobian
v = [2.4, 3.5, 1.2]
product_storage = zeros(2)
MOI.eval_constraint_jacobian_product(d, product_storage, values, v)
@test product_storage ≈ correct_jacobian * v
w = [0.6, 4.3]
product_storage = zeros(3)
MOI.eval_constraint_jacobian_transpose_product(
d,
product_storage,
values,
w,
)
@test product_storage ≈ correct_jacobian' * w
return
end
function test_jacobians_and_jacvec_with_subexpressions()
m = Model()
@variable(m, a)
@variable(m, b)
@variable(m, c)
@NLexpression(m, bc, b * c)
@NLobjective(m, Min, a * b + c^2)
@NLconstraint(m, bc <= 1)
@NLconstraint(m, a^2 / 2 <= 1)
d = JuMP.NLPEvaluator(m)
MOI.initialize(d, [:JacVec, :Jac])
values = [1.0, 2.0, 3.0] # For a, b, c.
jacobian_sparsity = MOI.jacobian_structure(d)
V = zeros(length(jacobian_sparsity))
MOI.eval_constraint_jacobian(d, V, values)
correct_jacobian = [0.0 3.0 2.0; 1.0 0.0 0.0]
@test _dense_jacobian(jacobian_sparsity, V, 2, 3) ≈ correct_jacobian
v = [2.4, 3.5, 1.2]
product_storage = zeros(2)
MOI.eval_constraint_jacobian_product(d, product_storage, values, v)
@test product_storage ≈ correct_jacobian * v
w = [0.6, 4.3]
product_storage = zeros(3)
MOI.eval_constraint_jacobian_transpose_product(
d,
product_storage,
values,
w,
)
@test product_storage ≈ correct_jacobian' * w
return
end
function test_expression_graphs()
m = Model()
@variable(m, x)
@variable(m, y)
@NLobjective(m, Min, x^2 + y^2)
@NLexpression(m, ex, exp(x))
@NLconstraint(m, ex - y == 0)
@NLconstraint(m, ex + 1 == 0)
d = JuMP.NLPEvaluator(m)
MOI.initialize(d, [:ExprGraph])
xidx = x.index
yidx = y.index
@test MOI.objective_expr(d) == :(x[$xidx]^2.0 + x[$yidx]^2.0)
@test MOI.constraint_expr(d, 1) ==
:((exp(x[$xidx]) - x[$yidx]) - 0.0 == 0.0)
@test MOI.constraint_expr(d, 2) == :((exp(x[$xidx]) + 1) - 0.0 == 0.0)
return
end
function test_more_expression_graphs()
m = Model()
@variable(m, x)
@variable(m, y)
ψ(x) = 1
t(x, y) = 2
JuMP.register(m, :ψ, 1, ψ; autodiff = true)
JuMP.register(m, :t, 2, t; autodiff = true)
@NLobjective(m, Min, x^y)
@NLconstraint(m, sin(x) * cos(y) == 5)
@NLconstraint(m, nlconstr[i = 1:2], i * x^2 == i)
@NLconstraint(m, -0.5 <= sin(x) <= 0.5)
@NLconstraint(m, ψ(x) + t(x, y) <= 3)
d = JuMP.NLPEvaluator(m)
MOI.initialize(d, [:ExprGraph])
xidx = x.index
yidx = y.index
@test MOI.objective_expr(d) == :(x[$xidx]^x[$yidx])
@test MOI.constraint_expr(d, 1) ==
:(sin(x[$xidx]) * cos(x[$yidx]) - 5 == 0.0)
@test MOI.constraint_expr(d, 2) == :(1.0 * x[$xidx]^2 - 1.0 == 0.0)
@test MOI.constraint_expr(d, 3) == :(2.0 * x[$xidx]^2 - 2.0 == 0.0)
@test MOI.constraint_expr(d, 4) == :(-0.5 <= sin(x[$xidx]) <= 0.5)
@test MOI.constraint_expr(d, 5) ==
:(ψ(x[$xidx]) + t(x[$xidx], x[$yidx]) - 3.0 <= 0.0)
return
end
function test_expression_graph_for_ifelse()
m = Model()
@variable(m, x)
@NLobjective(m, Min, ifelse(x <= 1, x^2, x))
d = JuMP.NLPEvaluator(m)
MOI.initialize(d, [:ExprGraph])
xidx = x.index
@test MOI.objective_expr(d) ==
:(ifelse(x[$xidx] <= 1, x[$xidx]^2, x[$xidx]))
return
end
function test_expression_graph_for_empty_sum_and_prod()
m = Model()
@variable(m, x)
@NLconstraint(m, x <= sum(0 for i in []) + prod(1 for i in []))
d = JuMP.NLPEvaluator(m)
MOI.initialize(d, [:ExprGraph])
xidx = x.index
@test MOI.constraint_expr(d, 1) == :((x[$xidx] - (0.0 + 1.0)) - 0.0 <= 0.0)
return
end
function test_NLparameter()
model = Model()
@NLparameter(model, p == 1.0)
@test JuMP.value(p) == 1.0
return
end
function test_NLparameter_set_value()
model = Model()
@NLparameter(model, p == 1.0)
JuMP.set_value(p, 10.0)
@test JuMP.value(p) == 10.0
return
end
function test_NLconstraints()
model = Model()
@variable(model, 0 <= x <= 1)
@variable(model, y[1:3])
@objective(model, Max, x)
@NLconstraints(model, begin
ref[i = 1:3], y[i] == 0
x + y[1] * y[2] * y[3] <= 0.5
end)
@test JuMP.num_nonlinear_constraints(model) == 4
evaluator = JuMP.NLPEvaluator(model)
MOI.initialize(evaluator, [:ExprGraph])
for i in 1:3
@test MOI.constraint_expr(evaluator, i) ==
:(x[$(y[i].index)] - 0.0 == 0.0)
end
@test MOI.constraint_expr(evaluator, 4) == :(
(
x[$(x.index)] +
x[$(y[1].index)] * x[$(y[2].index)] * x[$(y[3].index)]
) - 0.5 <= 0.0
)
return
end
# This covers the code that computes Hessians in odd chunks of Hess-vec
# products.
function test_dense_Hessian()
m = Model()
@variable(m, x[1:18])
@NLobjective(m, Min, prod(x[i] for i in 1:18))
d = JuMP.NLPEvaluator(m)
MOI.initialize(d, [:Hess])
hessian_sparsity = MOI.hessian_lagrangian_structure(d)
V = zeros(length(hessian_sparsity))
values = ones(18)
MOI.eval_hessian_lagrangian(d, V, values, 1.0, Float64[])
@test _dense_hessian(hessian_sparsity, V, 18) ≈
ones(18, 18) - LinearAlgebra.diagm(0 => ones(18))
values[1] = 0.5
MOI.eval_hessian_lagrangian(d, V, values, 1.0, Float64[])
@test _dense_hessian(hessian_sparsity, V, 18) ≈ [
0 ones(17)'
ones(17) (ones(17, 17)-LinearAlgebra.diagm(0 => ones(17)))/2
]
return
end
function test_eval_objective_and_eval_objective_gradient()
m = Model()
@variable(m, x[1:4])
@NLparameter(m, p == 2)
@NLexpression(m, ex, p * x[1])
ψ(x) = sin(x)
t(x, y) = x + 3y
JuMP.register(m, :ψ, 1, ψ; autodiff = true)
JuMP.register(m, :t, 2, t; autodiff = true)
@NLobjective(m, Min, ex / 2 + sin(x[2]) / ψ(x[2]) + t(x[3], x[4]))
d = JuMP.NLPEvaluator(m)
MOI.initialize(d, [:Grad])
variable_values = fill(2.0, (4,))
@test MOI.eval_objective(d, variable_values) ≈
variable_values[1] + 1 + variable_values[3] + 3variable_values[4]
grad = zeros(4)
MOI.eval_objective_gradient(d, grad, variable_values)
@test grad ≈ [1.0, 0.0, 1.0, 3.0]
return
end
function test_eval_constraint_and_jacobians()
m = Model()
@variable(m, x[1:4])
@NLparameter(m, p == 2)
@NLexpression(m, ex, p * x[1])
ψ(x) = sin(x)
t(x, y) = x + 3y
JuMP.register(m, :ψ, 1, ψ; autodiff = true)
JuMP.register(m, :t, 2, t; autodiff = true)
@NLconstraint(m, Min, ex / 2 + sin(x[2]) / ψ(x[2]) + t(x[3], x[4]) <= 0.0)
d = JuMP.NLPEvaluator(m)
MOI.initialize(d, [:Jac])
variable_values = fill(2.0, (4,))
constraint_value = zeros(1)
MOI.eval_constraint(d, constraint_value, variable_values)
@test constraint_value[1] ≈
variable_values[1] + 1 + variable_values[3] + 3variable_values[4]
jacobian_sparsity = MOI.jacobian_structure(d)
I = [i for (i, j) in jacobian_sparsity]
J = [j for (i, j) in jacobian_sparsity]
@test all(I .== 1)
jac_nonzeros = zeros(length(J))
MOI.eval_constraint_jacobian(d, jac_nonzeros, variable_values)
jac_values = zeros(4)
jac_values[J] = jac_nonzeros
@test jac_values ≈ [1.0, 0.0, 1.0, 3.0]
return
end
function test_non_macro_nonlinear_functions()
model = Model()
@variable(model, x)
@variable(model, y)
@expression(model, aff, x + 2y - 3)
@expression(model, quad, x^2 + 2y^2 - x)
nlexpr = JuMP.add_nonlinear_expression(model, :($x^2 + $y^2))
JuMP.set_nonlinear_objective(model, MIN_SENSE, :(2 * $nlexpr))
JuMP.add_nonlinear_constraint(model, :($x + $y <= 1))
JuMP.add_nonlinear_constraint(model, :($x + $y >= 1))
JuMP.add_nonlinear_constraint(model, :($x + $y == 1))
JuMP.add_nonlinear_constraint(model, :(0 <= $x + $y <= 1))
JuMP.add_nonlinear_constraint(model, :($aff == 1))
JuMP.add_nonlinear_constraint(model, :($quad == 1))
d = JuMP.NLPEvaluator(model)
MOI.initialize(d, [:ExprGraph])
xidx = x.index
yidx = y.index
@test MOI.objective_expr(d) == :(2.0 * (x[$xidx]^2.0 + x[$yidx]^2.0))
@test MOI.constraint_expr(d, 1) == :((x[$xidx] + x[$yidx]) - 1.0 <= 0.0)
@test MOI.constraint_expr(d, 2) == :((x[$xidx] + x[$yidx]) - 1.0 >= 0.0)
@test MOI.constraint_expr(d, 3) == :((x[$xidx] + x[$yidx]) - 1.0 == 0.0)
@test MOI.constraint_expr(d, 4) == :(0.0 <= x[$xidx] + x[$yidx] <= 1.0)
@test MOI.constraint_expr(d, 5) ==
:((-3.0 + x[$xidx] + 2.0 * x[$yidx]) - 1.0 == 0.0)
@test MOI.constraint_expr(d, 6) == :(
(-1.0 * x[$xidx] + x[$xidx] * x[$xidx] + x[$yidx] * x[$yidx] * 2.0) -
1.0 == 0.0
)
return
end
function test_views_on_Hessian_functions()
m = Model()
@variable(m, a)
@variable(m, b)
@variable(m, c)
@NLexpression(m, foo, a * b + c^2)
@NLobjective(m, Min, foo)
d = JuMP.NLPEvaluator(m)
MOI.initialize(d, [:Hess, :HessVec])
hessian_sparsity = MOI.hessian_lagrangian_structure(d)
V = zeros(4)
values = [1.0, 2.0, 3.0] # Values for a, b, and c, respectively.
MOI.eval_hessian_lagrangian(d, V, values, 1.0, Float64[])
h = ones(3)
v = [2.4; 3.5; 4.6]
MOI.eval_hessian_lagrangian_product(d, h, values, v, 1.0, Float64[])
values2 = zeros(10)
values2[5:7] = values
values_view = @view values2[5:7]
V2 = zeros(10)
V_view = @view V2[4:7]
MOI.eval_hessian_lagrangian(d, V_view, values_view, 1.0, Float64[])
@test V_view == V
h2 = zeros(10)
h_view = @view h2[3:5]
v2 = zeros(10)
v2[4:6] = v
v_view = @view v2[4:6]
MOI.eval_hessian_lagrangian_product(
d,
h_view,
values_view,
v_view,
1.0,
Float64[],
)
@test h_view == h
return
end
function test_constant_expressions()
model = Model()
@variable(model, x)
@NLexpression(model, expr, 10)
@NLobjective(model, Min, expr + x)
d = JuMP.NLPEvaluator(model)
MOI.initialize(d, [:Grad])
grad = zeros(1)
MOI.eval_objective_gradient(d, grad, [2.0])
@test grad == [1.0]
return
end
function test_user_defined_function_with_variable_closure()
model = Model()
@variable(model, x[1:2])