-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.jl
More file actions
294 lines (273 loc) · 9.39 KB
/
types.jl
File metadata and controls
294 lines (273 loc) · 9.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# Copyright (c) 2017: Miles Lubin and contributors
# Copyright (c) 2017: Google Inc.
#
# Use of this source code is governed by an MIT-style license that can be found
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.
struct _SubexpressionStorage
nodes::Vector{Nonlinear.Node}
adj::SparseArrays.SparseMatrixCSC{Bool,Int}
sizes::Sizes
const_values::Vector{Float64}
forward_storage::Vector{Float64}
partials_storage::Vector{Float64}
reverse_storage::Vector{Float64}
partials_storage_ϵ::Vector{Float64}
linearity::Linearity
function _SubexpressionStorage(
nodes::Vector{Nonlinear.Node},
adj::SparseArrays.SparseMatrixCSC{Bool,Int},
const_values::Vector{Float64},
partials_storage_ϵ::Vector{Float64},
linearity::Linearity,
)
sizes = _infer_sizes(nodes, adj)
N = _length(sizes)
return new(
nodes,
adj,
_infer_sizes(nodes, adj),
const_values,
zeros(N), # forward_storage,
zeros(N), # partials_storage,
zeros(N), # reverse_storage,
partials_storage_ϵ,
linearity,
)
end
end
# We don't need to store the full vector of `linearity` but we return
# it because it is needed in `compute_hessian_sparsity`.
function _subexpression_and_linearity(
expr::Nonlinear.Expression,
moi_index_to_consecutive_index,
partials_storage_ϵ::Vector{Float64},
d,
)
nodes = _replace_moi_variables(expr.nodes, moi_index_to_consecutive_index)
adj = Nonlinear.adjacency_matrix(nodes)
linearity = if d.want_hess
_classify_linearity(nodes, adj, d.subexpression_linearity)
else
[NONLINEAR]
end
return _SubexpressionStorage(
nodes,
adj,
expr.values,
partials_storage_ϵ,
linearity[1],
),
linearity
end
struct _FunctionStorage
expr::_SubexpressionStorage
grad_sparsity::Vector{Int}
# Nonzero pattern of Hessian matrix
hess_I::Vector{Int}
hess_J::Vector{Int}
rinfo::Coloring.RecoveryInfo # coloring info for hessians
seed_matrix::Matrix{Float64}
# subexpressions which this function depends on, ordered for forward pass.
dependent_subexpressions::Vector{Int}
function _FunctionStorage(
expr::_SubexpressionStorage,
num_variables,
coloring_storage::Coloring.IndexedSet,
want_hess::Bool,
subexpressions::Vector{_SubexpressionStorage},
dependent_subexpressions,
subexpression_edgelist,
subexpression_variables,
linearity::Vector{Linearity},
)
empty!(coloring_storage)
_compute_gradient_sparsity!(coloring_storage, expr.nodes)
for k in dependent_subexpressions
_compute_gradient_sparsity!(
coloring_storage,
subexpressions[k].nodes,
)
end
grad_sparsity = sort!(collect(coloring_storage))
empty!(coloring_storage)
if want_hess
edgelist = _compute_hessian_sparsity(
expr.nodes,
expr.adj,
linearity,
subexpression_edgelist,
subexpression_variables,
)
hess_I, hess_J, rinfo = Coloring.hessian_color_preprocess(
edgelist,
num_variables,
coloring_storage,
)
seed_matrix = Coloring.seed_matrix(rinfo)
return new(
expr,
grad_sparsity,
hess_I,
hess_J,
rinfo,
seed_matrix,
dependent_subexpressions,
)
else
return new(
expr,
grad_sparsity,
Int[],
Int[],
Coloring.RecoveryInfo(),
Array{Float64}(undef, 0, 0),
dependent_subexpressions,
)
end
end
end
"""
Model()
The core datastructure for representing a nonlinear optimization problem.
It has the following fields:
* `objective::Union{Nothing,Expression}` : holds the nonlinear objective
function, if one exists, otherwise `nothing`.
* `expressions::Vector{Expression}` : a vector of expressions in the model.
* `constraints::OrderedDict{ConstraintIndex,Constraint}` : a map from
[`ConstraintIndex`](@ref) to the corresponding [`Constraint`](@ref). An
`OrderedDict` is used instead of a `Vector` to support constraint deletion.
* `parameters::Vector{Float64}` : holds the current values of the parameters.
* `operators::OperatorRegistry` : stores the operators used in the model.
"""
mutable struct Model
objective::Union{Nothing,MOI.Nonlinear.Expression}
expressions::Vector{MOI.Nonlinear.Expression}
constraints::OrderedDict{
MOI.Nonlinear.ConstraintIndex,
MOI.Nonlinear.Constraint,
}
parameters::Vector{Float64}
operators::OperatorRegistry
# This is a private field, used only to increment the ConstraintIndex.
last_constraint_index::Int64
function Model()
model = new(
nothing,
MOI.Nonlinear.Expression[],
OrderedDict{
MOI.Nonlinear.ConstraintIndex,
MOI.Nonlinear.Constraint,
}(),
Float64[],
OperatorRegistry(),
0,
)
return model
end
end
mutable struct Evaluator{B} <: MOI.AbstractNLPEvaluator
# The internal datastructure.
model::Model
# The abstract-differentiation backend
backend::B
# ordered_constraints is needed because `OrderedDict` doesn't support
# looking up a key by the linear index.
ordered_constraints::Vector{MOI.Nonlinear.ConstraintIndex}
# Storage for the NLPBlockDual, so that we can query the dual of individual
# constraints without needing to query the full vector each time.
constraint_dual::Vector{Float64}
# Timers
initialize_timer::Float64
eval_objective_timer::Float64
eval_constraint_timer::Float64
eval_objective_gradient_timer::Float64
eval_constraint_gradient_timer::Float64
eval_constraint_jacobian_timer::Float64
eval_hessian_objective_timer::Float64
eval_hessian_constraint_timer::Float64
eval_hessian_lagrangian_timer::Float64
function Evaluator(
model::Model,
backend::B = nothing,
) where {B<:Union{Nothing,MOI.AbstractNLPEvaluator}}
return new{B}(
model,
backend,
MOI.ConstraintIndex[],
Float64[],
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
)
end
end
"""
MOI.NLPBlockData(evaluator::Evaluator)
Create an [`MOI.NLPBlockData`](@ref) object from an [`Evaluator`](@ref)
object.
"""
function MOI.NLPBlockData(evaluator::Evaluator)
return MOI.NLPBlockData(
[_bound(c.set) for (_, c) in evaluator.model.constraints],
evaluator,
evaluator.model.objective !== nothing,
)
end
_bound(s::MOI.LessThan) = MOI.NLPBoundsPair(-Inf, s.upper)
_bound(s::MOI.GreaterThan) = MOI.NLPBoundsPair(s.lower, Inf)
_bound(s::MOI.EqualTo) = MOI.NLPBoundsPair(s.value, s.value)
_bound(s::MOI.Interval) = MOI.NLPBoundsPair(s.lower, s.upper)
"""
NLPEvaluator(
model::Nonlinear.Model,
ordered_variables::Vector{MOI.VariableIndex},
)
Return an `NLPEvaluator` object that implements the `MOI.AbstractNLPEvaluator`
interface.
!!! warning
Before using, you must initialize the evaluator using `MOI.initialize`.
"""
mutable struct NLPEvaluator <: MOI.AbstractNLPEvaluator
data::Model
ordered_variables::Vector{MOI.VariableIndex}
objective::Union{Nothing,_FunctionStorage}
constraints::Vector{_FunctionStorage}
subexpressions::Vector{_SubexpressionStorage}
subexpression_order::Vector{Int}
# Storage for the subexpressions in reverse-mode automatic differentiation.
subexpression_forward_values::Vector{Float64}
subexpression_reverse_values::Vector{Float64}
subexpression_linearity::Vector{Linearity}
# A cache of the last x. This is used to guide whether we need to re-run
# reverse-mode automatic differentiation.
last_x::Vector{Float64}
# Temporary storage for computing Jacobians. This is also used as temporary
# storage for the input of multivariate functions.
jac_storage::Vector{Float64}
# Temporary storage for the gradient of multivariate functions
user_output_buffer::Vector{Float64}
# storage for computing hessians
# these Float64 vectors are reinterpreted to hold multiple epsilon components
# so the length should be multiplied by the maximum number of epsilon components
disable_2ndorder::Bool # don't offer Hess or HessVec
want_hess::Bool
storage_ϵ::Vector{Float64} # (longest expression including subexpressions)
input_ϵ::Vector{Float64} # (number of variables)
output_ϵ::Vector{Float64} # (number of variables)
subexpression_forward_values_ϵ::Vector{Float64} # (number of subexpressions)
subexpression_reverse_values_ϵ::Vector{Float64} # (number of subexpressions)
hessian_sparsity::Vector{Tuple{Int64,Int64}}
max_chunk::Int # chunk size for which we've allocated storage
function NLPEvaluator(
data::Model,
ordered_variables::Vector{MOI.VariableIndex},
)
return new(data, ordered_variables)
end
end