Skip to content

Commit 379f7e7

Browse files
authored
Fix show for problems with variables containing constraints (#649)
1 parent 1b3a4bb commit 379f7e7

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/utilities/show.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,16 @@ function TreePrint.print_tree(io::IO, p::Problem, args...; kwargs...)
217217
args...;
218218
kwargs...,
219219
)
220-
if !isempty(p.constraints)
220+
all_constraints = copy(p.constraints)
221+
for leaf in AbstractTrees.Leaves(p)
222+
if leaf isa AbstractVariable
223+
append!(all_constraints, get_constraints(leaf))
224+
end
225+
end
226+
if !isempty(all_constraints)
221227
TreePrint.print_tree(
222228
io,
223-
ProblemConstraintsRoot(p.constraints),
229+
ProblemConstraintsRoot(all_constraints),
224230
args...;
225231
kwargs...,
226232
)

test/test_utilities.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,16 @@ function test_Constant_complex()
13251325
return
13261326
end
13271327

1328+
function test_show_with_variable_constraints()
1329+
x = Variable()
1330+
add_constraint!(x, x >= 1)
1331+
p = minimize(x)
1332+
tree = sprint(show, p)
1333+
@test occursin("subject to", tree)
1334+
@test occursin("≥ constraint (affine)", tree)
1335+
return
1336+
end
1337+
13281338
end # TestUtilities
13291339

13301340
TestUtilities.runtests()

0 commit comments

Comments
 (0)