forked from jump-dev/JuMP.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint.jl
766 lines (685 loc) · 24.6 KB
/
print.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
# 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/print.jl
# Testing $fa pretty-printing-related functionality
#############################################################################
using JuMP
using LinearAlgebra
using Test
import JuMP.IJuliaMode
import JuMP.REPLMode
@static if !(:JuMPExtension in names(Main))
include(joinpath(@__DIR__, "JuMPExtension.jl"))
end
# Helper function to test IO methods work correctly
function io_test(mode, obj, exp_str; repl = :both)
if mode == REPLMode
repl != :show && @test sprint(print, obj) == exp_str
repl != :print && @test sprint(show, obj) == exp_str
else
@test sprint(show, "text/latex", obj) ==
string("\$\$ ", exp_str, " \$\$")
end
end
# Used to test that JuMP printing works correctly for types for which
# oneunit is not convertible to Float64
struct UnitNumber <: Number
α::Float64
end
Base.zero(::Union{UnitNumber,Type{UnitNumber}}) = UnitNumber(0.0)
Base.oneunit(::Union{UnitNumber,Type{UnitNumber}}) = UnitNumber(1.0)
Base.:(+)(u::UnitNumber, v::UnitNumber) = UnitNumber(u.α + v.α)
Base.:(-)(u::UnitNumber, v::UnitNumber) = UnitNumber(u.α - v.α)
Base.:(*)(α::Float64, u::UnitNumber) = UnitNumber(α * u.α)
Base.abs(u::UnitNumber) = UnitNumber(abs(u.α))
Base.isless(u::UnitNumber, v::UnitNumber) = isless(u.α, v.α)
# Used to test extensibility of JuMP printing for `JuMP.AbstractConstraint`
struct CustomConstraint{S<:JuMP.AbstractShape} <: JuMP.AbstractConstraint
function_str::String
in_set_str::String
shape::S
end
function JuMP.function_string(print_mode, constraint::CustomConstraint)
return constraint.function_str
end
function JuMP.in_set_string(print_mode, constraint::CustomConstraint)
return constraint.in_set_str
end
struct CustomIndex
value::Int
end
function JuMP.add_constraint(
model::JuMP.Model,
constraint::CustomConstraint,
name::String,
)
if !haskey(model.ext, :custom)
model.ext[:custom_constraints] = CustomConstraint[]
model.ext[:custom_names] = String[]
end
constraints = model.ext[:custom_constraints]
push!(constraints, constraint)
push!(model.ext[:custom_names], name)
return JuMP.ConstraintRef(
model,
CustomIndex(length(constraints)),
constraint.shape,
)
end
function JuMP.constraint_object(
cref::JuMP.ConstraintRef{JuMP.Model,CustomIndex},
)
return cref.model.ext[:custom_constraints][cref.index.value]
end
function JuMP.name(cref::JuMP.ConstraintRef{JuMP.Model,CustomIndex})
return cref.model.ext[:custom_names][cref.index.value]
end
@testset "Printing" begin
@testset "expressions" begin
# Most of the expression logic is well covered by test/operator.jl
# This is really just to check IJulia printing for expressions
le = JuMP._math_symbol(REPLMode, :leq)
ge = JuMP._math_symbol(REPLMode, :geq)
#------------------------------------------------------------------
mod = Model()
@variable(mod, x[1:5])
@variable(mod, y[i = 2:4, j = i:5])
@variable(mod, z)
ex = @expression(mod, x[1] + 2 * y[2, 3])
io_test(REPLMode, ex, "x[1] + 2 y[2,3]")
io_test(IJuliaMode, ex, "x_{1} + 2 y_{2,3}")
ex = @expression(mod, x[1] + 2 * y[2, 3] + x[1])
io_test(REPLMode, ex, "2 x[1] + 2 y[2,3]")
io_test(IJuliaMode, ex, "2 x_{1} + 2 y_{2,3}")
# TODO: These tests shouldn't depend on order of the two variables in
# quadratic terms, i.e., x*y vs y*x.
ex = @expression(mod, (x[1] + x[2]) * (y[2, 2] + 3.0))
io_test(REPLMode, ex, "x[1]*y[2,2] + x[2]*y[2,2] + 3 x[1] + 3 x[2]")
io_test(
IJuliaMode,
ex,
"x_{1}\\times y_{2,2} + x_{2}\\times y_{2,2} + 3 x_{1} + 3 x_{2}",
)
ex = @expression(mod, (y[2, 2] + 3.0) * (x[1] + x[2]))
io_test(REPLMode, ex, "y[2,2]*x[1] + y[2,2]*x[2] + 3 x[1] + 3 x[2]")
io_test(
IJuliaMode,
ex,
"y_{2,2}\\times x_{1} + y_{2,2}\\times x_{2} + 3 x_{1} + 3 x_{2}",
)
ex = @expression(mod, (x[1] + x[2]) * (y[2, 2] + 3.0) + z^2 - 1)
repl_sq = JuMP._math_symbol(REPLMode, :sq)
io_test(
REPLMode,
ex,
"x[1]*y[2,2] + x[2]*y[2,2] + z$repl_sq + 3 x[1] + 3 x[2] - 1",
)
ijulia_sq = JuMP._math_symbol(IJuliaMode, :sq)
io_test(
IJuliaMode,
ex,
"x_{1}\\times y_{2,2} + x_{2}\\times y_{2,2} + z$ijulia_sq + 3 x_{1} + 3 x_{2} - 1",
)
ex = @expression(mod, -z * x[1] - x[1] * z + x[1] * x[2] + 0 * z^2)
io_test(REPLMode, ex, "-2 z*x[1] + x[1]*x[2]")
io_test(IJuliaMode, ex, "-2 z\\times x_{1} + x_{1}\\times x_{2}")
ex = z^2 + x[1] - z^2 - x[1]
io_test(REPLMode, ex, "0 z² + 0 x[1]")
io_test(IJuliaMode, ex, "0 z$ijulia_sq + 0 x_{1}")
end
# See https://github.com/jump-dev/JuMP.jl/pull/1352
@testset "Expression of coefficient type with unit" begin
m = Model()
@variable m x
@variable m y
u = UnitNumber(2.0)
aff = JuMP.GenericAffExpr(zero(u), x => u, y => zero(u))
io_test(REPLMode, aff, "UnitNumber(2.0) x + UnitNumber(0.0) y")
io_test(IJuliaMode, aff, "UnitNumber(2.0) x + UnitNumber(0.0) y")
drop_zeros!(aff)
io_test(REPLMode, aff, "UnitNumber(2.0) x")
io_test(IJuliaMode, aff, "UnitNumber(2.0) x")
quad = aff * x
io_test(REPLMode, quad, "UnitNumber(2.0) x² + UnitNumber(0.0)")
io_test(IJuliaMode, quad, "UnitNumber(2.0) x^2 + UnitNumber(0.0)")
end
@testset "Nonlinear expressions" begin
model = Model()
@variable(model, x)
expr = @NLexpression(model, x + 1)
io_test(REPLMode, expr, "\"Reference to nonlinear expression #1\"")
end
@testset "Nonlinear parameters" begin
model = Model()
@NLparameter(model, param == 1.0)
io_test(REPLMode, param, "\"Reference to nonlinear parameter #1\"")
end
@testset "NLPEvaluator" begin
model = Model()
evaluator = JuMP.NLPEvaluator(model)
io_test(REPLMode, evaluator, "\"A JuMP.NLPEvaluator\"")
end
@testset "Nonlinear constraints" begin
le = JuMP._math_symbol(REPLMode, :leq)
ge = JuMP._math_symbol(REPLMode, :geq)
eq = JuMP._math_symbol(REPLMode, :eq)
model = Model()
@variable(model, x)
constr_le = @NLconstraint(model, sin(x) <= 1)
constr_ge = @NLconstraint(model, sin(x) >= 1)
constr_eq = @NLconstraint(model, sin(x) == 1)
constr_range = @NLconstraint(model, 0 <= sin(x) <= 1)
io_test(REPLMode, constr_le, "sin(x) - 1.0 $le 0")
io_test(REPLMode, constr_ge, "sin(x) - 1.0 $ge 0")
io_test(REPLMode, constr_eq, "sin(x) - 1.0 $eq 0")
# Note: This is inconsistent with the "x in [-1, 1]" printing for
# regular constraints.
io_test(REPLMode, constr_range, "0 $le sin(x) $le 1")
io_test(IJuliaMode, constr_le, "sin(x) - 1.0 \\leq 0")
io_test(IJuliaMode, constr_ge, "sin(x) - 1.0 \\geq 0")
io_test(IJuliaMode, constr_eq, "sin(x) - 1.0 = 0")
io_test(IJuliaMode, constr_range, "0 \\leq sin(x) \\leq 1")
end
@testset "Nonlinear constraints with embedded parameters/expressions" begin
le = JuMP._math_symbol(REPLMode, :leq)
model = Model()
@variable(model, x)
expr = @NLexpression(model, x + 1)
@NLparameter(model, param == 1.0)
constr = @NLconstraint(model, expr - param <= 0)
io_test(
REPLMode,
constr,
"(subexpression[1] - parameter[1]) - 0.0 $le 0",
)
io_test(
IJuliaMode,
constr,
"(subexpression_{1} - parameter_{1}) - 0.0 \\leq 0",
)
end
@testset "Custom constraint" begin
model = Model()
function test_constraint(function_str, in_set_str, name)
constraint =
CustomConstraint(function_str, in_set_str, JuMP.ScalarShape())
cref = JuMP.add_constraint(model, constraint, name)
@test string(cref) == "$name : $function_str $in_set_str"
end
test_constraint("fun", "set", "name")
test_constraint("a", "b", "c")
end
end
function printing_test(ModelType::Type{<:JuMP.AbstractModel})
@testset "VariableRef" begin
m = ModelType()
@variable(m, 0 <= x <= 2)
@test JuMP.name(x) == "x"
io_test(REPLMode, x, "x")
io_test(IJuliaMode, x, "x")
JuMP.set_name(x, "x2")
@test JuMP.name(x) == "x2"
io_test(REPLMode, x, "x2")
io_test(IJuliaMode, x, "x2")
JuMP.set_name(x, "")
@test JuMP.name(x) == ""
io_test(REPLMode, x, "noname")
io_test(IJuliaMode, x, "noname")
@variable(m, z[1:2, 3:5])
@test JuMP.name(z[1, 3]) == "z[1,3]"
io_test(REPLMode, z[1, 3], "z[1,3]")
io_test(IJuliaMode, z[1, 3], "z_{1,3}")
@test JuMP.name(z[2, 4]) == "z[2,4]"
io_test(REPLMode, z[2, 4], "z[2,4]")
io_test(IJuliaMode, z[2, 4], "z_{2,4}")
@test JuMP.name(z[2, 5]) == "z[2,5]"
io_test(REPLMode, z[2, 5], "z[2,5]")
io_test(IJuliaMode, z[2, 5], "z_{2,5}")
@variable(m, w[3:9, ["red", "blue", "green"]])
@test JuMP.name(w[7, "green"]) == "w[7,green]"
io_test(REPLMode, w[7, "green"], "w[7,green]")
io_test(IJuliaMode, w[7, "green"], "w_{7,green}")
rng = 2:5
@variable(m, v[rng, rng, rng, rng, rng, rng, rng])
a_v = v[4, 5, 2, 3, 2, 2, 4]
@test JuMP.name(a_v) == "v[4,5,2,3,2,2,4]"
io_test(REPLMode, a_v, "v[4,5,2,3,2,2,4]")
io_test(IJuliaMode, a_v, "v_{4,5,2,3,2,2,4}")
end
@testset "base_name keyword argument" begin
m = ModelType()
@variable(m, x, base_name = "foo")
@variable(m, y[1:3], base_name = "bar")
num = 123
@variable(m, z[[:red, :blue]], base_name = "color_$num")
@variable(m, v[1:2, 1:2], PSD, base_name = string("i", "$num", num))
@variable(m, w[1:3, 1:3], Symmetric, base_name = "symm")
io_test(REPLMode, x, "foo")
io_test(IJuliaMode, x, "foo")
io_test(REPLMode, y[2], "bar[2]")
io_test(IJuliaMode, y[2], "bar_{2}")
io_test(REPLMode, z[:red], "color_123[red]")
io_test(IJuliaMode, z[:red], "color_123_{red}")
io_test(REPLMode, v[2, 1], "i123123[1,2]")
io_test(IJuliaMode, v[2, 1], "i123123_{1,2}")
io_test(REPLMode, w[1, 3], "symm[1,3]")
io_test(IJuliaMode, w[1, 3], "symm_{1,3}")
end
@testset "VectorOfVariable constraints" begin
ge = JuMP._math_symbol(REPLMode, :geq)
in_sym = JuMP._math_symbol(REPLMode, :in)
model = ModelType()
@variable(model, x)
@variable(model, y)
zero_constr = @constraint(model, [x, y] in MOI.Zeros(2))
io_test(
REPLMode,
zero_constr,
"[x, y] $in_sym MathOptInterface.Zeros(2)",
)
# TODO: Test in IJulia mode and do nice printing for Zeros().
end
@testset "Scalar AffExpr constraints" begin
le = JuMP._math_symbol(REPLMode, :leq)
ge = JuMP._math_symbol(REPLMode, :geq)
eq = JuMP._math_symbol(REPLMode, :eq)
in_sym = JuMP._math_symbol(REPLMode, :in)
model = ModelType()
@variable(model, x)
@constraint(model, linear_le, x + 0 <= 1)
@constraint(model, linear_ge, x + 0 >= 1)
@constraint(model, linear_eq, x + 0 == 1)
@constraint(model, linear_range, -1 <= x + 0 <= 1)
linear_noname = @constraint(model, x + 0 <= 1)
io_test(REPLMode, linear_le, "linear_le : x $le 1.0")
io_test(REPLMode, linear_eq, "linear_eq : x $eq 1.0")
io_test(REPLMode, linear_range, "linear_range : x $in_sym [-1.0, 1.0]")
io_test(REPLMode, linear_noname, "x $le 1.0")
# io_test doesn't work here because constraints print with a mix of math
# and non-math.
@test sprint(show, "text/latex", linear_le) ==
"linear_le : \$ x \\leq 1.0 \$"
@test sprint(show, "text/latex", linear_ge) ==
"linear_ge : \$ x \\geq 1.0 \$"
@test sprint(show, "text/latex", linear_eq) ==
"linear_eq : \$ x = 1.0 \$"
@test sprint(show, "text/latex", linear_range) ==
"linear_range : \$ x \\in \\[-1.0, 1.0\\] \$"
@test sprint(show, "text/latex", linear_noname) == "\$ x \\leq 1.0 \$"
end
@testset "Vector AffExpr constraints" begin
in_sym = JuMP._math_symbol(REPLMode, :in)
model = ModelType()
@variable(model, x)
@constraint(model, soc_constr, [x - 1, x + 1] in SecondOrderCone())
io_test(
REPLMode,
soc_constr,
"soc_constr : " *
"[x - 1, x + 1] $in_sym MathOptInterface.SecondOrderCone(2)",
)
# TODO: Test in IJulia mode.
end
@testset "Scalar QuadExpr constraints" begin
in_sym = JuMP._math_symbol(REPLMode, :in)
le = JuMP._math_symbol(REPLMode, :leq)
sq = JuMP._math_symbol(REPLMode, :sq)
model = ModelType()
@variable(model, x)
quad_constr = @constraint(model, 2x^2 <= 1)
io_test(REPLMode, quad_constr, "2 x$sq $le 1.0")
# TODO: Test in IJulia mode.
end
@testset "Scalar Indicator constraints" begin
le = JuMP._math_symbol(REPLMode, :leq)
model = ModelType()
@variable(model, x, Bin)
@variable(model, y)
ind_constr = @constraint(model, !x => {y <= 1})
io_test(REPLMode, ind_constr, "!x => {y $le 1.0}")
# TODO: Test in IJulia mode.
end
end
# Test printing of models of type `ModelType` for which the model is stored in
# an MOI backend
function model_printing_test(ModelType::Type{<:JuMP.AbstractModel})
@testset "Model" begin
repl(s) = JuMP._math_symbol(REPLMode, s)
le, ge, eq, fa = repl(:leq), repl(:geq), repl(:eq), repl(:for_all)
inset, dots = repl(:in), repl(:dots)
infty, union = repl(:infty), repl(:union)
Vert, sub2 = repl(:Vert), repl(:sub2)
for_all = repl(:for_all)
#------------------------------------------------------------------
model_1 = ModelType()
@variable(model_1, a >= 1)
@variable(model_1, b <= 1)
@variable(model_1, -1 <= c <= 1)
@variable(model_1, a1 >= 1, Int)
@variable(model_1, b1 <= 1, Int)
@variable(model_1, -1 <= c1 <= 1, Int)
@variable(model_1, x, Bin)
@variable(model_1, y)
@variable(model_1, z, Int)
@variable(model_1, u[1:3], Bin)
@variable(model_1, fi == 9)
@objective(model_1, Max, a - b + 2a1 - 10x)
@constraint(model_1, con, a + b - 10c + c1 - 2x <= 1)
@constraint(model_1, a * b <= 2)
@constraint(model_1, soc, [1 - a; u] in SecondOrderCone())
@constraint(model_1, [a b; c x] in PSDCone())
@constraint(model_1, Symmetric([a b; b x]) in PSDCone())
@constraint(
model_1,
[a, b, c] in MOI.PositiveSemidefiniteConeTriangle(2)
)
@constraint(
model_1,
[a, b, c, x] in MOI.PositiveSemidefiniteConeSquare(2)
)
VariableType = typeof(a)
io_test(
REPLMode,
model_1,
"""
Max a - b + 2 a1 - 10 x
Subject to
con : a + b - 10 c + c1 - 2 x $le 1.0
a*b $le 2.0
[a b;
b x] $inset PSDCone()
[a, b, c] $inset MathOptInterface.PositiveSemidefiniteConeTriangle(2)
[a b;
c x] $inset PSDCone()
[a, b, c, x] $inset MathOptInterface.PositiveSemidefiniteConeSquare(2)
soc : [-a + 1, u[1], u[2], u[3]] $inset MathOptInterface.SecondOrderCone(4)
fi $eq 9.0
a $ge 1.0
c $ge -1.0
a1 $ge 1.0
c1 $ge -1.0
b $le 1.0
c $le 1.0
b1 $le 1.0
c1 $le 1.0
a1 integer
b1 integer
c1 integer
z integer
x binary
u[1] binary
u[2] binary
u[3] binary
""",
repl = :print,
)
io_test(
REPLMode,
model_1,
"""
A JuMP Model
Maximization problem with:
Variables: 13
Objective function type: GenericAffExpr{Float64,$VariableType}
`GenericAffExpr{Float64,$VariableType}`-in-`MathOptInterface.LessThan{Float64}`: 1 constraint
`GenericQuadExpr{Float64,$VariableType}`-in-`MathOptInterface.LessThan{Float64}`: 1 constraint
`Array{$VariableType,1}`-in-`MathOptInterface.PositiveSemidefiniteConeTriangle`: 2 constraints
`Array{$VariableType,1}`-in-`MathOptInterface.PositiveSemidefiniteConeSquare`: 2 constraints
`Array{GenericAffExpr{Float64,$VariableType},1}`-in-`MathOptInterface.SecondOrderCone`: 1 constraint
`$VariableType`-in-`MathOptInterface.EqualTo{Float64}`: 1 constraint
`$VariableType`-in-`MathOptInterface.GreaterThan{Float64}`: 4 constraints
`$VariableType`-in-`MathOptInterface.LessThan{Float64}`: 4 constraints
`$VariableType`-in-`MathOptInterface.Integer`: 4 constraints
`$VariableType`-in-`MathOptInterface.ZeroOne`: 4 constraints
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.
Names registered in the model: a, a1, b, b1, c, c1, con, fi, soc, u, x, y, z""",
repl = :show,
)
io_test(
IJuliaMode,
model_1,
"""
\\begin{alignat*}{1}\\max\\quad & a - b + 2 a1 - 10 x\\\\
\\text{Subject to} \\quad & a + b - 10 c + c1 - 2 x \\leq 1.0\\\\
& a\\times b \\leq 2.0\\\\
& \\begin{bmatrix}
a & b\\\\
\\cdot & x\\\\
\\end{bmatrix} \\in PSDCone()\\\\
& [a, b, c] \\in MathOptInterface.PositiveSemidefiniteConeTriangle(2)\\\\
& \\begin{bmatrix}
a & b\\\\
c & x\\\\
\\end{bmatrix} \\in PSDCone()\\\\
& [a, b, c, x] \\in MathOptInterface.PositiveSemidefiniteConeSquare(2)\\\\
& [-a + 1, u_{1}, u_{2}, u_{3}] \\in MathOptInterface.SecondOrderCone(4)\\\\
& fi = 9.0\\\\
& a \\geq 1.0\\\\
& c \\geq -1.0\\\\
& a1 \\geq 1.0\\\\
& c1 \\geq -1.0\\\\
& b \\leq 1.0\\\\
& c \\leq 1.0\\\\
& b1 \\leq 1.0\\\\
& c1 \\leq 1.0\\\\
& a1 integer\\\\
& b1 integer\\\\
& c1 integer\\\\
& z integer\\\\
& x binary\\\\
& u_{1} binary\\\\
& u_{2} binary\\\\
& u_{3} binary\\\\
\\end{alignat*}
""",
)
#------------------------------------------------------------------
model_2 = ModelType()
@variable(model_2, x, Bin)
@variable(model_2, y, Int)
@constraint(model_2, x * y <= 1)
io_test(
REPLMode,
model_2,
"""
A JuMP Model
Feasibility problem with:
Variables: 2
`GenericQuadExpr{Float64,$VariableType}`-in-`MathOptInterface.LessThan{Float64}`: 1 constraint
`$VariableType`-in-`MathOptInterface.Integer`: 1 constraint
`$VariableType`-in-`MathOptInterface.ZeroOne`: 1 constraint
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.
Names registered in the model: x, y""",
repl = :show,
)
model_3 = ModelType()
@variable(model_3, x)
@constraint(model_3, x <= 3)
io_test(
REPLMode,
model_3,
"""
A JuMP Model
Feasibility problem with:
Variable: 1
`GenericAffExpr{Float64,$VariableType}`-in-`MathOptInterface.LessThan{Float64}`: 1 constraint
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.
Names registered in the model: x""",
repl = :show,
)
end
end
# Test printing of models of type `ModelType` for which the model is stored in
# its JuMP form, e.g., as `AbstractVariable`s and `AbstractConstraint`s.
# This is used by `JuMPExtension` but can also be used by external packages such
# as `StructJuMP`, see https://github.com/jump-dev/JuMP.jl/issues/1711
function model_extension_printing_test(ModelType::Type{<:JuMP.AbstractModel})
@testset "Model" begin
repl(s) = JuMP._math_symbol(REPLMode, s)
le, ge, eq, fa = repl(:leq), repl(:geq), repl(:eq), repl(:for_all)
inset, dots = repl(:in), repl(:dots)
infty, union = repl(:infty), repl(:union)
Vert, sub2 = repl(:Vert), repl(:sub2)
for_all = repl(:for_all)
#------------------------------------------------------------------
model_1 = ModelType()
@variable(model_1, a >= 1)
@variable(model_1, b <= 1)
@variable(model_1, -1 <= c <= 1)
@variable(model_1, a1 >= 1, Int)
@variable(model_1, b1 <= 1, Int)
@variable(model_1, -1 <= c1 <= 1, Int)
@variable(model_1, x, Bin)
@variable(model_1, y)
@variable(model_1, z, Int)
@variable(model_1, u[1:3], Bin)
@variable(model_1, fi == 9)
@objective(model_1, Max, a - b + 2a1 - 10x)
@constraint(model_1, a + b - 10c - 2x + c1 <= 1)
@constraint(model_1, a * b <= 2)
@constraint(model_1, [1 - a; u] in SecondOrderCone())
VariableType = typeof(a)
# TODO variable constraints
io_test(
REPLMode,
model_1,
"""
Max a - b + 2 a1 - 10 x
Subject to
a + b - 10 c - 2 x + c1 $le 1.0
a*b $le 2.0
[-a + 1, u[1], u[2], u[3]] $inset MathOptInterface.SecondOrderCone(4)
""",
repl = :print,
)
io_test(
REPLMode,
model_1,
"""
A JuMP Model
Maximization problem with:
Variables: 13
Objective function type: GenericAffExpr{Float64,$VariableType}
Constraints: 3
Names registered in the model: a, a1, b, b1, c, c1, fi, u, x, y, z""",
repl = :show,
)
io_test(
IJuliaMode,
model_1,
"""
\\begin{alignat*}{1}\\max\\quad & a - b + 2 a1 - 10 x\\\\
\\text{Subject to} \\quad & a + b - 10 c - 2 x + c1 \\leq 1.0\\\\
& a\\times b \\leq 2.0\\\\
& [-a + 1, u_{1}, u_{2}, u_{3}] \\in MathOptInterface.SecondOrderCone(4)\\\\
\\end{alignat*}
""",
)
#------------------------------------------------------------------
model_2 = ModelType()
@variable(model_2, x, Bin)
@variable(model_2, y, Int)
@constraint(model_2, x * y <= 1)
io_test(
REPLMode,
model_2,
"""
A JuMP Model
Feasibility problem with:
Variables: 2
Constraint: 1
Names registered in the model: x, y""",
repl = :show,
)
model_3 = ModelType()
@variable(model_3, x)
@constraint(model_3, x <= 3)
io_test(
REPLMode,
model_3,
"""
A JuMP Model
Feasibility problem with:
Variable: 1
Constraint: 1
Names registered in the model: x""",
repl = :show,
)
end
end
@testset "Printing for JuMP.Model" begin
printing_test(Model)
model_printing_test(Model)
@testset "Model with nonlinear terms" begin
eq = JuMP._math_symbol(REPLMode, :eq)
model = Model()
@variable(model, x)
@NLobjective(model, Max, sin(x))
c = @NLexpression(model, cos(x))
@NLconstraint(model, c == 0)
io_test(
REPLMode,
model,
"""
A JuMP Model
Maximization problem with:
Variable: 1
Objective function type: Nonlinear
Nonlinear: 1 constraint
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.
Names registered in the model: x""",
repl = :show,
)
io_test(
REPLMode,
model,
"""
Max sin(x)
Subject to
subexpression[1] - 0.0 $eq 0
With NL expressions
subexpression[1]: cos(x)
""",
repl = :print,
)
io_test(
IJuliaMode,
model,
"""
\\begin{alignat*}{1}\\max\\quad & sin(x)\\\\
\\text{Subject to} \\quad & subexpression_{1} - 0.0 = 0\\\\
\\text{With NL expressions} \\quad & subexpression_{1}: cos(x)\\\\
\\end{alignat*}
""",
)
end
@testset "SingleVariable constraints" begin
ge = JuMP._math_symbol(REPLMode, :geq)
in_sym = JuMP._math_symbol(REPLMode, :in)
model = Model()
@variable(model, x >= 10)
zero_one = @constraint(model, x in MOI.ZeroOne())
io_test(REPLMode, JuMP.LowerBoundRef(x), "x $ge 10.0")
io_test(REPLMode, zero_one, "x binary")
# TODO: Test in IJulia mode
end
end
@testset "Printing for JuMPExtension.MyModel" begin
printing_test(JuMPExtension.MyModel)
model_extension_printing_test(JuMPExtension.MyModel)
end