-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathtypes.jl
More file actions
319 lines (278 loc) · 9.08 KB
/
types.jl
File metadata and controls
319 lines (278 loc) · 9.08 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# 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.
"""
NodeType
An enum describing the possible node types. Each [`Node`](@ref) has a `.index`
field, which should be interpreted as follows:
* `NODE_CALL_MULTIVARIATE`: the index into `operators.multivariate_operators`
* `NODE_CALL_UNIVARIATE`: the index into `operators.univariate_operators`
* `NODE_LOGIC`: the index into `operators.logic_operators`
* `NODE_COMPARISON`: the index into `operators.comparison_operators`
* `NODE_MOI_VARIABLE`: the value of `MOI.VariableIndex(index)` in the user's
space of the model.
* `NODE_VARIABLE`: the 1-based index of the internal vector
* `NODE_VALUE`: the index into the `.values` field of `Expression`
* `NODE_PARAMETER`: the index into `data.parameters`
* `NODE_SUBEXPRESSION`: the index into `data.expressions`
"""
@enum(
NodeType,
# Index into multivariate operators
NODE_CALL_MULTIVARIATE,
# Index into univariate operators
NODE_CALL_UNIVARIATE,
# Index into logic operators
NODE_LOGIC,
# Index into comparison operators
NODE_COMPARISON,
# Index is the value of `MOI.VariableIndex`. This is from the original
# model, and is not consecutive.
NODE_MOI_VARIABLE,
# Index of the internal, consecutive, and ordered `MOI.VariableIndex`.
NODE_VARIABLE,
# Index is into the list of constants
NODE_VALUE,
# Index is into the list of parameters
NODE_PARAMETER,
# Index is into the list of subexpressions
NODE_SUBEXPRESSION,
)
"""
struct Node
type::NodeType
index::Int
parent::Int
end
A single node in a nonlinear expression tree. Used by
[`Expression`](@ref).
See the MathOptInterface documentation for information on how the nodes and
values form an expression tree.
"""
struct Node
type::NodeType
index::Int
parent::Int
end
"""
struct Expression
nodes::Vector{Node}
values::Vector{Float64}
end
The core type that represents a nonlinear expression. See the MathOptInterface
documentation for information on how the nodes and values form an expression
tree.
"""
struct Expression
nodes::Vector{Node}
values::Vector{Float64}
end
Expression() = Expression(Node[], Float64[])
function Base.:(==)(x::Expression, y::Expression)
return x.nodes == y.nodes && x.values == y.values
end
"""
struct Constraint
expression::Expression
set::Union{
MOI.LessThan{Float64},
MOI.GreaterThan{Float64},
MOI.EqualTo{Float64},
MOI.Interval{Float64},
}
end
A type to hold information relating to the nonlinear constraint `f(x) in S`,
where `f(x)` is defined by `.expression`, and `S` is `.set`.
"""
struct Constraint
expression::Expression
set::Union{
MOI.LessThan{Float64},
MOI.GreaterThan{Float64},
MOI.EqualTo{Float64},
MOI.Interval{Float64},
}
end
"""
ParameterIndex
An index to a nonlinear parameter that is returned by [`add_parameter`](@ref).
Given `data::Model` and `p::ParameterIndex`, use `data[p]` to retrieve
the current value of the parameter and `data[p] = value` to set a new value.
"""
struct ParameterIndex
value::Int
end
"""
ExpressionIndex
An index to a nonlinear expression that is returned by [`add_expression`](@ref).
Given `data::Model` and `ex::ExpressionIndex`, use `data[ex]` to
retrieve the corresponding [`Expression`](@ref).
"""
struct ExpressionIndex
value::Int
end
"""
ConstraintIndex
An index to a nonlinear constraint that is returned by [`add_constraint`](@ref).
Given `data::Model` and `c::ConstraintIndex`, use `data[c]` to
retrieve the corresponding [`Constraint`](@ref).
"""
struct ConstraintIndex
value::Int
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,Expression}
expressions::Vector{Expression}
constraints::OrderedDict{ConstraintIndex,Constraint}
parameters::Vector{Float64}
operators::OperatorRegistry
# This is a private field, used only to increment the ConstraintIndex.
last_constraint_index::Int64
# This is a private field, used to detect common subexpressions.
cache::Dict{
MOI.ScalarNonlinearFunction,
Union{ExpressionIndex,Tuple{Expression,Int}},
}
function Model()
return new(
nothing,
Expression[],
OrderedDict{ConstraintIndex,Constraint}(),
Float64[],
OperatorRegistry(),
0,
Dict{
MOI.ScalarNonlinearFunction,
Union{ExpressionIndex,Tuple{Expression,Int}},
}(),
)
end
end
"""
AbstractAutomaticDifferentiation
An abstract type for extending [`Evaluator`](@ref).
"""
abstract type AbstractAutomaticDifferentiation end
function MOI.Utilities.map_indices(
::F,
backend::AbstractAutomaticDifferentiation,
) where {F<:Function}
return backend
end
"""
Evaluator(
model::Model,
backend::AbstractAutomaticDifferentiation,
ordered_variables::Vector{MOI.VariableIndex},
)
Create `Evaluator`, a subtype of `MOI.AbstractNLPEvaluator`, from `Model`.
"""
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{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,
ConstraintIndex[],
Float64[],
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
)
end
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)
"""
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
"""
ExprGraphOnly() <: AbstractAutomaticDifferentiation
The default implementation of `AbstractAutomaticDifferentiation`. The only
supported feature is `:ExprGraph`.
"""
struct ExprGraphOnly <: AbstractAutomaticDifferentiation end
function Evaluator(model::Model, ::ExprGraphOnly, ::Vector{MOI.VariableIndex})
return Evaluator(model)
end
"""
SparseReverseMode() <: AbstractAutomaticDifferentiation
An implementation of `AbstractAutomaticDifferentiation` that uses sparse
reverse-mode automatic differentiation to compute derivatives. Supports all
features in the MOI nonlinear interface.
"""
struct SparseReverseMode <: AbstractAutomaticDifferentiation end
function Evaluator(
model::Model,
::SparseReverseMode,
ordered_variables::Vector{MOI.VariableIndex},
)
return Evaluator(model, ReverseAD.NLPEvaluator(model, ordered_variables))
end
"""
SymbolicMode() <: AbstractAutomaticDifferentiation
A type for setting as the value of the `MOI.AutomaticDifferentiationBackend()`
attribute to enable symbolic automatic differentiation.
"""
struct SymbolicMode <: AbstractAutomaticDifferentiation end
function Evaluator(
model::Model,
::SymbolicMode,
ordered_variables::Vector{MOI.VariableIndex},
)
return Evaluator(model, SymbolicAD.Evaluator(model, ordered_variables))
end