-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMOI_Nonlinear_fork.jl
More file actions
336 lines (321 loc) · 9.56 KB
/
MOI_Nonlinear_fork.jl
File metadata and controls
336 lines (321 loc) · 9.56 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# Inspired by MathOptInterface/src/Nonlinear/parse_expression.jl
const DEFAULT_MULTIVARIATE_OPERATORS = [
:+,
:-,
:*,
:^,
:/,
:ifelse,
:atan,
:min,
:max,
:vect,
:dot,
:hcat,
:vcat,
:norm,
:sum,
:row,
]
struct OperatorRegistry
# NODE_CALL_UNIVARIATE
univariate_operators::Vector{Symbol}
univariate_operator_to_id::Dict{Symbol,Int}
univariate_user_operator_start::Int
registered_univariate_operators::Vector{MOI.Nonlinear._UnivariateOperator}
# NODE_CALL_MULTIVARIATE
multivariate_operators::Vector{Symbol}
multivariate_operator_to_id::Dict{Symbol,Int}
multivariate_user_operator_start::Int
registered_multivariate_operators::Vector{
MOI.Nonlinear._MultivariateOperator,
}
# NODE_LOGIC
logic_operators::Vector{Symbol}
logic_operator_to_id::Dict{Symbol,Int}
# NODE_COMPARISON
comparison_operators::Vector{Symbol}
comparison_operator_to_id::Dict{Symbol,Int}
function OperatorRegistry()
univariate_operators = copy(DEFAULT_UNIVARIATE_OPERATORS)
multivariate_operators = copy(DEFAULT_MULTIVARIATE_OPERATORS)
logic_operators = [:&&, :||]
comparison_operators = [:<=, :(==), :>=, :<, :>]
return new(
# NODE_CALL_UNIVARIATE
univariate_operators,
Dict{Symbol,Int}(
op => i for (i, op) in enumerate(univariate_operators)
),
length(univariate_operators),
_UnivariateOperator[],
# NODE_CALL
multivariate_operators,
Dict{Symbol,Int}(
op => i for (i, op) in enumerate(multivariate_operators)
),
length(multivariate_operators),
_MultivariateOperator[],
# NODE_LOGIC
logic_operators,
Dict{Symbol,Int}(op => i for (i, op) in enumerate(logic_operators)),
# NODE_COMPARISON
comparison_operators,
Dict{Symbol,Int}(
op => i for (i, op) in enumerate(comparison_operators)
),
)
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 = MOI.Nonlinear.Model()
ops = [:vect, :dot, :hcat, :vcat, :norm, :sum, :row]
start = length(model.operators.multivariate_operators)
append!(model.operators.multivariate_operators, ops)
for (i, op) in enumerate(ops)
model.operators.multivariate_operator_to_id[op] = start + i
end
return model
end
end
function set_objective(model::MOI.Nonlinear.Model, obj)
model.objective = parse_expression(model, obj)
return
end
function parse_expression(data::MOI.Nonlinear.Model, input)
expr = MOI.Nonlinear.Expression()
parse_expression(data, expr, input, -1)
return expr
end
function parse_expression(data, expr, item, parent)
return MOI.Nonlinear.parse_expression(data, expr, item, parent)
end
function parse_expression(
data::MOI.Nonlinear.Model,
expr::MOI.Nonlinear.Expression,
x::Expr,
parent_index::Int,
)
stack = Tuple{Int,Any}[]
push!(stack, (parent_index, x))
while !isempty(stack)
parent, item = pop!(stack)
if item isa Expr
_parse_expression(stack, data, expr, item, parent)
else
parse_expression(data, expr, item, parent)
end
end
return
end
function _parse_expression(stack, data, expr, x, parent_index)
if Meta.isexpr(x, :call)
if length(x.args) == 2 && !Meta.isexpr(x.args[2], :...)
MOI.Nonlinear._parse_univariate_expression(
stack,
data,
expr,
x,
parent_index,
)
else
# The call is either n-ary, or it is a splat, in which case we
# cannot tell just yet whether the expression is unary or nary.
# Punt to multivariate and try to recover later.
MOI.Nonlinear._parse_multivariate_expression(
stack,
data,
expr,
x,
parent_index,
)
end
elseif Meta.isexpr(x, :comparison)
MOI.Nonlinear._parse_comparison_expression(
stack,
data,
expr,
x,
parent_index,
)
elseif Meta.isexpr(x, :...)
MOI.Nonlinear._parse_splat_expression(
stack,
data,
expr,
x,
parent_index,
)
elseif Meta.isexpr(x, :&&) || Meta.isexpr(x, :||)
MOI.Nonlinear._parse_logic_expression(
stack,
data,
expr,
x,
parent_index,
)
elseif Meta.isexpr(x, :vect)
_parse_vect_expression(stack, data, expr, x, parent_index)
elseif Meta.isexpr(x, :hcat)
_parse_hcat_expression(stack, data, expr, x, parent_index)
elseif Meta.isexpr(x, :vcat)
_parse_vcat_expression(stack, data, expr, x, parent_index)
elseif Meta.isexpr(x, :row)
_parse_row_expression(stack, data, expr, x, parent_index)
else
error("Unsupported expression: $x")
end
end
function eval_multivariate_function(
registry::MOI.Nonlinear.OperatorRegistry,
op::Symbol,
x::AbstractVector{T},
) where {T}
if op == :+
return sum(x; init = zero(T))
elseif op == :-
@assert length(x) == 2
return x[1] - x[2]
elseif op == :*
return prod(x; init = one(T))
elseif op == :^
@assert length(x) == 2
# Use _nan_pow here to avoid throwing an error in common situations like
# (-1.0)^1.5.
return _nan_pow(x[1], x[2])
elseif op == :/
@assert length(x) == 2
return x[1] / x[2]
elseif op == :ifelse
@assert length(x) == 3
return ifelse(Bool(x[1]), x[2], x[3])
elseif op == :atan
@assert length(x) == 2
return atan(x[1], x[2])
elseif op == :min
return minimum(x)
elseif op == :max
return maximum(x)
elseif op == :vect
return x
end
id = registry.multivariate_operator_to_id[op]
offset = id - registry.multivariate_user_operator_start
operator = registry.registered_multivariate_operators[offset]
@assert length(x) == operator.N
ret = operator.f(x)
MOI.Nonlinear.check_return_type(T, ret)
return ret::T
end
function _parse_vect_expression(
stack::Vector{Tuple{Int,Any}},
data::MOI.Nonlinear.Model,
expr::MOI.Nonlinear.Expression,
x::Expr,
parent_index::Int,
)
@assert Meta.isexpr(x, :vect)
id = get(data.operators.multivariate_operator_to_id, :vect, nothing)
push!(
expr.nodes,
MOI.Nonlinear.Node(
MOI.Nonlinear.NODE_CALL_MULTIVARIATE,
id,
parent_index,
),
)
for i in length(x.args):-1:1
push!(stack, (length(expr.nodes), x.args[i]))
end
return
end
function _parse_row_expression(
stack::Vector{Tuple{Int,Any}},
data::MOI.Nonlinear.Model,
expr::MOI.Nonlinear.Expression,
x::Expr,
parent_index::Int,
)
@assert Meta.isexpr(x, :row)
id = get(data.operators.multivariate_operator_to_id, :row, nothing)
push!(
expr.nodes,
MOI.Nonlinear.Node(
MOI.Nonlinear.NODE_CALL_MULTIVARIATE,
id,
parent_index,
),
)
for i in length(x.args):-1:1
push!(stack, (length(expr.nodes), x.args[i]))
end
return
end
function _parse_hcat_expression(
stack::Vector{Tuple{Int,Any}},
data::MOI.Nonlinear.Model,
expr::MOI.Nonlinear.Expression,
x::Expr,
parent_index::Int,
)
@assert Meta.isexpr(x, :hcat)
id = get(data.operators.multivariate_operator_to_id, :hcat, nothing)
push!(
expr.nodes,
MOI.Nonlinear.Node(
MOI.Nonlinear.NODE_CALL_MULTIVARIATE,
id,
parent_index,
),
)
for i in length(x.args):-1:1
push!(stack, (length(expr.nodes), x.args[i]))
end
return
end
function _parse_vcat_expression(
stack::Vector{Tuple{Int,Any}},
data::MOI.Nonlinear.Model,
expr::MOI.Nonlinear.Expression,
x::Expr,
parent_index::Int,
)
@assert Meta.isexpr(x, :vcat)
id = get(data.operators.multivariate_operator_to_id, :vcat, nothing)
push!(
expr.nodes,
MOI.Nonlinear.Node(
MOI.Nonlinear.NODE_CALL_MULTIVARIATE,
id,
parent_index,
),
)
for i in length(x.args):-1:1
push!(stack, (length(expr.nodes), x.args[i]))
end
return
end