Skip to content

Commit c0ca600

Browse files
committed
More updates from matbesancon
1 parent a6f6b18 commit c0ca600

File tree

6 files changed

+56
-17
lines changed

6 files changed

+56
-17
lines changed

Diff for: docs/src/submodules/Nonlinear/reference.md

+1
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,5 @@ Nonlinear.NonlinearExpression
8181
Nonlinear.NonlinearConstraint
8282
Nonlinear.adjacency_matrix
8383
Nonlinear.parse_expression
84+
Nonlinear.convert_to_expr
8485
```

Diff for: src/Nonlinear/Nonlinear.jl

+7-3
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,11 @@ function MOI.initialize(data::NonlinearData, features::Vector{Symbol})
272272
for i in 1:length(data.expressions)
273273
push!(
274274
data.julia_expressions,
275-
_to_expr(data, data.expressions[i]; use_x_ref = true),
275+
convert_to_expr(
276+
data,
277+
data.expressions[i];
278+
moi_output_format = true,
279+
),
276280
)
277281
end
278282
filter!(f -> f != :ExprGraph, features)
@@ -285,12 +289,12 @@ end
285289

286290
function MOI.objective_expr(data::NonlinearData)
287291
@assert data.objective !== nothing
288-
return _to_expr(data, data.objective; use_x_ref = true)
292+
return convert_to_expr(data, data.objective; moi_output_format = true)
289293
end
290294

291295
function MOI.constraint_expr(data::NonlinearData, i::Int)
292296
constraint = data[data.ordered_constraints[i]]
293-
f = _to_expr(data, constraint.expression; use_x_ref = true)
297+
f = convert_to_expr(data, constraint.expression; moi_output_format = true)
294298
if constraint.set isa MOI.LessThan
295299
return :($f <= $(constraint.set.upper))
296300
elseif constraint.set isa MOI.GreaterThan

Diff for: src/Nonlinear/ReverseAD/Coloring/Coloring.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,8 @@ end
433433
seen_idx = IndexedSet(0),
434434
)
435435
436-
edgelist is nonzeros in hessian, *including* nonzeros on the diagonal
436+
`edgelist` contains the nonzeros in the Hessian, *including* nonzeros on the
437+
diagonal.
437438
"""
438439
function hessian_color_preprocess(
439440
edgelist,

Diff for: src/Nonlinear/ReverseAD/types.jl

+5-2
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,11 @@ end
135135
ordered_variables::Vector{MOI.VariableIndex},
136136
)
137137
138-
Return an `MOI.AbstractNLPEvaluator`. Before using, you must initialize the
139-
evaluator using `MOI.initialize`.
138+
Return an `NLPEvaluator` object that implements the `MOI.AbstractNLPEvaluator`
139+
interface.
140+
141+
!!! warning
142+
Before using, you must initialize the evaluator using `MOI.initialize`.
140143
"""
141144
mutable struct NLPEvaluator <: MOI.AbstractNLPEvaluator
142145
data::Nonlinear.NonlinearData

Diff for: src/Nonlinear/operators.jl

+8-4
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,30 @@ function _create_binary_switch(ids, exprs)
1616
end
1717
end
1818

19+
# We use a let block here for `expr` to create a local variable that does not
20+
# persist in the scope of the module. All we care about is the _eval_univariate
21+
# function that is eval'd as a result.
1922
let exprs = map(SYMBOLIC_UNIVARIATE_EXPRESSIONS) do arg
2023
return :(return $(arg[1])(x), $(arg[2]))
2124
end
22-
expr = _create_binary_switch(1:length(exprs), exprs)
2325
@eval @inline function _eval_univariate(id, x::T) where {T}
24-
$(expr)
26+
$(_create_binary_switch(1:length(exprs), exprs))
2527
return error("Invalid operator_id")
2628
end
2729
end
2830

31+
# We use a let block here for `expr` to create a local variable that does not
32+
# persist in the scope of the module. All we care about is the
33+
# _eval_univariate_2nd_deriv function that is eval'd as a result.
2934
let exprs = map(SYMBOLIC_UNIVARIATE_EXPRESSIONS) do arg
3035
if arg === :(nothing) # f''(x) isn't defined
3136
:(error("Invalid operator_id"))
3237
else
3338
:(return $(arg[3]))
3439
end
3540
end
36-
expr = _create_binary_switch(1:length(exprs), exprs)
3741
@eval @inline function _eval_univariate_2nd_deriv(id, x::T) where {T}
38-
$(expr)
42+
$(_create_binary_switch(1:length(exprs), exprs))
3943
return error("Invalid operator_id")
4044
end
4145
end

Diff for: src/Nonlinear/parse.jl

+33-7
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,34 @@ function _expr_to_constraint(expr::Expr)
257257
end
258258
end
259259

260-
function _to_expr(
260+
"""
261+
convert_to_expr(
262+
data::NonlinearData,
263+
expr::NonlinearExpression;
264+
moi_output_format::Bool,
265+
)
266+
267+
Convert the [`NonlinearExpression`](@ref) `expr` into a Julia `Expr`.
268+
269+
If `moi_output_format = true`:
270+
* subexpressions will be converted to Julia `Expr` and substituted into the
271+
output expression.
272+
* the current value of each parameter will be interpolated into the expression
273+
* variables will be represented in the form `x[MOI.VariableIndex(i)]`
274+
275+
If `moi_output_format = false`:
276+
* subexpressions will be represented by a [`ExpressionIndex`](@ref) object.
277+
* parameters will be represented by a [`ParameterIndex`](@ref) object.
278+
* variables will be represennted by an [`MOI.VariableIndex`](@ref) object.
279+
280+
!!! warning
281+
To use `moi_output_format = true`, you must have first called
282+
[`MOI.initialize`](@ref) with `:ExprGraph` as a requested feature.
283+
"""
284+
function convert_to_expr(
261285
data::NonlinearData,
262286
expr::NonlinearExpression;
263-
expand_subexpressions::Bool = true,
264-
use_x_ref::Bool = false,
287+
moi_output_format::Bool,
265288
)
266289
tree = Any[]
267290
for node in expr.nodes
@@ -274,16 +297,19 @@ function _to_expr(
274297
elseif node.type == NODE_LOGIC
275298
Expr(data.operators.logic_operators[node.index])
276299
elseif node.type == NODE_MOI_VARIABLE
277-
x = MOI.VariableIndex(node.index)
278-
use_x_ref ? :(x[$x]) : x
300+
if moi_output_format
301+
:(x[$(MOI.VariableIndex(node.index))])
302+
else
303+
MOI.VariableIndex(node.index)
304+
end
279305
elseif node.type == NODE_PARAMETER
280-
if expand_subexpressions
306+
if moi_output_format
281307
data.parameters[node.index]
282308
else
283309
ParameterIndex(node.index)
284310
end
285311
elseif node.type == NODE_SUBEXPRESSION
286-
if expand_subexpressions
312+
if moi_output_format
287313
data.julia_expressions[node.index]
288314
else
289315
ExpressionIndex(node.index)

0 commit comments

Comments
 (0)