Skip to content

Commit a80f0ab

Browse files
committed
Fixes
1 parent e0f6ac1 commit a80f0ab

File tree

2 files changed

+45
-33
lines changed

2 files changed

+45
-33
lines changed

src/Nonlinear/ReverseAD/mathoptinterface_api.jl

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,11 @@ function MOI.initialize(d::NLPEvaluator, requested_features::Vector{Symbol})
6666
# Only load expressions which actually are used
6767
d.subexpression_forward_values[k] = NaN
6868
expr = d.data.expressions[k]
69-
adj = Nonlinear.adjacency_matrix(expr.nodes)
70-
linearity = if d.want_hess
71-
_classify_linearity(expr.nodes, adj, d.subexpression_linearity)
72-
else
73-
[NONLINEAR]
74-
end
75-
subex = _SubexpressionStorage(
69+
subex, _ = _subexpression_and_linearity(
7670
expr,
77-
adj,
7871
moi_index_to_consecutive_index,
7972
Float64[],
80-
linearity[1],
73+
d,
8174
)
8275
d.subexpressions[k] = subex
8376
d.subexpression_linearity[k] = subex.linearity
@@ -112,20 +105,14 @@ function MOI.initialize(d::NLPEvaluator, requested_features::Vector{Symbol})
112105
shared_partials_storage_ϵ = Float64[]
113106
if d.data.objective !== nothing
114107
expr = something(d.data.objective)
115-
adj = Nonlinear.adjacency_matrix(expr.nodes)
116-
linearity = if d.want_hess
117-
_classify_linearity(expr.nodes, adj, d.subexpression_linearity)
118-
else
119-
[NONLINEAR]
120-
end
108+
subexpr, linearity = _subexpression_and_linearity(
109+
expr,
110+
moi_index_to_consecutive_index,
111+
shared_partials_storage_ϵ,
112+
d,
113+
)
121114
objective = _FunctionStorage(
122-
_SubexpressionStorage(
123-
expr,
124-
adj,
125-
moi_index_to_consecutive_index,
126-
shared_partials_storage_ϵ,
127-
linearity[1],
128-
),
115+
subexpr,
129116
N,
130117
coloring_storage,
131118
d.want_hess,
@@ -142,12 +129,12 @@ function MOI.initialize(d::NLPEvaluator, requested_features::Vector{Symbol})
142129
for (k, (_, constraint)) in enumerate(d.data.constraints)
143130
idx = d.data.objective !== nothing ? k + 1 : k
144131
expr = constraint.expression
145-
adj = Nonlinear.adjacency_matrix(expr.nodes)
146-
linearity = if d.want_hess
147-
_classify_linearity(expr.nodes, adj, d.subexpression_linearity)
148-
else
149-
[NONLINEAR]
150-
end
132+
subexpr, linearity = _subexpression_and_linearity(
133+
expr,
134+
moi_index_to_consecutive_index,
135+
shared_partials_storage_ϵ,
136+
d,
137+
)
151138
push!(
152139
d.constraints,
153140
_FunctionStorage(

src/Nonlinear/ReverseAD/types.jl

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,17 @@ struct _SubexpressionStorage
1515
linearity::Linearity
1616

1717
function _SubexpressionStorage(
18-
expr::Nonlinear.Expression,
18+
nodes::Vector{Nonlinear.Node},
1919
adj::SparseArrays.SparseMatrixCSC{Bool,Int},
20-
moi_index_to_consecutive_index,
20+
const_values::Vector{Float64},
2121
partials_storage_ϵ::Vector{Float64},
2222
linearity::Linearity,
2323
)
24-
nodes =
25-
_replace_moi_variables(expr.nodes, moi_index_to_consecutive_index)
2624
N = length(nodes)
2725
return new(
2826
nodes,
2927
adj,
30-
expr.values,
28+
const_values,
3129
zeros(N), # forward_storage,
3230
zeros(N), # partials_storage,
3331
zeros(N), # reverse_storage,
@@ -37,6 +35,31 @@ struct _SubexpressionStorage
3735
end
3836
end
3937

38+
# We don't need to store the full vector of `linearity` but we return
39+
# it because it is needed in `compute_hessian_sparsity`.
40+
function _subexpression_and_linearity(
41+
expr::Nonlinear.Expression,
42+
moi_index_to_consecutive_index,
43+
partials_storage_ϵ::Vector{Float64},
44+
d,
45+
)
46+
nodes =
47+
_replace_moi_variables(expr.nodes, moi_index_to_consecutive_index)
48+
adj = Nonlinear.adjacency_matrix(nodes)
49+
linearity = if d.want_hess
50+
_classify_linearity(nodes, adj, d.subexpression_linearity)
51+
else
52+
[NONLINEAR]
53+
end
54+
return _SubexpressionStorage(
55+
nodes,
56+
adj,
57+
expr.values,
58+
partials_storage_ϵ,
59+
linearity[1],
60+
), linearity
61+
end
62+
4063
struct _FunctionStorage
4164
expr::_SubexpressionStorage
4265
grad_sparsity::Vector{Int}
@@ -78,11 +101,13 @@ struct _FunctionStorage
78101
subexpression_edgelist,
79102
subexpression_variables,
80103
)
104+
@show edgelist
81105
hess_I, hess_J, rinfo = Coloring.hessian_color_preprocess(
82106
edgelist,
83107
num_variables,
84108
coloring_storage,
85109
)
110+
@show hess_I, hess_J
86111
seed_matrix = Coloring.seed_matrix(rinfo)
87112
return new(
88113
expr,

0 commit comments

Comments
 (0)