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

[FileFormats.NL] fix nested scalar affine and quadratic functions #2231

Merged
merged 1 commit into from
Jun 28, 2023
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
12 changes: 12 additions & 0 deletions src/FileFormats/NL/NLExpr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,18 @@ function _process_expr!(expr::_NLExpr, arg::Expr)
return error("Unsupported expression: $(arg)")
end

function _process_expr!(
expr::_NLExpr,
arg::Union{MOI.ScalarAffineFunction,MOI.ScalarQuadraticFunction},
)
# This method gets called recursively from the arguments to
# ScalarNonlinearFunction, so we cannot add the linear parts to
# `expr.linear_terms`.
f = convert(MOI.ScalarNonlinearFunction, arg)
_process_expr!(expr, f)
return
end

function _process_expr!(expr::_NLExpr, arg::MOI.ScalarNonlinearFunction)
if length(arg.args) == 1
f = get(_UNARY_SPECIAL_CASES, arg.head, nothing)
Expand Down
18 changes: 18 additions & 0 deletions test/FileFormats/NL/NL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,24 @@ function test_nlexpr_scalarnonlinearfunction_ternary_multiplication()
return
end

function test_nlexpr_scalarnonlinearfunction_inner_functions()
x = MOI.VariableIndex(1)
f = 1.0 * x + 1.0
g = 2.0 * x * x + 3.0 * x + 4.0
f = MOI.ScalarNonlinearFunction(:*, Any[2.0, x, f, g])
ex = Expr(
:call,
:*,
2.0,
x,
:(1.0 * $x + 1.0),
:(2.0 * $x * $x + 3.0 * $x + 4.0),
)
expr = NL._NLExpr(ex)
_test_nlexpr(f, expr.nonlinear_terms, Dict(x => 0), 0.0)
return
end

function test_nlexpr_unary_addition()
x = MOI.VariableIndex(1)
return _test_nlexpr(:(+$x), [x], Dict(x => 0), 0.0)
Expand Down