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

Fix error for printing in IJuliaMode #2420

Merged
merged 11 commits into from
Jan 5, 2021
13 changes: 9 additions & 4 deletions src/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -617,10 +617,15 @@ end
#------------------------------------------------------------------------
## _NonlinearExprData
#------------------------------------------------------------------------
function nl_expr_string(model::Model, mode, c::_NonlinearExprData)
return string(_tape_to_expr(model, 1, c.nd, adjmat(c.nd), c.const_values,
[], [], model.nlp_data.user_operators, false,
false, mode))
function nl_expr_string(model::Model, print_mode, c::_NonlinearExprData)
nl = string(_tape_to_expr(model, 1, c.nd, adjmat(c.nd), c.const_values,
[], [], model.nlp_data.user_operators, false,
false, print_mode))
if print_mode == IJuliaMode
# Replace exponents x ^ 4.0 for x ^ {4.0}
nl = replace(nl, r"(\^) (?<exponent>(?=\()\(.+\)|-?\d+\.\d+)" => s"^ {\g<exponent>}")
end
return nl
end

#------------------------------------------------------------------------
Expand Down
21 changes: 21 additions & 0 deletions test/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,39 @@ end
constr_ge = @NLconstraint(model, sin(x) >= 1)
constr_eq = @NLconstraint(model, sin(x) == 1)
constr_range = @NLconstraint(model, 0 <= sin(x) <= 1)
constr_exponent_1 = @NLconstraint(model, x^4 <= 1)
constr_exponent_2 = @NLconstraint(model, x^40.23 <= 1)
constr_exponent_3 = @NLconstraint(model, x^4 + x^3 + x^2 <= 1)
constr_exponent_4 = @NLconstraint(model, x^-4 + x^-3 + x^-2 <= 1)
constr_exponent_5 = @NLconstraint(model, x^(x * x) <= 1)
constr_exponent_6 = @NLconstraint(model, x^(2x) <= 1)
constr_exponent_7 = @NLconstraint(model, x^(2 * 5) <= 1)

io_test(REPLMode, constr_le, "sin(x) - 1.0 $le 0")
io_test(REPLMode, constr_ge, "sin(x) - 1.0 $ge 0")
io_test(REPLMode, constr_eq, "sin(x) - 1.0 $eq 0")
# Note: This is inconsistent with the "x in [-1, 1]" printing for
# regular constraints.
io_test(REPLMode, constr_range, "0 $le sin(x) $le 1")
io_test(REPLMode, constr_exponent_1, "x ^ 4.0 - 1.0 $le 0")
io_test(REPLMode, constr_exponent_2, "x ^ 40.23 - 1.0 $le 0")
io_test(REPLMode, constr_exponent_3, "(x ^ 4.0 + x ^ 3.0 + x ^ 2.0) - 1.0 $le 0")
io_test(REPLMode, constr_exponent_4, "(x ^ -4.0 + x ^ -3.0 + x ^ -2.0) - 1.0 $le 0")
io_test(REPLMode, constr_exponent_5, "x ^ (x * x) - 1.0 ≤ 0")
io_test(REPLMode, constr_exponent_6, "x ^ (2.0 * x) - 1.0 ≤ 0")
io_test(REPLMode, constr_exponent_7, "x ^ (2.0 * 5.0) - 1.0 ≤ 0")

io_test(IJuliaMode, constr_le, "sin(x) - 1.0 \\leq 0")
io_test(IJuliaMode, constr_ge, "sin(x) - 1.0 \\geq 0")
io_test(IJuliaMode, constr_eq, "sin(x) - 1.0 = 0")
io_test(IJuliaMode, constr_range, "0 \\leq sin(x) \\leq 1")
io_test(IJuliaMode, constr_exponent_1, "x ^ {4.0} - 1.0 \\leq 0")
io_test(IJuliaMode, constr_exponent_2, "x ^ {40.23} - 1.0 \\leq 0")
io_test(IJuliaMode, constr_exponent_3, "(x ^ {4.0} + x ^ {3.0} + x ^ {2.0}) - 1.0 \\leq 0")
io_test(IJuliaMode, constr_exponent_4, "(x ^ {-4.0} + x ^ {-3.0} + x ^ {-2.0}) - 1.0 \\leq 0")
io_test(IJuliaMode, constr_exponent_5, "x ^ {(x * x)} - 1.0 \\leq 0")
io_test(IJuliaMode, constr_exponent_6, "x ^ {(2.0 * x)} - 1.0 \\leq 0")
io_test(IJuliaMode, constr_exponent_7, "x ^ {(2.0 * 5.0)} - 1.0 \\leq 0")
end

@testset "Nonlinear constraints with embedded parameters/expressions" begin
Expand Down