Skip to content

Commit 0fcd485

Browse files
authored
[Nonlinear] lazily build ::Expr representaton of subexpressions (#1984)
The main benefit of this is that modifying a parameter no longer needs a call to initialize to reconstruct the expressions.
1 parent 28b90cd commit 0fcd485

File tree

2 files changed

+6
-19
lines changed

2 files changed

+6
-19
lines changed

src/Nonlinear/evaluator.jl

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,13 @@ end
6565

6666
function MOI.initialize(evaluator::Evaluator, features::Vector{Symbol})
6767
empty!(evaluator.ordered_constraints)
68-
empty!(evaluator.julia_expressions)
6968
evaluator.eval_objective_timer = 0.0
7069
evaluator.eval_objective_gradient_timer = 0.0
7170
evaluator.eval_constraint_timer = 0.0
7271
evaluator.eval_constraint_jacobian_timer = 0.0
7372
evaluator.eval_hessian_lagrangian_timer = 0.0
7473
append!(evaluator.ordered_constraints, keys(evaluator.model.constraints))
75-
if :ExprGraph in features
76-
for i in 1:length(evaluator.model.expressions)
77-
push!(
78-
evaluator.julia_expressions,
79-
convert_to_expr(
80-
evaluator,
81-
evaluator.model.expressions[i];
82-
moi_output_format = true,
83-
),
84-
)
85-
end
86-
filter!(f -> f != :ExprGraph, features)
87-
end
74+
filter!(f -> f != :ExprGraph, features)
8875
if evaluator.backend !== nothing
8976
MOI.initialize(evaluator.backend, features)
9077
end
@@ -270,7 +257,11 @@ function _convert_to_moi_format(evaluator::Evaluator, p::ParameterIndex)
270257
end
271258

272259
function _convert_to_moi_format(evaluator::Evaluator, x::ExpressionIndex)
273-
return evaluator.julia_expressions[x.value]
260+
return convert_to_expr(
261+
evaluator,
262+
evaluator.model.expressions[x.value];
263+
moi_output_format = true,
264+
)
274265
end
275266

276267
_convert_to_moi_format(::Evaluator, x) = x

src/Nonlinear/types.jl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,6 @@ mutable struct Evaluator{B} <: MOI.AbstractNLPEvaluator
198198
model::Model
199199
# The abstract-differentiation backend
200200
backend::B
201-
# A vector to store expressions in Julia's Expr form. The eltype is Any
202-
# because expressions may be constants, and `:(1)` is just `1`.
203-
julia_expressions::Vector{Any}
204201
# ordered_constraints is needed because `OrderedDict` doesn't support
205202
# looking up a key by the linear index.
206203
ordered_constraints::Vector{ConstraintIndex}
@@ -221,7 +218,6 @@ mutable struct Evaluator{B} <: MOI.AbstractNLPEvaluator
221218
return new{B}(
222219
model,
223220
backend,
224-
Expr[],
225221
ConstraintIndex[],
226222
Float64[],
227223
0.0,

0 commit comments

Comments
 (0)