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

Print constraint name in model string #2108

Merged
merged 2 commits into from
Nov 23, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 8 additions & 7 deletions src/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,7 @@ function constraints_string(print_mode, model::Model)
strings = String[]
for (F, S) in list_of_constraint_types(model)
for cref in all_constraints(model, F, S)
con = constraint_object(cref)
push!(strings, constraint_string(print_mode, con))
push!(strings, constraint_string(print_mode, cref, in_math_mode = true))
end
end
if model.nlp_data !== nothing
Expand Down Expand Up @@ -557,19 +556,21 @@ function constraint_string(print_mode, constraint_object::AbstractConstraint)
end
end
function constraint_string(print_mode, constraint_name,
constraint_object::AbstractConstraint)
constraint_object::AbstractConstraint;
in_math_mode = false)
constraint_without_name = constraint_string(print_mode, constraint_object)
if print_mode == IJuliaMode
if print_mode == IJuliaMode && !in_math_mode
constraint_without_name = _wrap_in_inline_math_mode(constraint_without_name)
end
if isempty(constraint_name)
# Names don't print well in LaTeX math mode
if isempty(constraint_name) || (print_mode == IJuliaMode && in_math_mode)
return constraint_without_name
else
return constraint_name * " : " * constraint_without_name
end
end
function constraint_string(print_mode, ref::ConstraintRef)
return constraint_string(print_mode, name(ref), constraint_object(ref))
function constraint_string(print_mode, ref::ConstraintRef; in_math_mode = false)
return constraint_string(print_mode, name(ref), constraint_object(ref), in_math_mode = in_math_mode)
end

#------------------------------------------------------------------------
Expand Down
10 changes: 5 additions & 5 deletions test/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,9 @@ function model_printing_test(ModelType::Type{<:JuMP.AbstractModel})
@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, con, a + b - 10c - 2x + c1 <= 1)
@constraint(model_1, a*b <= 2)
@constraint(model_1, [1 - a; u] in SecondOrderCone())
@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))
Expand All @@ -396,15 +396,15 @@ function model_printing_test(ModelType::Type{<:JuMP.AbstractModel})
io_test(REPLMode, model_1, """
Max a - b + 2 a1 - 10 x
Subject to
a + b - 10 c - 2 x + c1 $le 1.0
con : a + b - 10 c - 2 x + c1 $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)
[-a + 1, u[1], u[2], u[3]] $inset MathOptInterface.SecondOrderCone(4)
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
Expand Down Expand Up @@ -442,7 +442,7 @@ function model_printing_test(ModelType::Type{<:JuMP.AbstractModel})
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.
Names registered in the model: a, a1, b, b1, c, c1, fi, u, x, y, z""", repl=:show)
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\\\\
Expand Down