Skip to content
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
42 changes: 32 additions & 10 deletions src/Nonlinear/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -345,18 +345,40 @@ function parse_expression(
x::MOI.ScalarQuadraticTerm,
parent_index::Int,
)
id_mul = data.operators.multivariate_operator_to_id[:*]
push!(expr.nodes, Node(NODE_CALL_MULTIVARIATE, id_mul, parent_index))
mul_parent = length(expr.nodes)
coef = x.coefficient
# There are four cases:
# (1): 1 * x * x --> ^(x, 2)
# (2): a * x * x --> *(a, ^(x, 2))
# (3): a * x * y --> *(a, x, y)
# (4): 1 * x * y --> *(x, y)
if x.variable_1 == x.variable_2
coef /= 2
end
if !isone(coef)
parse_expression(data, expr, coef, mul_parent)
# Case (1): 1 * x * x --> ^(x, 2)
# Case (2): a * x * x --> *(a, ^(x, 2))
coef = x.coefficient / 2
parent = parent_index
if !isone(coef)
id = data.operators.multivariate_operator_to_id[:*]
push!(expr.nodes, Node(NODE_CALL_MULTIVARIATE, id, parent_index))
parent = length(expr.nodes)
parse_expression(data, expr, coef, parent)
parent
end
id = data.operators.multivariate_operator_to_id[:^]
push!(expr.nodes, Node(NODE_CALL_MULTIVARIATE, id, parent))
pow_parent = length(expr.nodes)
parse_expression(data, expr, x.variable_1, pow_parent)
parse_expression(data, expr, 2, pow_parent)
else
# Case (3): a * x * y --> *(a, x, y)
# Case (4): 1 * x * y --> *(x, y)
id_mul = data.operators.multivariate_operator_to_id[:*]
push!(expr.nodes, Node(NODE_CALL_MULTIVARIATE, id_mul, parent_index))
mul_parent = length(expr.nodes)
if !isone(x.coefficient)
parse_expression(data, expr, x.coefficient, mul_parent)
end
parse_expression(data, expr, x.variable_1, mul_parent)
parse_expression(data, expr, x.variable_2, mul_parent)
end
parse_expression(data, expr, x.variable_1, mul_parent)
parse_expression(data, expr, x.variable_2, mul_parent)
return
end

Expand Down
12 changes: 6 additions & 6 deletions test/Nonlinear/Nonlinear.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1167,13 +1167,13 @@ function test_scalar_nonlinear_function_parse_scalarquadraticfunction()
MOI.ScalarQuadraticFunction(qterms, terms, 0.0) => :(0.0),
MOI.ScalarQuadraticFunction(qterms, terms, 1.0) => :(1.0),
MOI.ScalarQuadraticFunction(qterms, [aterm], 0.0) => :(2.0 * $x),
(1.0 * x * x + 1.0 * x + 1.0) => :($x * $x + $x + 1),
(1.0 * x * x + 1.0 * x) => :($x * $x + $x),
(1.0 * x * x + 2.0 * x) => :($x * $x + 2.0 * $x),
(2.0 * x * x + 2.0 * x) => :(2.0 * $x * $x + 2.0 * $x),
(1.0 * x * x) => :($x * $x),
(1.0 * x * x + 1.0 * x + 1.0) => :($x^2 + $x + 1),
(1.0 * x * x + 1.0 * x) => :($x^2 + $x),
(1.0 * x * x + 2.0 * x) => :($x^2 + 2.0 * $x),
(2.0 * x * x + 2.0 * x) => :(2.0 * $x^2 + 2.0 * $x),
(1.0 * x * x) => :($x^2),
(1.5 * x * x + 2.5 * x * y + 3.5 * x + 2.0) =>
:(1.5 * $x * $x + 2.5 * $x * $y + 3.5 * $x + 2.0),
:(1.5 * $x^2 + 2.5 * $x * $y + 3.5 * $x + 2.0),
)
nlp_model = MOI.Nonlinear.Model()
f1 = MOI.Nonlinear.add_expression(nlp_model, f)
Expand Down
Loading