/
show.jl
185 lines (152 loc) · 5.5 KB
/
show.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
import Base.show, Base.summary
using .TreePrint
"""
show_id(io::IO, x::Union{AbstractExpr, Constraint}; digits = 3)
Print a truncated version of the objects `id_hash` field.
## Example
```julia-repl
julia> x = Variable();
julia> Convex.show_id(stdout, x)
id: 163…906
```
"""
show_id(io::IO, x::Union{AbstractExpr, Constraint}; digits = MAXDIGITS[]) = print(io, show_id(x; digits=digits))
function show_id(x::Union{AbstractExpr, Constraint}; digits = MAXDIGITS[])
hash_str = string(x.id_hash)
if length(hash_str) > (2*digits + 1);
return "id: " * first(hash_str, digits) * "…" * last(hash_str, digits)
else
return "id: " * hash_str
end
end
"""
Base.summary(io::IO, x::Variable)
Prints a one-line summary of a variable `x` to `io`.
## Examples
```julia-repl
julia> x = ComplexVariable(3,2);
julia> summary(stdout, x)
3×2 complex variable (id: 732…737)
```
"""
function Base.summary(io::IO, x::Variable)
sgn = summary(sign(x))
cst = vexity(x) == ConstVexity() ? " (fixed)" : ""
cst = cst * " (" * sprint(show_id, x) * ")"
if size(x) == (1,1)
print(io, "$(sgn) variable$(cst)")
elseif size(x,2) == 1
print(io, "$(size(x,1))-element $(sgn) variable$(cst)")
else
print(io, "$(size(x,1))×$(size(x,2)) $(sgn) variable$(cst)")
end
end
Base.summary(io::IO, ::AffineVexity) = print(io, "affine")
Base.summary(io::IO, ::ConvexVexity) = print(io, "convex")
Base.summary(io::IO, ::ConcaveVexity) = print(io, "concave")
Base.summary(io::IO, ::ConstVexity) = print(io, "constant")
Base.summary(io::IO, ::Positive) = print(io, "positive")
Base.summary(io::IO, ::Negative) = print(io, "negative")
Base.summary(io::IO, ::NoSign) = print(io, "real")
Base.summary(io::IO, ::ComplexSign) = print(io, "complex")
function Base.summary(io::IO, c::Constraint)
print(io, "$(c.head) constraint (")
summary(io, vexity(c))
print(io, ")")
end
function Base.summary(io::IO, e::AbstractExpr)
print(io, "$(e.head) (")
summary(io, vexity(e))
print(io, "; ")
summary(io, sign(e))
print(io, ")")
end
# A Constant is simply a wrapper around a native Julia constant
# Hence, we simply display its value
show(io::IO, x::Constant) = print(io, x.value)
# A variable, for example, Variable(3, 4), will be displayed as:
# julia> Variable(3,4)
# Variable
# size: (3, 4)
# sign: real
# vexity: affine
# id: 758…633
# here, the `id` will change from run to run.
function show(io::IO, x::Variable)
print(io, "Variable")
print(io, "\nsize: $(size(x))")
print(io, "\nsign: ")
summary(io, sign(x))
print(io, "\nvexity: ")
summary(io, vexity(x))
println(io)
show_id(io, x)
if x.value !== nothing
print(io, "\nvalue: $(x.value)")
end
end
"""
print_tree_rstrip(io::IO, x)
Prints the results of `TreePrint.print_tree(io, x)`
without the final newline. Used for `show` methods which
invoke `print_tree`.
"""
function print_tree_rstrip(io::IO, x)
str = sprint(TreePrint.print_tree, x, MAXDEPTH[], MAXWIDTH[])
print(io, rstrip(str))
end
# This object is used to work around the fact that
# Convex overloads booleans for AbstractExpr's
# in order to generate constraints. This is problematic
# for `AbstractTrees.print_tree` which wants to compare
# the root of the tree to itself at some point.
# By wrapping all tree roots in structs, this comparison
# occurs on the level of the `struct`, and `==` falls
# back to object equality (`===`), which is what we
# want in this case.
#
# The same construct is used below for other tree roots.
struct ConstraintRoot
constraint::Constraint
end
TreePrint.print_tree(io::IO, c::Constraint, args...; kwargs...) = TreePrint.print_tree(io, ConstraintRoot(c), args...; kwargs...)
AbstractTrees.children(c::ConstraintRoot) = AbstractTrees.children(c.constraint)
AbstractTrees.printnode(io::IO, c::ConstraintRoot) = AbstractTrees.printnode(io, c.constraint)
show(io::IO, c::Constraint) = print_tree_rstrip(io, c)
struct ExprRoot
expr::AbstractExpr
end
TreePrint.print_tree(io::IO, e::AbstractExpr, args...; kwargs...) = TreePrint.print_tree(io, ExprRoot(e), args...; kwargs...)
AbstractTrees.children(e::ExprRoot) = AbstractTrees.children(e.expr)
AbstractTrees.printnode(io::IO, e::ExprRoot) = AbstractTrees.printnode(io, e.expr)
show(io::IO, e::AbstractExpr) = print_tree_rstrip(io, e)
struct ProblemObjectiveRoot
head::Symbol
objective::AbstractExpr
end
AbstractTrees.children(p::ProblemObjectiveRoot) = (p.objective,)
AbstractTrees.printnode(io::IO, p::ProblemObjectiveRoot) = print(io, string(p.head))
struct ProblemConstraintsRoot
constraints::Vector{Constraint}
end
AbstractTrees.children(p::ProblemConstraintsRoot) = p.constraints
AbstractTrees.printnode(io::IO, p::ProblemConstraintsRoot) = print(io, "subject to")
function TreePrint.print_tree(io::IO, p::Problem, args...; kwargs...)
TreePrint.print_tree(io, ProblemObjectiveRoot(p.head, p.objective), args...; kwargs...)
if !(isempty(p.constraints))
TreePrint.print_tree(io, ProblemConstraintsRoot(p.constraints), args...; kwargs...)
end
end
function show(io::IO, p::Problem)
TreePrint.print_tree(io, p, MAXDEPTH[], MAXWIDTH[])
if p.status == MOI.OPTIMIZE_NOT_CALLED
print(io, "\nstatus: `solve!` not called yet")
else
print(io, "\ntermination status: $(p.status)")
print(io, "\nprimal status: $(primal_status(p))")
print(io, "\ndual status: $(dual_status(p))")
end
if p.status == "solved"
print(io, " with optimal value of $(round(p.optval, digits=4))")
end
end