Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add _CONSTRAINT_LIMIT_FOR_PRINTING to control printing of many constraints #3686

Merged
merged 2 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 44 additions & 4 deletions src/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,24 @@ function show_backend_summary(io::IO, model::GenericModel)
return
end

"""
const _CONSTRAINT_LIMIT_FOR_PRINTING = Ref{Int}(100)

A global constant used to control when constraints are omitted when printing
the model.

Get and set this value using `_CONSTRAINT_LIMIT_FOR_PRINTING[]`.

```julia
julia> _CONSTRAINT_LIMIT_FOR_PRINTING[]
100

julia> _CONSTRAINT_LIMIT_FOR_PRINTING[] = 10
odow marked this conversation as resolved.
Show resolved Hide resolved
10
```
"""
const _CONSTRAINT_LIMIT_FOR_PRINTING = Ref{Int}(100)
odow marked this conversation as resolved.
Show resolved Hide resolved

"""
_print_model(io::IO, model::AbstractModel)

Expand All @@ -303,8 +321,18 @@ function _print_model(io::IO, model::AbstractModel)
println(io, "Feasibility")
end
println(io, "Subject to")
for constraint in constraints_string(mode, model)
println(io, " ", replace(constraint, '\n' => "\n "))
constraints = constraints_string(mode, model)
m = div(_CONSTRAINT_LIMIT_FOR_PRINTING[], 2)
skip_start = _CONSTRAINT_LIMIT_FOR_PRINTING[] - m + 1
skip_stop = length(constraints) - m
n = skip_stop - skip_start + 1
for (i, constraint) in enumerate(constraints)
if i < skip_start || skip_stop < i
println(io, " ", replace(constraint, '\n' => "\n "))
end
if n > 0 && i == skip_start
println(io, "[[...$n constraints skipped...]]")
end
end
nl_subexpressions = _nl_subexpression_string(mode, model)
if !isempty(nl_subexpressions)
Expand Down Expand Up @@ -342,8 +370,20 @@ function _print_latex(io::IO, model::AbstractModel)
constraints = constraints_string(mode, model)
if !isempty(constraints)
print(io, "\\text{Subject to} \\quad")
for constraint in constraints
println(io, " & ", constraint, "\\\\")
m = div(_CONSTRAINT_LIMIT_FOR_PRINTING[], 2)
skip_start = _CONSTRAINT_LIMIT_FOR_PRINTING[] - m + 1
skip_stop = length(constraints) - m
n = skip_stop - skip_start + 1
for (i, constraint) in enumerate(constraints)
if i < skip_start || skip_stop < i
println(io, " & ", constraint, "\\\\")
end
if n > 0 && i == skip_start
println(
io,
" & [[\\ldots\\text{$n constraints skipped}\\ldots]] \\\\",
)
end
end
end
nl_subexpressions = _nl_subexpression_string(mode, model)
Expand Down
74 changes: 74 additions & 0 deletions test/test_print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -987,4 +987,78 @@ function test_truncated_printing()
return
end

function test_skipping_constraints()
model = Model()
@variable(model, 0 <= x[1:100] <= 10, Int)
N = num_constraints(model; count_variable_in_set_constraints = true)
@test N == 300
str = sprint(print, model)
for i in 1:100
@test occursin(sprint(print, LowerBoundRef(x[i])), str) == (i <= 50)
end
@test occursin("[[...200 constraints skipped...]]", str)
for i in 1:100
@test occursin(sprint(print, IntegerRef(x[i])), str) == (i >= 51)
end
ret = JuMP._CONSTRAINT_LIMIT_FOR_PRINTING[]
JuMP._CONSTRAINT_LIMIT_FOR_PRINTING[] = 3
str = sprint(print, model)
for i in 1:100
@test occursin(sprint(print, LowerBoundRef(x[i])), str) == (i <= 2)
end
@test occursin("[[...297 constraints skipped...]]", str)
for i in 1:100
@test occursin(sprint(print, IntegerRef(x[i])), str) == (i >= 100)
end
JuMP._CONSTRAINT_LIMIT_FOR_PRINTING[] = ret
return
end

function test_skipping_constraints_latex()
model = Model()
@variable(model, 0 <= x[1:100] <= 10, Int)
N = num_constraints(model; count_variable_in_set_constraints = true)
@test N == 300
str = sprint(print, latex_formulation(model))
for i in 1:100
output = constraint_string(
MIME("text/latex"),
LowerBoundRef(x[i]);
in_math_mode = true,
)
@test occursin(output, str) == (i <= 50)
end
@test occursin("[[\\ldots\\text{200 constraints skipped}\\ldots]]", str)
for i in 1:100
output = constraint_string(
MIME("text/latex"),
IntegerRef(x[i]);
in_math_mode = true,
)
@test occursin(output, str) == (i >= 51)
end
ret = JuMP._CONSTRAINT_LIMIT_FOR_PRINTING[]
JuMP._CONSTRAINT_LIMIT_FOR_PRINTING[] = 3
str = sprint(print, latex_formulation(model))
for i in 1:100
output = constraint_string(
MIME("text/latex"),
LowerBoundRef(x[i]);
in_math_mode = true,
)
@test occursin(output, str) == (i <= 2)
end
@test occursin("[[\\ldots\\text{297 constraints skipped}\\ldots]]", str)
for i in 1:100
output = constraint_string(
MIME("text/latex"),
IntegerRef(x[i]);
in_math_mode = true,
)
@test occursin(output, str) == (i >= 100)
end
JuMP._CONSTRAINT_LIMIT_FOR_PRINTING[] = ret
return
end

end # TestPrint
Loading