-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathread.jl
More file actions
1095 lines (1032 loc) · 27.3 KB
/
read.jl
File metadata and controls
1095 lines (1032 loc) · 27.3 KB
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) 2017: Miles Lubin and contributors
# Copyright (c) 2017: Google Inc.
#
# Use of this source code is governed by an MIT-style license that can be found
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.
module TestNonlinearRead
using Test
import MathOptInterface as MOI
import MathOptInterface.FileFormats: NL
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 test_next_int()
model = NL._CacheModel()
for i in (1, 9, 10, 11, 99)
io = IOBuffer()
print(io, " $i\n")
seekstart(io)
@test NL._next(Int, io, model) == i
@test peek(io) == UInt8('\n')
end
return
end
function test_next_float()
model = NL._CacheModel()
for (s, x) in [
("-1", -1.0),
("1", 1.0),
("-2.1e+1", -21.0),
("2.1e+1", 21.0),
("-2.1e-01", -0.21),
("2.1e-01", 0.21),
("-2.1E-01", -0.21),
("2.1E-01", 0.21),
("-20.1e+0", -20.1),
("20.1e+00", 20.1),
(" 20.1e+00", 20.1),
]
io = IOBuffer()
print(io, "$s\n")
seekstart(io)
@test NL._next(Float64, io, model) == x
@test peek(io) == UInt8('\n')
end
return
end
function test_parse_expr()
model = NL._CacheModel()
io = IOBuffer()
write(io, "o2\nv0\no2\nn2\no2\nv3\nv1\n")
# (* x1 (* 2 (* x4 x2)))
seekstart(io)
x = MOI.VariableIndex.(1:4)
@test NL._parse_expr(io, model) ==
:(*($(x[1]), *(2.0, *($(x[4]), $(x[2])))))
@test eof(io)
return
end
function test_parse_expr_nary()
model = NL._CacheModel()
io = IOBuffer()
write(io, "o54\n4\no5\nv0\nn2\no5\nv2\nn2\no5\nv3\nn2\no5\nv1\nn2\n")
seekstart(io)
x = MOI.VariableIndex.(1:4)
@test NL._parse_expr(io, model) ==
:(+($(x[1])^2.0, $(x[3])^2.0, $(x[4])^2.0, $(x[2])^2.0))
@test eof(io)
return
end
function test_parse_expr_minimum()
model = NL._CacheModel()
io = IOBuffer()
write(io, "o11\n3\nv0\nv1\nv2\n")
seekstart(io)
x = MOI.VariableIndex.(1:3)
@test NL._parse_expr(io, model) == :(min($(x[1]), $(x[2]), $(x[3])))
@test eof(io)
return
end
function test_parse_expr_maximum()
model = NL._CacheModel()
io = IOBuffer()
write(io, "o12\n3\nv0\nv1\nv2\n")
seekstart(io)
x = MOI.VariableIndex.(1:3)
@test NL._parse_expr(io, model) == :(max($(x[1]), $(x[2]), $(x[3])))
@test eof(io)
return
end
function test_parse_header_unsupported_mode()
model = NL._CacheModel()
NL._resize_variables(model, 4)
io = IOBuffer()
write(io, "z3 1 1 0\n")
seekstart(io)
@test_throws(
ErrorException("Unable to parse NL file : unsupported mode z"),
NL._parse_header(io, model),
)
return
end
function test_parse_expr_atan2()
model = NL._CacheModel()
io = IOBuffer()
write(io, "o48\nv0\nv1\n")
seekstart(io)
x = MOI.VariableIndex.(1:2)
@test NL._parse_expr(io, model) == :(atan($(x[1]), $(x[2])))
@test eof(io)
return
end
function test_parse_expr_atan()
model = NL._CacheModel()
io = IOBuffer()
write(io, "o49\nv0\n")
seekstart(io)
x = MOI.VariableIndex.(1:1)
@test NL._parse_expr(io, model) == :(atan($(x[1])))
@test eof(io)
return
end
function test_parse_header_assertion_errors()
model = NL._CacheModel()
for header in [
"g4 1 1 0\n3 3 2 0 0 0\n",
"g4 1 1 0\n3 3 1 0 0 0\n0 2\n",
"g4 1 1 0\n3 3 1 0 0 0\n0 0\n1 0\n",
"g4 1 1 0\n3 3 1 0 0 0\n0 0\n0 1\n",
"g3 1 1 0\n4 2 1 0 1 0\n2 1\n0 0\n4 0 0\n1 0 0 1\n",
"g3 1 1 0\n4 2 1 0 1 0\n2 1\n0 0\n4 0 0\n0 1 0 1\n",
# "g3 1 1 0\n4 2 1 0 1 0\n2 1\n0 0\n4 0 0\n0 0 1 1\n",
"g3 1 1 0\n4 2 1 0 1 0\n2 1\n0 0\n4 0 0\n0 0 0 1\n0 0 0 2 0\n-1 0\n",
"g3 1 1 0\n4 2 1 0 1 0\n2 1\n0 0\n4 0 0\n0 0 0 1\n0 0 0 2 0\n0 -1\n",
]
io = IOBuffer()
write(io, header)
seekstart(io)
@test_throws(AssertionError, NL._parse_header(io, model))
end
return
end
function test_parse_header_common_expressions()
model = NL._CacheModel()
err = ErrorException(
"Unable to parse NL file : we don't support common exprs",
)
for header in [
"g3 1 1 0\n4 2 1 0 1 0\n2 1\n0 0\n4 0 0\n0 0 0 1\n0 0 0 2 0\n8 4\n0 0\n1 0 0 0 0\n",
"g3 1 1 0\n4 2 1 0 1 0\n2 1\n0 0\n4 0 0\n0 0 0 1\n0 0 0 2 0\n8 4\n0 0\n0 1 0 0 0\n",
"g3 1 1 0\n4 2 1 0 1 0\n2 1\n0 0\n4 0 0\n0 0 0 1\n0 0 0 2 0\n8 4\n0 0\n0 0 1 0 0\n",
"g3 1 1 0\n4 2 1 0 1 0\n2 1\n0 0\n4 0 0\n0 0 0 1\n0 0 0 2 0\n8 4\n0 0\n0 0 0 1 0\n",
"g3 1 1 0\n4 2 1 0 1 0\n2 1\n0 0\n4 0 0\n0 0 0 1\n0 0 0 2 0\n8 4\n0 0\n0 0 0 0 1\n",
]
io = IOBuffer()
write(io, header)
seekstart(io)
@test_throws(err, NL._parse_header(io, model))
end
return
end
function test_parse_y_error()
model = NL._CacheModel()
NL._resize_variables(model, 4)
io = IOBuffer()
write(io, "y\n")
seekstart(io)
@test_throws(
ErrorException("Unable to parse NL file: unhandled header y"),
NL._parse_section(io, model),
)
return
end
function test_parse_O()
model = NL._CacheModel()
NL._resize_variables(model, 4)
io = IOBuffer()
write(
io,
"""
O0 0# can stick a comment anywhere
o2
v0 # can stick a comment anywhere
o2
v2
o2
v3
v1
""",
)
seekstart(io)
NL._parse_section(io, model)
@test eof(io)
@test model.sense == MOI.MIN_SENSE
x = MOI.VariableIndex.(1:4)
@test model.objective == :($(x[1]) * ($(x[3]) * ($(x[4]) * $(x[2]))))
return
end
function test_parse_O_max()
model = NL._CacheModel()
NL._resize_variables(model, 4)
io = IOBuffer()
write(io, "O0 1\nv0\n")
seekstart(io)
NL._parse_section(io, model)
@test eof(io)
@test model.sense == MOI.MAX_SENSE
x = MOI.VariableIndex(1)
@test model.objective == :(+$x)
return
end
function test_parse_x()
model = NL._CacheModel()
NL._resize_variables(model, 5)
io = IOBuffer()
write(
io,
"""
x3
0 1.1# can stick a comment anywhere
3 2.2
2 3.3 # can stick a comment anywhere
""",
)
seekstart(io)
NL._parse_section(io, model)
@test eof(io)
@test model.variable_primal == [1.1, 0.0, 3.3, 2.2, 0.0]
return
end
function test_parse_d()
model = NL._CacheModel()
io = IOBuffer()
write(
io,
"""
d3
0 1.1# can stick a comment anywhere
3 2.2
2 3.3 # can stick a comment anywhere
""",
)
seekstart(io)
NL._parse_section(io, model)
@test eof(io)
return
end
function test_parse_r()
model = NL._CacheModel()
NL._resize_constraints(model, 5)
io = IOBuffer()
write(
io,
"""
r# can stick a comment anywhere
1 3.3
3
0 1.1 2.2 # can stick a comment anywhere
4 5.5
2 4.4# can stick a comment anywhere
""",
)
seekstart(io)
NL._parse_section(io, model)
@test eof(io)
@test model.constraint_lower == [-Inf, -Inf, 1.1, 5.5, 4.4]
@test model.constraint_upper == [3.3, Inf, 2.2, 5.5, Inf]
return
end
function test_parse_b()
model = NL._CacheModel()
NL._resize_variables(model, 5)
io = IOBuffer()
write(
io,
"""
b# can stick a comment anywhere
1 3.3
3# can stick a comment anywhere
0 1.1 2.2
4 5.5
2 4.4 # can stick a comment anywhere
""",
)
seekstart(io)
NL._parse_section(io, model)
@test eof(io)
@test model.variable_lower == [-Inf, -Inf, 1.1, 5.5, 4.4]
@test model.variable_upper == [3.3, Inf, 2.2, 5.5, Inf]
return
end
function test_parse_k()
model = NL._CacheModel()
NL._resize_variables(model, 3)
io = IOBuffer()
write(
io,
"""
k2
2 # can stick a comment anywhere
4
""",
)
seekstart(io)
NL._parse_section(io, model)
@test eof(io)
return
end
function test_parse_J()
model = NL._CacheModel()
NL._resize_constraints(model, 3)
io = IOBuffer()
write(
io,
"""
J1 2 # can stick a comment anywhere
2 1.1
3 2.2 # can stick a comment anywhere
""",
)
seekstart(io)
NL._parse_section(io, model)
@test eof(io)
@test model.constraints[1] == :()
x, y = MOI.VariableIndex(3), MOI.VariableIndex(4)
@test model.constraints[2] == :(1.1 * $x + 2.2 * $y)
@test model.constraints[3] == :()
return
end
function test_parse_J_zeros()
model = NL._CacheModel()
NL._resize_constraints(model, 1)
io = IOBuffer()
write(
io,
"""
J0 2
0 0
1 0
""",
)
seekstart(io)
NL._parse_section(io, model)
@test eof(io)
@test model.constraints[1] == :()
return
end
function test_parse_C_J()
model = NL._CacheModel()
NL._resize_constraints(model, 1)
io = IOBuffer()
write(
io,
"""
C0
o2
v0
v1
J0 2
0 1.1
1 2.2
""",
)
seekstart(io)
NL._parse_section(io, model)
NL._parse_section(io, model)
@test eof(io)
x, y = MOI.VariableIndex(1), MOI.VariableIndex(2)
@test model.constraints[1] == :((1.1 * $x + 2.2 * $y) + $x * $y)
return
end
function test_parse_J_C()
model = NL._CacheModel()
NL._resize_constraints(model, 1)
io = IOBuffer()
write(
io,
"""
J0 2
0 1.1
1 2.2
C0
o2
v0
v1
""",
)
seekstart(io)
NL._parse_section(io, model)
NL._parse_section(io, model)
@test eof(io)
x, y = MOI.VariableIndex(1), MOI.VariableIndex(2)
@test model.constraints[1] == :((1.1 * $x + 2.2 * $y) + $x * $y)
return
end
function test_parse_G()
model = NL._CacheModel()
NL._resize_constraints(model, 3)
io = IOBuffer()
write(
io,
"""
G0 2 # can stick a comment anywhere
2 1.1 # can stick a comment anywhere
3 2.2
""",
)
seekstart(io)
NL._parse_section(io, model)
@test eof(io)
x, y = MOI.VariableIndex(3), MOI.VariableIndex(4)
@test model.objective == :(1.1 * $x + 2.2 * $y)
return
end
function test_parse_G_zeros()
model = NL._CacheModel()
NL._resize_constraints(model, 3)
io = IOBuffer()
write(
io,
"""
G0 3
0 0
1 0
2 0
""",
)
seekstart(io)
NL._parse_section(io, model)
@test eof(io)
@test model.objective == :()
return
end
function test_parse_O_G()
model = NL._CacheModel()
NL._resize_variables(model, 4)
io = IOBuffer()
write(
io,
"""
O0 0
o2
v0
v1
G0 2
0 1.1
1 2.2
""",
)
seekstart(io)
NL._parse_section(io, model)
NL._parse_section(io, model)
@test eof(io)
x, y = MOI.VariableIndex(1), MOI.VariableIndex(2)
@test model.objective == :((1.1 * $x + 2.2 * $y) + $x * $y)
return
end
function test_parse_G_O()
model = NL._CacheModel()
NL._resize_variables(model, 4)
io = IOBuffer()
write(
io,
"""
G0 2
0 -1.1
1 2.2e+01
O0 0
o2
v0
v1
""",
)
seekstart(io)
NL._parse_section(io, model)
NL._parse_section(io, model)
@test eof(io)
x, y = MOI.VariableIndex(1), MOI.VariableIndex(2)
@test model.objective == :((-1.1 * $x + 22.0 * $y) + $x * $y)
return
end
function test_parse_F()
model = NL._CacheModel()
io = IOBuffer()
write(io, "F")
seekstart(io)
@test_throws(
ErrorException(
"Unable to parse NL file: imported function descriptions ('F' " *
"sections) are not yet supported. To request support, please open an " *
"issue at https://github.com/jump-dev/MathOptInterface.jl with a " *
"reproducible example.",
),
NL._parse_section(io, model),
)
return
end
function test_parse_V()
model = NL._CacheModel()
io = IOBuffer()
write(io, "V")
seekstart(io)
@test_throws(
ErrorException(
"Unable to parse NL file: defined variable definitions ('V' sections)" *
" are not yet supported. To request support, please open an issue at " *
"https://github.com/jump-dev/MathOptInterface.jl with a reproducible " *
"example.",
),
NL._parse_section(io, model),
)
return
end
function test_parse_L()
model = NL._CacheModel()
io = IOBuffer()
write(io, "L")
seekstart(io)
@test_throws(
ErrorException(
"Unable to parse NL file: logical constraints ('L' sections) are not " *
"yet supported. To request support, please open an issue at " *
"https://github.com/jump-dev/MathOptInterface.jl with a reproducible " *
"example.",
),
NL._parse_section(io, model),
)
return
end
function test_parse_S()
model = NL._CacheModel()
io = IOBuffer()
write(
io,
"""
S0 8 zork
0 2
1 6
2 7
3 8
4 9
5 3
6 5
8 4
""",
)
seekstart(io)
@test_logs(
(:warn, "Skipping suffix: `S0 8 zork`"),
NL._parse_section(io, model),
)
@test eof(io)
return
end
function test_parse_S_Float64()
model = NL._CacheModel()
io = IOBuffer()
write(
io,
"""
S4 8 zork
0 2.0
1 6.0
2 7.0
3 8.0
4 9.0
5 3.0
6 5.0
8 4.0
""",
)
seekstart(io)
@test_logs(
(:warn, "Skipping suffix: `S4 8 zork`"),
NL._parse_section(io, model),
)
@test eof(io)
return
end
function test_hs071()
model = NL.Model()
open(joinpath(@__DIR__, "data", "hs071.nl"), "r") do io
return read!(io, model)
end
model_print = sprint(print, model)
_in_ = @static Sys.iswindows() ? "in" : "∈"
for line in [
"(1.1 * v[1] + 1.4 * v[2] + 1.2 * v[3] + 1.3 * v[4]) + +2.0",
"v[1] >= 1.1",
"v[2] >= 1.4",
"v[3] >= 1.2",
"v[4] >= 1.3",
"v[1] <= 5.1",
"v[2] <= 5.4",
"v[3] <= 1.0",
"v[4] <= 5.3",
"v[3] $_in_ ℤ",
"v[4] $_in_ ℤ",
"v[1] * (v[3] * (v[4] * v[2])) >= 25.0",
"v[1] ^ 2.0 + v[3] ^ 2.0 + v[4] ^ 2.0 + v[2] ^ 2.0 == 40.0",
]
@test occursin(line, model_print)
end
return
end
function test_parse_header_integrality_obj()
model = NL._CacheModel()
NL._resize_variables(model, 4)
io = IOBuffer()
write(
io,
"""g3 0 1 0 # problem test_simple
2 1 1 0 0 # vars, constraints, objectives, ranges, eqns
1 1 # nonlinear constraints, objectives
0 0 # network constraints: nonlinear, linear
1 2 0 # nonlinear vars in constraints, objectives, both
0 0 0 1 # linear network variables; functions; arith, flags
0 0 0 0 0 # discrete variables: binary, integer, nonlinear (b,c,o)
1 1 # nonzeros in Jacobian, gradients
0 0 # max name lengths: constraints, variables
0 0 0 0 0 # common exprs: b,c,o,c1,o1\n""",
)
seekstart(io)
NL._parse_header(io, model)
@test model.variable_type[1] == NL._CONTINUOUS
@test model.variable_type[2] == NL._CONTINUOUS
return
end
function test_parse_header_integrality_obj_int()
model = NL._CacheModel()
NL._resize_variables(model, 4)
io = IOBuffer()
write(
io,
"""g3 0 1 0 # problem test_simple
2 1 1 0 0 # vars, constraints, objectives, ranges, eqns
1 1 # nonlinear constraints, objectives
0 0 # network constraints: nonlinear, linear
1 2 0 # nonlinear vars in constraints, objectives, both
0 0 0 1 # linear network variables; functions; arith, flags
0 0 0 0 1 # discrete variables: binary, integer, nonlinear (b,c,o)
1 1 # nonzeros in Jacobian, gradients
0 0 # max name lengths: constraints, variables
0 0 0 0 0 # common exprs: b,c,o,c1,o1\n""",
)
seekstart(io)
NL._parse_header(io, model)
@test model.variable_type[1] == NL._CONTINUOUS
@test model.variable_type[2] == NL._INTEGER
return
end
function test_parse_header_integrality_both()
model = NL._CacheModel()
NL._resize_variables(model, 4)
io = IOBuffer()
write(
io,
"""g3 0 1 0 # problem test_simple
2 1 1 0 0 # vars, constraints, objectives, ranges, eqns
1 1 # nonlinear constraints, objectives
0 0 # network constraints: nonlinear, linear
2 1 1 # nonlinear vars in constraints, objectives, both
0 0 0 1 # linear network variables; functions; arith, flags
0 0 0 0 0 # discrete variables: binary, integer, nonlinear (b,c,o)
1 1 # nonzeros in Jacobian, gradients
0 0 # max name lengths: constraints, variables
0 0 0 0 0 # common exprs: b,c,o,c1,o1\n""",
)
seekstart(io)
NL._parse_header(io, model)
@test model.variable_type[1] == NL._CONTINUOUS
@test model.variable_type[2] == NL._CONTINUOUS
return
end
function test_parse_header_integrality_both_int()
model = NL._CacheModel()
NL._resize_variables(model, 4)
io = IOBuffer()
write(
io,
"""g3 0 1 0 # problem test_simple
2 1 1 0 0 # vars, constraints, objectives, ranges, eqns
1 1 # nonlinear constraints, objectives
0 0 # network constraints: nonlinear, linear
2 1 1 # nonlinear vars in constraints, objectives, both
0 0 0 1 # linear network variables; functions; arith, flags
0 0 1 0 0 # discrete variables: binary, integer, nonlinear (b,c,o)
1 1 # nonzeros in Jacobian, gradients
0 0 # max name lengths: constraints, variables
0 0 0 0 0 # common exprs: b,c,o,c1,o1\n""",
)
seekstart(io)
NL._parse_header(io, model)
@test model.variable_type[1] == NL._INTEGER
@test model.variable_type[2] == NL._CONTINUOUS
return
end
function test_hs071_free_constraint_nlexpr()
model = NL.Model(; use_nlp_block = false)
open(joinpath(@__DIR__, "data", "hs071_free_constraint.nl"), "r") do io
return read!(io, model)
end
types = MOI.get(model, MOI.ListOfConstraintTypesPresent())
@test length(types) == 2
@test (MOI.ScalarAffineFunction{Float64}, MOI.GreaterThan{Float64}) in types
@test (MOI.ScalarNonlinearFunction, MOI.Interval{Float64}) in types
for (F, S) in MOI.get(model, MOI.ListOfConstraintTypesPresent())
@test MOI.get(model, MOI.NumberOfConstraints{F,S}()) == 1
end
F, S = MOI.ScalarNonlinearFunction, MOI.Interval{Float64}
ci = first(MOI.get(model, MOI.ListOfConstraintIndices{F,S}()))
@test MOI.get(model, MOI.ConstraintSet(), ci) == MOI.Interval(-Inf, Inf)
return
end
function test_hs071_free_constraint()
model = NL.Model()
open(joinpath(@__DIR__, "data", "hs071_free_constraint.nl"), "r") do io
return read!(io, model)
end
block = MOI.get(model, MOI.NLPBlock())
@test block.constraint_bounds ==
[MOI.NLPBoundsPair(25.0, Inf), MOI.NLPBoundsPair(-Inf, Inf)]
return
end
function test_no_objective()
model = NL.Model()
open(joinpath(@__DIR__, "data", "hs071_no_objective.nl"), "r") do io
return read!(io, model)
end
@test MOI.get(model, MOI.NumberOfVariables()) == 4
@test MOI.get(model, MOI.ObjectiveSense()) == MOI.FEASIBILITY_SENSE
return
end
"""
test_mac_minlp()
This function tests reading the files in Sven Leyffer's MINLP testset, which is
available at: https://wiki.mcs.anl.gov/leyffer/index.php/MacMINLP.
If the folder isn't available locally, the test is skipped.
"""
function test_mac_minlp()
dir = joinpath(@__DIR__, "MacMINLP")
if !isdir(dir)
return
end
exclude = [
# Has subexpressions
"space-25-r.nl",
"space-960-ir.nl",
"space-960-r.nl",
"top1-15x05.nl",
]
for file in filter(f -> !(f in exclude) && endswith(f, ".nl"), readdir(dir))
model = NL.Model()
open(joinpath(dir, file), "r") do io
return read!(io, model)
end
end
return
end
function test_nl_read_scalar_affine_function()
for obj_fn in (x -> x, x -> 1.0 * x, x -> 2.0 * x + 3.0)
src = MOI.Utilities.Model{Float64}()
x = MOI.add_variable(src)
MOI.set(src, MOI.ObjectiveSense(), MOI.MIN_SENSE)
f = obj_fn(x)
MOI.set(src, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.add_constraint(src, f, MOI.LessThan(1.0))
dest = MOI.FileFormats.NL.Model()
MOI.copy_to(dest, src)
io = IOBuffer()
write(io, dest)
input = MOI.FileFormats.NL.Model(; use_nlp_block = false)
seekstart(io)
read!(io, input)
model = MOI.Utilities.Model{Float64}()
MOI.copy_to(model, input)
y = only(MOI.get(model, MOI.ListOfVariableIndices()))
g = MOI.Utilities.substitute_variables(
_ -> y,
convert(MOI.ScalarAffineFunction{Float64}, f),
)
obj = MOI.get(model, MOI.ObjectiveFunction{typeof(g)}())
@test ≈(obj, g)
@test MOI.get(model, MOI.ListOfConstraintTypesPresent()) ==
[(typeof(f), MOI.LessThan{Float64})]
end
return
end
function test_binary_next_Float64()
model = NL._CacheModel()
model.is_binary = true
sequence = [0.0, 1.0, -2.0, 1.234, 1e-12]
io = IOBuffer()
for s in sequence
write(io, s)
end
seekstart(io)
for s in sequence
@test NL._next(Float64, io, model) === s
end
return
end
function test_binary_next_Int()
model = NL._CacheModel()
model.is_binary = true
sequence = Int32[0, 1, 2, -3, -5, typemax(Int32), typemin(Int32)]
io = IOBuffer()
for s in sequence
write(io, s)
end
seekstart(io)
for s in sequence
@test NL._next(Int, io, model) === Int(s)
end
return
end
function test_binary_read_til_newline()
model = NL._CacheModel()
model.is_binary = true
io = IOBuffer()
@test NL._read_til_newline(io, model) === nothing
@test position(io) == 0
return
end
function test_parse_header_binary()
model = NL._CacheModel()
io = IOBuffer()
write(
io,
"""b3 0 1 0 # problem test_simple
2 1 1 0 0 # vars, constraints, objectives, ranges, eqns
1 1 # nonlinear constraints, objectives
0 0 # network constraints: nonlinear, linear
1 2 0 # nonlinear vars in constraints, objectives, both
0 0 1 1 # linear network variables; functions; arith, flags
0 0 0 0 0 # discrete variables: binary, integer, nonlinear (b,c,o)
1 1 # nonzeros in Jacobian, gradients
0 0 # max name lengths: constraints, variables
0 0 0 0 0 # common exprs: b,c,o,c1,o1
""",
)
seekstart(io)
NL._parse_header(io, model)
@test model.is_binary
return
end
function test_binary_parse_O()
model = NL._CacheModel()
model.is_binary = true
NL._resize_variables(model, 4)
io = IOBuffer()
write(io, Cchar('O'), Int32(0), Int32(0))
write(io, Cchar('o'), Int32(2))
write(io, Cchar('v'), Int32(0))
write(io, Cchar('o'), Int32(2))
write(io, Cchar('v'), Int32(2))
write(io, Cchar('o'), Int32(2))
write(io, Cchar('v'), Int32(3))
write(io, Cchar('v'), Int32(1))
seekstart(io)
NL._parse_section(io, model)
@test eof(io)
@test model.sense == MOI.MIN_SENSE
x = MOI.VariableIndex.(1:4)
@test model.objective == :($(x[1]) * ($(x[3]) * ($(x[4]) * $(x[2]))))
return
end
function test_binary_parse_O_max()
model = NL._CacheModel()
model.is_binary = true
NL._resize_variables(model, 4)
io = IOBuffer()
write(io, Cchar('O'), Int32(0), Int32(1), Cchar('v'), Int32(0))
seekstart(io)
NL._parse_section(io, model)
@test eof(io)
@test model.sense == MOI.MAX_SENSE
x = MOI.VariableIndex(1)
@test model.objective == :(+$x)
return
end
function test_binary_parse_x()
model = NL._CacheModel()
model.is_binary = true
NL._resize_variables(model, 5)
io = IOBuffer()
write(io, Cchar('x'), Int32(3))
write(io, Int32(0), 1.1)
write(io, Int32(3), 2.2)
write(io, Int32(2), 3.3)
seekstart(io)
NL._parse_section(io, model)
@test eof(io)
@test model.variable_primal == [1.1, 0.0, 3.3, 2.2, 0.0]
return
end
function test_parse_d()
model = NL._CacheModel()
model.is_binary = true
io = IOBuffer()
write(io, Cchar('d'), Int32(3))
write(io, Int32(0), 1.1)
write(io, Int32(3), 2.2)
write(io, Int32(2), 3.3)
seekstart(io)
NL._parse_section(io, model)
@test eof(io)
return
end
function test_binary_parse_r()
model = NL._CacheModel()
model.is_binary = true
NL._resize_constraints(model, 5)
io = IOBuffer()
write(io, Cchar('r'))
write(io, Cchar('1'), 3.3)
write(io, Cchar('3'))
write(io, Cchar('0'), 1.1, 2.2)
write(io, Cchar('4'), 5.5)
write(io, Cchar('2'), 4.4)
seekstart(io)
NL._parse_section(io, model)
@test eof(io)
@test model.constraint_lower == [-Inf, -Inf, 1.1, 5.5, 4.4]
@test model.constraint_upper == [3.3, Inf, 2.2, 5.5, Inf]
return
end
function test_binary_parse_b()
model = NL._CacheModel()
model.is_binary = true
NL._resize_variables(model, 5)
io = IOBuffer()
write(io, Cchar('b'))
write(io, Cchar('1'), 3.3)
write(io, Cchar('3'))
write(io, Cchar('0'), 1.1, 2.2)
write(io, Cchar('4'), 5.5)