/
print.jl
897 lines (825 loc) · 34.2 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
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
# Copyright 2015, 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 http://mozilla.org/MPL/2.0/.
#############################################################################
# JuMP
# An algebraic modeling language for Julia
# See http://github.com/JuliaOpt/JuMP.jl
#############################################################################
# print.jl
# All "pretty printers" for JuMP types.
# - Delegates to appropriate handler methods for REPL or IJulia.
# - These handler methods then pass the correct symbols to use into a
# generic string builder. The IJulia handlers will also wrap in MathJax
# start/close tags.
# - To find printing code for a type in this file, search for `## TypeName`
# - Code here does not need to be fast, in fact simplicity trumps speed
# within reason as this code is thorny enough as it is.
# - Corresponding tests are in test/print.jl, although test/operator.jl
# is also testing the constraint/expression code extensively as well.
#############################################################################
# Used for dispatching
abstract PrintMode
abstract REPLMode <: PrintMode
abstract IJuliaMode <: PrintMode
# Whether something is zero or not for the purposes of printing it
const PRINT_ZERO_TOL = 1e-10
# List of indices available for variable printing
const DIMS = ["i","j","k","l","m","n"]
# Helper function that rounds carefully for the purposes of printing
# e.g. 5.3 => 5.3
# 1.0 => 1
function str_round(f::Float64)
abs(f) == 0.0 && return "0" # strip sign off zero
str = string(f)
length(str) >= 2 && str[end-1:end] == ".0" ? str[1:end-2] : str
end
# TODO: get rid of this! This is only a helper, and should be Base.values
# (and probably live there, as well)
_values(x::Array) = x
_values(x) = Base.values(x)
# REPL-specific symbols
const repl = @compat Dict{Symbol,UTF8String}(
:leq => (OS_NAME===:Windows ? "<=" : "≤"),
:geq => (OS_NAME===:Windows ? ">=" : "≥"),
:eq => (OS_NAME===:Windows ? "==" : "="),
:times => "*",
:sq => "\u00B2", # Superscript 2
:ind_open => "[",
:ind_close => "]",
:for_all => "for all",
:in => "in",
:open_set => "{",
:mid_set => "..",
:close_set => "}",
:union => "or",
:infty => "Inf",
:open_rng => "[",
:close_rng => "]",
:integer => "integer",
:succeq0 => " is semidefinite")
# IJulia-specific symbols
const ijulia = @compat Dict{Symbol,UTF8String}(
:leq => "\\leq",
:geq => "\\geq",
:eq => "=",
:times => "\\times",
:sq => "^2",
:ind_open => "_{",
:ind_close => "}",
:for_all => "\\quad\\forall",
:in => "\\in",
:open_set => "\\{",
:mid_set => ",\\dots,",
:close_set => "\\}",
:union => "\\cup",
:infty => "\\intfy",
:open_rng => "\\[",
:close_rng => "\\]",
:integer => "\\in \\mathbb{Z}",
:succeq0 => "\\succeq 0")
const sqrt = "√"
const pow_two = "²"
typealias PrintSymbols Dict{Symbol,UTF8String}
# If not already mathmode, then wrap in MathJax start/close tags
math(s,mathmode) = mathmode ? s : "\$\$ $s \$\$"
# helper to look up corresponding JuMPContainerData
printdata(v::JuMPContainer) = getmeta(v, :model).varData[v]
function printdata(v::Array{Variable})
if isempty(v)
error("Cannot locate printing data for an empty array")
end
m = first(v).m
m.varData[v]
end
#------------------------------------------------------------------------
## Model
#------------------------------------------------------------------------
function Base.print(io::IO, m::Model; ignore_print_hook=(m.printhook==nothing))
ignore_print_hook || return m.printhook(io, m)
print(io, model_str(REPLMode,m))
end
function Base.show(io::IO, m::Model)
plural(n) = (n==1 ? "" : "s")
print(io, m.objSense == :Max ? "Maximization" : ((m.objSense == :Min && (!isempty(m.obj) || (m.nlpdata !== nothing && isa(m.nlpdata.nlobj, ReverseDiffSparse.SymbolicOutput)))) ? "Minimization" : "Feasibility"))
println(io, " problem with:")
nlin = length(m.linconstr)
println(io, " * $(nlin) linear constraint$(plural(nlin))")
nquad = length(m.quadconstr)
if nquad > 0
println(io, " * $(nquad) quadratic constraint$(plural(nquad))")
end
nsdp = length(m.sdpconstr)
if nsdp > 0
println(io, " * $(nsdp) semidefinite constraint$(plural(nsdp))")
end
nlp = m.nlpdata
if nlp !== nothing && length(nlp.nlconstr) > 0
println(io, " * $(length(nlp.nlconstr)) nonlinear constraint$(plural(length(nlp.nlconstr)))")
end
print(io, " * $(m.numCols) variable$(plural(m.numCols))")
nbin = sum(m.colCat .== :Bin)
nint = sum(m.colCat .== :Int)
nsc = sum(m.colCat .== :SemiCont)
nsi = sum(m.colCat .== :SemiInt)
varstr = Any[]
nbin == 0 || push!(varstr, "$nbin binary")
nint == 0 || push!(varstr, "$nint integer")
nsc == 0 || push!(varstr, "$nsc semicontinuous")
nsi == 0 || push!(varstr, "$nsi semi-integer")
if isempty(varstr)
println(io,)
else
println(io, ": $(join(varstr, ", "))")
end
print(io, "Solver set to ")
if isa(m.solver, UnsetSolver)
solver = "Default"
else
solver = string(m.solver)
end
print(io, split(solver, "Solver")[1])
end
Base.writemime(io::IO, ::MIME"text/latex", m::Model) =
print(io, model_str(IJuliaMode,m))
function model_str(mode, m::Model, sym::PrintSymbols)
ijl = mode == IJuliaMode
sep = ijl ? " & " : " "
eol = ijl ? "\\\\\n" : "\n"
nlp = m.nlpdata
# Objective
qobj_str = quad_str(mode, m.obj)
obj_sense = ijl ? (m.objSense == :Max ? "\\max" : "\\min")*"\\quad" :
(m.objSense == :Max ? "Max" : "Min")
str = obj_sense * sep
if nlp !== nothing && nlp.nlobj !== nothing
str *= (qobj_str=="0"?"":"$qobj_str + ") * "(nonlinear expression)"
else
str *= qobj_str
end
str *= eol
# Constraints
str *= ijl ? "\\text{Subject to} \\quad" : "Subject to" * eol
for c in m.linconstr
str *= sep * con_str(mode,c,mathmode=true) * eol
end
for c in m.quadconstr
str *= sep * con_str(mode,c,mathmode=true) * eol
end
for c in m.sosconstr
str *= sep * con_str(mode,c,mathmode=true) * eol
end
if nlp !== nothing && length(nlp.nlconstr) > 0
num = length(nlp.nlconstr)
str *= sep * string("$num nonlinear constraint", num>1?"s":"") * eol
end
# Display indexed variables
in_dictlist = falses(m.numCols)
for d in m.dictList
str *= sep * cont_str(mode,d,mathmode=true) * eol
# make sure that you haven't changed a variable type in the collection
cat = getCategory(first(_values(d)))
allsame = true
for v in _values(d)
if getCategory(v) != cat
allsame = false
break
end
end
if allsame
for it in _values(d) # Mark variables in JuMPContainer as printed
in_dictlist[it.col] = true
end
end
end
# Display non-indexed variables
for i in 1:m.numCols
in_dictlist[i] && continue
var_name = var_str(mode,m,i)
var_lb, var_ub = m.colLower[i], m.colUpper[i]
str_lb, str_ub = str_round(var_lb), str_round(var_ub)
var_cat = m.colCat[i]
if var_cat == :Bin # x binary
str *= string(sep, var_name,
" ", sym[:in],
" ", sym[:open_set],
"0,1", sym[:close_set])
elseif var_cat == :SemiInt # x in union of 0 and {lb,...,ub}
str *= string(sep, var_name,
" ", sym[:in],
" ", sym[:open_set],
str_lb, sym[:mid_set], str_ub,
sym[:close_set],
" ", sym[:union], " ",
sym[:open_set], "0", sym[:close_set])
elseif var_cat == :SemiCont # x in union of 0 and [lb,ub]
str *= string(sep, var_name,
" ", sym[:in],
" ", sym[:open_rng],
str_lb, ",", str_ub,
sym[:close_rng],
" ", sym[:union], " ",
sym[:open_set], "0", sym[:close_set])
elseif var_cat == :Fixed
str *= string(sep, var_name, " = ", str_lb)
elseif var_lb == -Inf && var_ub == +Inf # Free variable
str *= string(sep, var_name, " free")
elseif var_lb == -Inf # No lower bound
str *= string(sep, var_name, " ", sym[:leq], " ", str_ub)
elseif var_ub == +Inf # No upper bound
str *= string(sep, var_name, " ", sym[:geq], " ", str_lb)
else
str *= string(sep, str_lb, " ", sym[:leq],
" ", var_name, " ",
sym[:leq], " ", str_ub)
end
if var_cat == :Int
str *= string(", ", sym[:integer])
end
str *= eol
end
ijl ? "\$\$ \\begin{alignat*}{1}"*str*"\\end{alignat*}\n \$\$" :
str
end
# Handlers to use correct symbols
model_str(::Type{REPLMode}, m::Model) =
model_str(REPLMode, m, repl)
model_str(::Type{IJuliaMode}, m::Model; mathmode=true) =
math(model_str(IJuliaMode, m, ijulia), mathmode)
#------------------------------------------------------------------------
## Variable
#------------------------------------------------------------------------
Base.print(io::IO, v::Variable) = print(io, var_str(REPLMode,v))
Base.show( io::IO, v::Variable) = print(io, var_str(REPLMode,v))
Base.writemime(io::IO, ::MIME"text/latex", v::Variable) =
print(io, var_str(IJuliaMode,v,mathmode=false))
function var_str(mode, m::Model, col::Int; mathmode=true)
colNames = mode == REPLMode ? m.colNames : m.colNamesIJulia
if colNames[col] === EMPTYSTRING
for cont in m.dictList
fill_var_names(mode, colNames, cont)
end
end
return math(colNames[col] == "" ? "col_$col" : colNames[col], mathmode)
end
function fill_var_names{N}(mode, colNames, v::JuMPArray{Variable,N})
data = printdata(v)
idxsets = data.indexsets
lengths = map(length, idxsets)
name = data.name
cprod = cumprod([lengths...])
for (ind,var) in enumerate(v.innerArray)
idx_strs = [string( idxsets[1][mod1(ind,lengths[1])] )]
for i = 2:N
push!(idx_strs, string(idxsets[i][@compat Int(ceil(mod1(ind,cprod[i]) / cprod[i-1]))]))
end
if mode == IJuliaMode
colNames[var.col] = string(name, "_{", join(idx_strs,",") , "}")
else
colNames[var.col] = string(name, "[", join(idx_strs,",") , "]")
end
end
end
function fill_var_names(mode, colNames, v::JuMPDict{Variable})
name = printdata(v).name
for (ind,var) in zip(keys(v),values(v))
if mode == IJuliaMode
colNames[var.col] = string(name, "_{", join([string(i) for i in ind],","), "}")
else
colNames[var.col] = string(name, "[", join([string(i) for i in ind],","), "]")
end
end
end
function fill_var_names(mode, colNames, v::Array{Variable})
isempty(v) && return
sizes = size(v)
m = first(v).m
if !haskey(m.varData, v)
return
end
name = m.varData[v].name
for (ii,var) in enumerate(v)
@assert var.m === m
ind = ind2sub(sizes, ii)
colNames[var.col] = if mode === IJuliaMode
string(name, "_{", join(ind, ","), "}")
else
string(name, "[", join(ind, ","), "]")
end
end
return
end
# Handlers to use correct symbols
var_str(::Type{REPLMode}, v::Variable) =
var_str(REPLMode, v.m, v.col)
var_str(::Type{IJuliaMode}, v::Variable; mathmode=true) =
var_str(IJuliaMode, v.m, v.col, mathmode=mathmode)
#------------------------------------------------------------------------
## Norm
#------------------------------------------------------------------------
Base.print(io::IO, j::Norm) = print(io, norm_str(REPLMode,j))
Base.show( io::IO, j::Norm) = print(io, norm_str(REPLMode,j))
Base.writemime(io::IO, ::MIME"text/latex", j::Norm) =
print(io, norm_str(IJuliaMode,j))
function norm_str(mode, n::Norm)
terms_strs = Array(UTF8String, length(n.terms))
for i in 1:length(terms_strs)
t = aff_str(mode, n.terms[i])
terms_strs[i] = (if contains(t, " ")
"($t)$pow_two"
else
"$t$pow_two"
end)
end
string("$sqrt(", join(terms_strs, " + "), ")")
end
exprToStr(n::Norm) = exprToStr(convert(SOCExpr, copy(n)))
#------------------------------------------------------------------------
## JuMPContainer{Variable}
#------------------------------------------------------------------------
Base.print(io::IO, j::Union(JuMPContainer{Variable}, Array{Variable})) = print(io, cont_str(REPLMode,j))
Base.show( io::IO, j::Union(JuMPContainer{Variable}, Array{Variable})) = print(io, cont_str(REPLMode,j))
Base.writemime(io::IO, ::MIME"text/latex", j::Union(JuMPContainer{Variable},Array{Variable})) =
print(io, cont_str(IJuliaMode,j,mathmode=false))
# Generic string converter, called by mode-specific handlers
# Assumes that !isempty(j)
_getmodel(j::Array{Variable}) = first(j).m
_getmodel(j::JuMPContainer) = getmeta(j, :model)
function cont_str(mode, j, sym::PrintSymbols)
# Check if anything in the container
if isempty(j)
name = isa(j, JuMPContainer) ? printdata(j).name : "Empty Array{Variable}"
return "$name (no indices)"
end
m = _getmodel(j)
data = printdata(j)
# 1. construct the part with variable name and indexing
locvars = map(data.indexexprs) do tmp
var = tmp.idxvar
if var == nothing
return ""
else
return string(var)
end
end
num_dims = length(data.indexsets)
idxvars = Array(UTF8String, num_dims)
dimidx = 1
for i in 1:num_dims
if data.indexexprs[i].idxvar == nothing
while DIMS[dimidx] in locvars
dimidx += 1
end
if dimidx > length(DIMS)
error("Unexpectedly ran out of indices")
end
idxvars[i] = DIMS[dimidx]
dimidx += 1
else
idxvars[i] = locvars[i]
end
end
name_idx = string(data.name, sym[:ind_open], join(idxvars,","), sym[:ind_close])
# 2. construct part with what we index over
idx_sets = sym[:for_all]*" "*join(map(dim->string(idxvars[dim], " ", sym[:in],
" ", sym[:open_set],
cont_str_set(data.indexsets[dim],sym[:mid_set]),
sym[:close_set]), 1:num_dims), ", ")
# 3. Handle any conditions
if isa(j, JuMPDict) && data.condition != :()
idx_sets *= string(" s.t. ",join(parse_conditions(data.condition), " and "))
end
# 4. Bounds and category, if possible, and return final string
a_var = first(_values(j))
model = a_var.m
var_cat = model.colCat[a_var.col]
var_lb = model.colLower[a_var.col]
var_ub = model.colUpper[a_var.col]
# Variables may have different bounds, so we can't really print nicely
# at this time (possibly ever, as they could have been changed post
# creation, which we'd never be able to handle.
all_same_lb = true
all_same_ub = true
for var in _values(j)
all_same_lb &= model.colLower[var.col] == var_lb
all_same_ub &= model.colUpper[var.col] == var_ub
end
str_lb = var_lb == -Inf ? "-"*sym[:infty] : str_round(var_lb)
str_ub = var_ub == +Inf ? sym[:infty] : str_round(var_ub)
# Special case bounds printing based on the category
if var_cat == :Bin # x in {0,1}
return "$name_idx $(sym[:in]) $(sym[:open_set])0,1$(sym[:close_set]) $idx_sets"
elseif var_cat == :SemiInt # x in union of 0 and {lb,...,ub}
si_lb = all_same_lb ? str_lb : ".."
si_ub = all_same_ub ? str_ub : ".."
return "$name_idx $(sym[:in]) $(sym[:open_set])$si_lb$(sym[:mid_set])$si_ub$(sym[:close_set]) $(sym[:union]) $(sym[:open_set])0$(sym[:close_set]) $idx_sets"
elseif var_cat == :SemiCont # x in union of 0 and [lb,ub]
si_lb = all_same_lb ? str_lb : ".."
si_ub = all_same_ub ? str_ub : ".."
return "$name_idx $(sym[:in]) $(sym[:open_rng])$si_lb,$si_ub$(sym[:close_rng]) $(sym[:union]) $(sym[:open_set])0$(sym[:close_set]) $idx_sets"
elseif var_cat == :Fixed
si_bnd = all_same_lb ? str_lb : ".."
return "$name_idx = $si_bnd $idx_sets"
end
# Continuous and Integer
idx_sets = var_cat == :Int ? ", $(sym[:integer]), $idx_sets" : " $idx_sets"
if all_same_lb && all_same_ub
# Free variable
var_lb == -Inf && var_ub == +Inf && return "$name_idx free$idx_sets"
# No lower bound
var_lb == -Inf && return "$name_idx $(sym[:leq]) $str_ub$idx_sets"
# No upper bound
var_ub == +Inf && return "$name_idx $(sym[:geq]) $str_lb$idx_sets"
# Range
return "$str_lb $(sym[:leq]) $name_idx $(sym[:leq]) $str_ub$idx_sets"
end
if all_same_lb && !all_same_ub
var_lb == -Inf && return "$name_idx $(sym[:leq]) ..$idx_sets"
return "$str_lb $(sym[:leq]) $name_idx $(sym[:leq]) ..$idx_sets"
end
if !all_same_lb && all_same_ub
var_ub == +Inf && return "$name_idx $(sym[:geq]) ..$idx_sets"
return ".. $(sym[:leq]) $name_idx $(sym[:leq]) $str_ub$idx_sets"
end
return ".. $(sym[:leq]) $name_idx $(sym[:leq]) ..$idx_sets"
end
# UTILITY FUNCTIONS FOR cont_str
function cont_str_set(idxset::Union(Range,Array), mid_set) # 2:2:20 -> {2,4..18,20}
length(idxset) == 1 && return string(idxset[1])
length(idxset) == 2 && return string(idxset[1],",",idxset[2])
length(idxset) == 3 && return string(idxset[1],",",idxset[2],",",idxset[3])
length(idxset) == 4 && return string(idxset[1],",",idxset[2],",",idxset[3],",",idxset[4])
return string(idxset[1],",",idxset[2],mid_set,idxset[end-1],",",idxset[end])
end
cont_str_set(idxset, mid_set) = return ".." # Fallback
# parse_conditions
# Not exported. Traverses an expression and constructs an array with entries
# corresponding to each condition. More specifically, if the condition is
# a && (b || c) && (d && e), it returns [a, b || c, d, e].
parse_conditions(not_an_expr) = not_an_expr
function parse_conditions(expr::Expr)
ret = Any[]
if expr.head != :&&
return push!(ret, expr)
end
recurse = map(parse_conditions, expr.args)
vcat(ret, recurse...)
end
# Handlers to use correct symbols
cont_str(::Type{REPLMode}, j; mathmode=false) =
cont_str(REPLMode, j, repl)
cont_str(::Type{IJuliaMode}, j; mathmode=true) =
math(cont_str(IJuliaMode, j, ijulia), mathmode)
#------------------------------------------------------------------------
## JuMPContainer{Float64}
#------------------------------------------------------------------------
Base.print(io::IO, j::JuMPContainer{Float64}) = print(io, val_str(REPLMode,j))
Base.show( io::IO, j::JuMPContainer{Float64}) = print(io, val_str(REPLMode,j))
function val_str{N}(mode, j::JuMPArray{Float64,N})
m = _getmodel(j)
data = printdata(j)
out_str = "$(data.name): $N dimensions:\n"
if isempty(j)
return out_str * " (no entries)"
end
function val_str_rec(depth, parent_index::Vector{Any}, parent_str::String)
# Turn index set into strings
indexset = data.indexsets[depth]
index_strs = map(string, indexset)
# Determine longest index so we can align columns
max_index_len = 0
for index_str in index_strs
max_index_len = max(max_index_len, strwidth(index_str))
end
# If have recursed, we need to prepend the parent's index strings
# accumulated, as well as white space so the alignment works.
for i = 1:length(index_strs)
index_strs[i] = parent_str * lpad(index_strs[i],max_index_len," ")
end
# Create a string for the number of spaces we need to indent
indent = " "^(2*(depth-1))
# Determine the need to recurse
if depth == N
# Deepest level
for i = 1:length(indexset)
value = length(parent_index) == 0 ?
j[indexset[i]] :
j[parent_index...,indexset[i]]
out_str *= indent * "[" * index_strs[i] * "] = $value\n"
end
else
# At least one more layer to go
for i = 1:length(indexset)
index = indexset[i]
# Print the ":" version of indices we will recurse over
out_str *= indent * "[" * index_strs[i] * ",:"^(N-depth) * "]\n"
val_str_rec(depth+1,
length(parent_index) == 0 ? Any[index] : Any[parent_index...,index],
index_strs[i] * ",")
end
end
end
val_str_rec(1,Any[],"")
return out_str
end
# support types that don't have built-in comparison
function _isless(t1::Tuple, t2::Tuple)
n1, n2 = length(t1), length(t2)
for i = 1:min(n1, n2)
a, b = t1[i], t2[i]
if !isequal(a, b)
return applicable(isless,a,b) ? isless(a, b) : isless(hash(a),hash(b))
end
end
return n1 < n2
end
function val_str(mode, dict::JuMPDict{Float64})
nelem = length(dict.tupledict)
isempty(dict) && return ""
m = _getmodel(dict)
data = printdata(dict)
out_str = "$(data.name): $(length(data.indexsets)) dimensions, $nelem "
out_str *= nelem == 1 ? "entry" : "entries"
out_str *= ":"
sortedkeys = sort(collect(keys(dict.tupledict)), lt = _isless)
ndim = length(first(keys(dict.tupledict)))
key_strs = Array(String, length(dict), ndim)
for (i, key) in enumerate(sortedkeys)
for j in 1:ndim
key_strs[i,j] = string(key[j])
end
end
max_dim_lens = map(1:ndim) do i
maximum(map(length,key_strs[:,i]))
end
key_str = map(1:length(dict)) do i
join(map(1:ndim) do j
lpad(key_strs[i,j], max_dim_lens[j])
end, ",")
end
max_key_len = maximum(map(length,key_str))
for (i,key) in enumerate(sortedkeys)
val = dict[key...]
out_str *= "\n" * lpad("[$(key_str[i])]", max_key_len+3)
out_str *= " = $val"
end
return out_str
end
#------------------------------------------------------------------------
## AffExpr (not GenericAffExpr)
#------------------------------------------------------------------------
Base.print(io::IO, a::AffExpr) = print(io, aff_str(REPLMode,a))
Base.show( io::IO, a::AffExpr) = print(io, aff_str(REPLMode,a))
Base.writemime(io::IO, ::MIME"text/latex", a::AffExpr) =
print(io, math(aff_str(IJuliaMode,a),false))
# Generic string converter, called by mode-specific handlers
function aff_str(mode, a::AffExpr, show_constant=true)
# If the expression is empty, return the constant (or 0)
if length(a.vars) == 0
return show_constant ? str_round(a.constant) : "0"
end
# Get reference to models included in this expression
moddict = Dict{Model,IndexedVector{Float64}}()
for var in a.vars
if !haskey(moddict, var.m)
moddict[var.m] = IndexedVector(Float64,var.m.numCols)
end
end
# Collect like terms
for ind in 1:length(a.vars)
addelt!(moddict[a.vars[ind].m], a.vars[ind].col, a.coeffs[ind])
end
elm = 1
term_str = Array(UTF8String, 2*length(a.vars))
# For each model
for m in keys(moddict)
indvec = moddict[m]
# For each non-zero for this model
for i in 1:indvec.nnz
idx = indvec.nzidx[i]
elt = indvec.elts[idx]
abs(elt) < PRINT_ZERO_TOL && continue # e.g. x - x
pre = abs(abs(elt)-1) < PRINT_ZERO_TOL ? "" : str_round(abs(elt)) * " "
var = var_str(mode,m,idx)
term_str[2*elm-1] = elt < 0 ? " - " : " + "
term_str[2*elm ] = "$pre$var"
elm += 1
end
end
if elm == 1
# Will happen with cancellation of all terms
# We should just return the constant, if its desired
return show_constant ? str_round(a.constant) : "0"
else
# Correction for very first term - don't want a " + "/" - "
term_str[1] = (term_str[1] == " - ") ? "-" : ""
ret = join(term_str[1:2*(elm-1)])
if abs(a.constant) >= PRINT_ZERO_TOL && show_constant
ret = string(ret, a.constant < 0 ? " - " : " + ", str_round(abs(a.constant)))
end
return ret
end
end
# Backwards compatability shim
affToStr(a::AffExpr) = aff_str(REPLMode,a)
# Precompile for faster boot times
Base.precompile(aff_str, (Type{JuMP.REPLMode}, AffExpr, Bool))
Base.precompile(aff_str, (Type{JuMP.IJuliaMode}, AffExpr, Bool))
Base.precompile(aff_str, (Type{JuMP.REPLMode}, AffExpr))
Base.precompile(aff_str, (Type{JuMP.IJuliaMode}, AffExpr))
#------------------------------------------------------------------------
## GenericQuadExpr
#------------------------------------------------------------------------
Base.print(io::IO, q::GenericQuadExpr) = print(io, quad_str(REPLMode,q))
Base.show( io::IO, q::GenericQuadExpr) = print(io, quad_str(REPLMode,q))
Base.writemime(io::IO, ::MIME"text/latex", q::GenericQuadExpr) =
print(io, quad_str(IJuliaMode,q,mathmode=false))
# Generic string converter, called by mode-specific handlers
function quad_str(mode, q::GenericQuadExpr, sym)
length(q.qvars1) == 0 && return aff_str(mode,q.aff)
# Canonicalize x_i * x_j so i <= j
for ind in 1:length(q.qvars1)
if q.qvars2[ind].col < q.qvars1[ind].col
q.qvars1[ind],q.qvars2[ind] = q.qvars2[ind],q.qvars1[ind]
end
end
# Merge duplicates
Q = sparse([v.col for v in q.qvars1], [v.col for v in q.qvars2], q.qcoeffs)
I,J,V = findnz(Q)
Qnnz = length(V)
# Odd terms are +/i, even terms are the variables/coeffs
term_str = Array(UTF8String, 2*Qnnz)
if Qnnz > 0
for ind in 1:Qnnz
val = abs(V[ind])
pre = (val == 1.0 ? "" : str_round(val)*" ")
x = var_str(mode,q.qvars1[ind].m,I[ind])
y = var_str(mode,q.qvars1[ind].m,J[ind])
term_str[2*ind-1] = V[ind] < 0 ? " - " : " + "
term_str[2*ind ] = "$pre$x" * (x == y ? sym[:sq] : "$(sym[:times])$y")
end
# Correction for first term as there is no space
# between - and variable coefficient/name
term_str[1] = V[1] < 0 ? "-" : ""
end
ret = join(term_str)
if q.aff.constant == 0 && length(q.aff.vars) == 0
return ret
else
aff = aff_str(mode,q.aff)
if aff[1] == '-'
return string(ret, " - ", aff[2:end])
else
return string(ret, " + ", aff)
end
end
end
# Backwards compatability shim
quadToStr(q::GenericQuadExpr) = quad_str(REPLMode,q)
# Handlers to use correct symbols
quad_str(::Type{REPLMode}, q::GenericQuadExpr) =
quad_str(REPLMode, q, repl)
quad_str(::Type{IJuliaMode}, q::GenericQuadExpr; mathmode=true) =
math(quad_str(IJuliaMode, q, ijulia), mathmode)
#------------------------------------------------------------------------
## SOCExpr
#------------------------------------------------------------------------
Base.print(io::IO, c::SOCExpr) = print(io, expr_str(REPLMode,c))
Base.show( io::IO, c::SOCExpr) = print(io, expr_str(REPLMode,c))
Base.writemime(io::IO, ::MIME"text/latex", c::SOCExpr) =
print(io, expr_str(IJuliaMode,c))
function expr_str(mode, c::SOCExpr)
coeff = c.coeff == 1 ? "" : string(c.coeff, " ")
nrm = norm_str(mode, c.norm)
aff = aff_str(mode, c.aff)
if aff[1] == '-'
chain = " - "
aff = aff[2:end]
elseif aff == "0"
aff = ""
chain = ""
else
chain = " + "
end
string(coeff, nrm, chain, aff)
end
exprToStr(c::SOCExpr) = expr_str(REPLMode, c)
#------------------------------------------------------------------------
## GenericRangeConstraint
#------------------------------------------------------------------------
Base.print(io::IO, c::GenericRangeConstraint) = print(io, con_str(REPLMode,c))
Base.show( io::IO, c::GenericRangeConstraint) = print(io, con_str(REPLMode,c))
Base.writemime(io::IO, ::MIME"text/latex", c::GenericRangeConstraint) =
print(io, con_str(IJuliaMode,c,mathmode=false))
# Generic string converter, called by mode-specific handlers
function con_str(mode, c::GenericRangeConstraint, sym)
s = sense(c)
a = aff_str(mode,c.terms,false)
if s == :range
out_str = "$(str_round(c.lb)) $(sym[:leq]) $a $(sym[:leq]) $(str_round(c.ub))"
else
rel = s == :<= ? sym[:leq] : (s == :>= ? sym[:geq] : sym[:eq])
out_str = string(a," ",rel," ",str_round(rhs(c)))
end
out_str
end
# Backwards compatability shim
conToStr(c::GenericRangeConstraint) = con_str(REPLMode,c)
# Handlers to use correct symbols
con_str(::Type{REPLMode}, c::GenericRangeConstraint; args...) =
con_str(REPLMode, c, repl)
con_str(::Type{IJuliaMode}, c::GenericRangeConstraint; mathmode=true) =
math(con_str(IJuliaMode, c, ijulia), mathmode)
#------------------------------------------------------------------------
## QuadConstraint
#------------------------------------------------------------------------
Base.print(io::IO, c::QuadConstraint) = print(io, con_str(REPLMode,c))
Base.show( io::IO, c::QuadConstraint) = print(io, con_str(REPLMode,c))
Base.writemime(io::IO, ::MIME"text/latex", c::QuadConstraint) =
print(io, con_str(IJuliaMode,c,mathmode=false))
# Generic string converter, called by mode-specific handlers
function con_str(mode, c::QuadConstraint, sym)
s = c.sense
r = (s == :<=) ? sym[:leq] : (s == :>= ? sym[:geq] : sym[:eq])
"$(quad_str(mode,c.terms)) $r 0"
end
# Backwards compatability shim
conToStr(c::QuadConstraint) = con_str(REPLMode,c)
# Handlers to use correct symbols
con_str(::Type{REPLMode}, c::QuadConstraint; args...) =
con_str(REPLMode, c, repl)
con_str(::Type{IJuliaMode}, c::QuadConstraint; mathmode=true) =
math(con_str(IJuliaMode, c, ijulia), mathmode)
#------------------------------------------------------------------------
## SOCConstraint
#------------------------------------------------------------------------
Base.print(io::IO, c::SOCConstraint) = print(io, con_str(REPLMode,c))
Base.show( io::IO, c::SOCConstraint) = print(io, con_str(REPLMode,c))
Base.writemime(io::IO, ::MIME"text/latex", c::SOCConstraint) =
print(io, con_str(IJuliaMode,c))
function con_str(mode, c::SOCConstraint)
ne = c.normexpr
coeff = ne.coeff == 1 ? "" : string(ne.coeff, " ")
nrm = norm_str(mode, ne.norm)
aff = aff_str(mode, -ne.aff)
string(coeff, nrm, " $(repl[:leq]) ", aff)
end
conToStr(c::SOCConstraint) = con_str(REPLMode, c)
#------------------------------------------------------------------------
## SOSConstraint
#------------------------------------------------------------------------
Base.print(io::IO, c::SOSConstraint) = print(io, con_str(REPLMode,c))
Base.show( io::IO, c::SOSConstraint) = print(io, con_str(REPLMode,c))
Base.writemime(io::IO, ::MIME"text/latex", c::SOSConstraint) =
print(io, con_str(IJuliaMode,c,mathmode=false))
# Generic string converter, called by mode-specific handlers
function con_str(mode, c::SOSConstraint, sym)
term_str = [string(str_round(c.weights[i]), " ", c.terms[i])
for i in 1:length(c.terms)]
"$(c.sostype): $(sym[:open_set])$(join(term_str,", "))$(sym[:close_set])"
end
# Handlers to use correct symbols
con_str(::Type{REPLMode}, c::SOSConstraint; args...) =
con_str(REPLMode, c, repl)
con_str(::Type{IJuliaMode}, c::SOSConstraint; mathmode=true) =
math(con_str(IJuliaMode, c, ijulia), mathmode)
#------------------------------------------------------------------------
## SDPConstraint
#------------------------------------------------------------------------
Base.print(io::IO, c::SDPConstraint) = print(io, con_str(REPLMode,c))
Base.show( io::IO, c::SDPConstraint) = print(io, con_str(REPLMode,c))
Base.writemime(io::IO, ::MIME"text/latex", c::SDPConstraint) =
print(io, con_str(IJuliaMode,c,mathmode=false))
# Generic string converter, called by mode-specific handlers
function con_str(mode, c::SDPConstraint, succeq0)
t = c.terms
str = sprint(print, t)
splitted = split(str, "\n")[2:end]
center = ceil(Int, length(splitted)/2)
splitted[center] *= succeq0
join(splitted, "\n")
end
# Handlers to use correct symbols
con_str(::Type{REPLMode}, c::SDPConstraint; args...) =
con_str(REPLMode, c, repl[:succeq0])
con_str(::Type{IJuliaMode}, c::SDPConstraint; mathmode=true) =
math(con_str(IJuliaMode, c, ijulia[:succeq0], mathmode))
#------------------------------------------------------------------------
## ConstraintRef
#------------------------------------------------------------------------
Base.print(io::IO, c::ConstraintRef{LinearConstraint}) = print(io, con_str(REPLMode,c.m.linconstr[c.idx]))
Base.print(io::IO, c::ConstraintRef{QuadConstraint}) = print(io, con_str(REPLMode,c.m.quadconstr[c.idx]))
Base.print(io::IO, c::ConstraintRef{SOSConstraint}) = print(io, con_str(REPLMode,c.m.sosconstr[c.idx]))
Base.print(io::IO, c::ConstraintRef{SDPConstraint}) = print(io, con_str(REPLMode,c.m.sdpconstr[c.idx]))
Base.show( io::IO, c::ConstraintRef{LinearConstraint}) = print(io, con_str(REPLMode,c.m.linconstr[c.idx]))
Base.show( io::IO, c::ConstraintRef{QuadConstraint}) = print(io, con_str(REPLMode,c.m.quadconstr[c.idx]))
Base.show( io::IO, c::ConstraintRef{SOSConstraint}) = print(io, con_str(REPLMode,c.m.sosconstr[c.idx]))
Base.show( io::IO, c::ConstraintRef{SOCConstraint}) = print(io, con_str(REPLMode,c.m.socconstr[c.idx]))
Base.show( io::IO, c::ConstraintRef{SDPConstraint}) = print(io, con_str(REPLMode,c.m.sdpconstr[c.idx]))
Base.writemime(io::IO, ::MIME"text/latex", c::ConstraintRef{LinearConstraint}) =
print(io, con_str(IJuliaMode,c.m.linconstr[c.idx],mathmode=false))
Base.writemime(io::IO, ::MIME"text/latex", c::ConstraintRef{QuadConstraint}) =
print(io, con_str(IJuliaMode,c.m.quadconstr[c.idx],mathmode=false))
Base.writemime(io::IO, ::MIME"text/latex", c::ConstraintRef{SOSConstraint}) =
print(io, con_str(IJuliaMode,c.m.sosconstr[c.idx],mathmode=false))