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 nonlinear subexpressions #2294

Merged
merged 1 commit into from
Jul 30, 2020
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
41 changes: 41 additions & 0 deletions src/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,53 @@ function model_string(print_mode, model::AbstractModel)
if !isempty(constraints)
str *= eol
end
# TODO: Generalize this when similar functionality is needed for
# AbstractModel.
nl_subexpressions = _nl_subexpression_string(print_mode, model)
if !isempty(nl_subexpressions)
str *= ijl ? "\\text{With NL expressions} \\quad" :
"With NL expressions" * eol
str *= sep * join(nl_subexpressions, eol * sep)
str *= eol
end
if ijl
str = "\\begin{alignat*}{1}" * str * "\\end{alignat*}\n"
end
return str
end

_nl_subexpression_string(print_mode, ::AbstractModel) = String[]

function _nl_subexpression_string(print_mode, model::Model)
strings = String[]
if model.nlp_data !== nothing
num_subexpressions = length(model.nlp_data.nlexpr)::Int
for k in 1:num_subexpressions
ex = model.nlp_data.nlexpr[k]
expr_string = _tape_to_expr(
model,
1, # start index in the expression
ex.nd,
adjmat(ex.nd),
ex.const_values,
[], # parameter_values (not used)
[], # subexpressions (not needed because !splat_subexpressions)
model.nlp_data.user_operators,
false, # generic_variable_names
false, # splat_subexpressions
print_mode,
)
if print_mode == IJuliaMode
expr_name = "subexpression_{$k}"
else
expr_name = "subexpression[$k]"
end
push!(strings, "$expr_name: $expr_string")
end
end
return strings
end

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

Expand Down
10 changes: 7 additions & 3 deletions test/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,8 @@ end
model = Model()
@variable(model, x)
@NLobjective(model, Max, sin(x))
@NLconstraint(model, cos(x) == 0)
c = @NLexpression(model, cos(x))
@NLconstraint(model, c == 0)

io_test(REPLMode, model, """
A JuMP Model
Expand All @@ -631,12 +632,15 @@ end
io_test(REPLMode, model, """
Max sin(x)
Subject to
cos(x) - 0.0 $eq 0
subexpression[1] - 0.0 $eq 0
With NL expressions
subexpression[1]: cos(x)
blegat marked this conversation as resolved.
Show resolved Hide resolved
""", repl=:print)

io_test(IJuliaMode, model, """
\\begin{alignat*}{1}\\max\\quad & sin(x)\\\\
\\text{Subject to} \\quad & cos(x) - 0.0 = 0\\\\
\\text{Subject to} \\quad & subexpression_{1} - 0.0 = 0\\\\
\\text{With NL expressions} \\quad & subexpression_{1}: cos(x)\\\\
\\end{alignat*}
""")
end
Expand Down