Skip to content

Commit

Permalink
Merge 6b81f10 into b92ba1b
Browse files Browse the repository at this point in the history
  • Loading branch information
mlubin committed Jul 25, 2020
2 parents b92ba1b + 6b81f10 commit 16f0877
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
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)
""", 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

0 comments on commit 16f0877

Please sign in to comment.