Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Nov 22, 2024
1 parent 1517124 commit 2796ec8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/nlp_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -673,25 +673,26 @@ function _evaluate_expr(
return convert(Float64, expr)
end

struct _ConcreteExpression{T}
struct _ConcreteExpression
head::Symbol
args::Vector{T}
args::Vector{Real}
_ConcreteExpression(head::Symbol) = new(head, Real[])
end

function _evaluate_expr(
registry::MOI.Nonlinear.OperatorRegistry,
f::Function,
expr::GenericNonlinearExpr,
)
ret = _ConcreteExpression{Float64}(expr.head, Float64[])
ret = _ConcreteExpression(expr.head)
stack = Any[]
for arg in reverse(expr.args)
push!(stack, (ret, arg))
end
while !isempty(stack)
parent, arg = pop!(stack)
if arg isa MOI.ScalarNonlinearFunction
new_ret = _ConcreteExpression{Float64}(arg.head, Float64[])
new_ret = _ConcreteExpression(arg.head)
push!(parent.args, new_ret)
for child in reverse(arg.args)
push!(stack, (new_ret, child))
Expand Down
16 changes: 16 additions & 0 deletions test/test_nlp_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,22 @@ function test_evaluate_expr_stackoverflow()
return
end

function test_evaluate_expr_stackoverflow_user_defined_function()
N = 10_000
f(x, y) = *(x, y)
model = Model()
@variable(model, x[1:N], start = 0)
@operator(model, op_f, 2, f)
y = x[1]
for i in 2:N
y = op_f(x[i], y)
end
@test value(start_value, y) == 0.0
set_start_value.(x, 1.0)
@test value(start_value, y) == 1.0
return
end

function test_nlobjective_with_nlexpr()
model = Model()
@variable(model, x)
Expand Down

0 comments on commit 2796ec8

Please sign in to comment.