Skip to content

Commit 36539bc

Browse files
committed
Fixes
1 parent d1a0e2e commit 36539bc

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

src/Nonlinear/ReverseAD/mathoptinterface_api.jl

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,19 @@ function MOI.initialize(d::NLPEvaluator, requested_features::Vector{Symbol})
6565
for k in d.subexpression_order
6666
# Only load expressions which actually are used
6767
d.subexpression_forward_values[k] = NaN
68+
nodes = d.data.expressions[k]
69+
adj = Nonlinear.adjacency_matrix(nodes)
70+
linearity = if d.want_hess
71+
_classify_linearity(nodes, adj, d.subexpression_linearity)[1]
72+
else
73+
NONLINEAR
74+
end
6875
subex = _SubexpressionStorage(
6976
d.data.expressions[k],
70-
d.subexpression_linearity,
77+
adj,
7178
moi_index_to_consecutive_index,
7279
Float64[],
73-
d.want_hess,
80+
linearity[1],
7481
)
7582
d.subexpressions[k] = subex
7683
d.subexpression_linearity[k] = subex.linearity
@@ -104,13 +111,20 @@ function MOI.initialize(d::NLPEvaluator, requested_features::Vector{Symbol})
104111
max_chunk = 1
105112
shared_partials_storage_ϵ = Float64[]
106113
if d.data.objective !== nothing
114+
nodes = something(d.data.objective)
115+
adj = Nonlinear.adjacency_matrix(nodes)
116+
linearity = if d.want_hess
117+
_classify_linearity(nodes, adj, d.subexpression_linearity)[1]
118+
else
119+
NONLINEAR
120+
end
107121
objective = _FunctionStorage(
108122
_SubexpressionStorage(
109-
something(d.data.objective),
110-
d.subexpression_linearity,
123+
nodes,
124+
adj,
111125
moi_index_to_consecutive_index,
112126
shared_partials_storage_ϵ,
113-
d.want_hess,
127+
linearity[1],
114128
),
115129
N,
116130
coloring_storage,
@@ -119,22 +133,30 @@ function MOI.initialize(d::NLPEvaluator, requested_features::Vector{Symbol})
119133
individual_order[1],
120134
subexpression_edgelist,
121135
subexpression_variables,
136+
linearity,
122137
)
123138
max_expr_length = max(max_expr_length, length(objective.nodes))
124139
max_chunk = max(max_chunk, size(objective.seed_matrix, 2))
125140
d.objective = objective
126141
end
127142
for (k, (_, constraint)) in enumerate(d.data.constraints)
128143
idx = d.data.objective !== nothing ? k + 1 : k
144+
nodes = main_expressions[idx]
145+
adj = Nonlinear.adjacency_matrix(nodes)
146+
linearity = if d.want_hess
147+
_classify_linearity(nodes, adj, d.subexpression_linearity)[1]
148+
else
149+
NONLINEAR
150+
end
129151
push!(
130152
d.constraints,
131153
_FunctionStorage(
132154
_SubexpressionStorage(
133-
main_expressions[idx],
134-
d.subexpression_linearity,
155+
nodes,
156+
adj,
135157
moi_index_to_consecutive_index,
136158
shared_partials_storage_ϵ,
137-
d.want_hess,
159+
linearity[1],
138160
),
139161
N,
140162
coloring_storage,
@@ -143,6 +165,7 @@ function MOI.initialize(d::NLPEvaluator, requested_features::Vector{Symbol})
143165
individual_order[idx],
144166
subexpression_edgelist,
145167
subexpression_variables,
168+
linearity,
146169
),
147170
)
148171
max_expr_length = max(max_expr_length, length(d.constraints[end].nodes))

src/Nonlinear/ReverseAD/types.jl

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,14 @@ struct _SubexpressionStorage
1616

1717
function _SubexpressionStorage(
1818
expr::Nonlinear.Expression,
19-
subexpression_linearity,
19+
adj::SparseArrays.SparseMatrixCSC{Bool,Int},
2020
moi_index_to_consecutive_index,
2121
partials_storage_ϵ::Vector{Float64},
22-
want_hess::Bool,
22+
linearity::Linearity,
2323
)
2424
nodes =
2525
_replace_moi_variables(expr.nodes, moi_index_to_consecutive_index)
26-
adj = Nonlinear.adjacency_matrix(nodes)
2726
N = length(nodes)
28-
linearity = if want_hess
29-
_classify_linearity(nodes, adj, subexpression_linearity)[1]
30-
else
31-
NONLINEAR
32-
end
3327
return new(
3428
nodes,
3529
adj,
@@ -63,6 +57,7 @@ struct _FunctionStorage
6357
dependent_subexpressions,
6458
subexpression_edgelist,
6559
subexpression_variables,
60+
linearity::Vector{Linearity},
6661
)
6762
empty!(coloring_storage)
6863
_compute_gradient_sparsity!(coloring_storage, expr.nodes)
@@ -78,7 +73,7 @@ struct _FunctionStorage
7873
edgelist = _compute_hessian_sparsity(
7974
expr.nodes,
8075
expr.adj,
81-
expr.linearity,
76+
linearity,
8277
coloring_storage,
8378
subexpression_edgelist,
8479
subexpression_variables,
@@ -100,6 +95,7 @@ struct _FunctionStorage
10095
)
10196
else
10297
return new(
98+
expr,
10399
grad_sparsity,
104100
Int[],
105101
Int[],

0 commit comments

Comments
 (0)