-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperators.jl
More file actions
486 lines (461 loc) · 13.7 KB
/
Copy pathoperators.jl
File metadata and controls
486 lines (461 loc) · 13.7 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
# Largely inspired by MathOptInterface/src/Nonlinear/operators.jl
# Most functions have been copy-pasted and slightly modified to adapt to small changes in OperatorRegistry and Model.
const DEFAULT_MULTIVARIATE_OPERATORS = [
:+,
:-,
:*,
:^,
:/,
:ifelse,
:atan,
:min,
:max,
:vect,
:dot,
:hcat,
:vcat,
:norm,
:sum,
:row,
:reduce,
]
function _validate_register_assumptions(
f::Function,
name::Symbol,
nb_args::Integer,
)
# Assumption 1: check that `f` can be called with `Float64` arguments.
arg = nb_args == 1 ? 0.0 : zeros(nb_args)
if hasmethod(f, Tuple{typeof(arg)})
y = f(arg)
else
error(
"Unable to register the function :$name.\n\n" *
"The function must be able to be called with $nb_args Float64 " *
"arguments, but no method was found for this.",
)
end
if !(y isa Real)
error(
"Expected return type of `Float64` from the user-defined " *
"function :$(name), but got `$(typeof(y))`.",
)
end
# Assumption 2: check that `f` can be differentiated using `ForwardDiff`.
try
if nb_args == 1
ForwardDiff.derivative(f, 0.0)
else
ForwardDiff.gradient(x -> f(x...), zeros(nb_args))
end
catch err
if err isa MethodError
error(
"Unable to register the function :$name.\n\n" *
_FORWARD_DIFF_METHOD_ERROR_HELPER,
)
end
# We hit some other error, perhaps we called a function like log(-1).
# Ignore for now, and hope that a useful error is shown to the user
# during the solve.
end
return
end
function _checked_derivative(f::F, op::Symbol) where {F}
return function (x)
try
return ForwardDiff.derivative(f, x)
catch err
_intercept_ForwardDiff_MethodError(err, op)
end
end
end
"""
check_return_type(::Type{T}, ret::S) where {T,S}
Overload this method for new types `S` to throw an informative error if a
user-defined function returns the type `S` instead of `T`.
"""
check_return_type(::Type{T}, ret::T) where {T} = nothing
function check_return_type(::Type{T}, ret) where {T}
return error(
"Expected return type of $T from a user-defined function, but got " *
"$(typeof(ret)).",
)
end
struct _UnivariateOperator{F,F′,F′′}
f::F
f′::F′
f′′::F′′
function _UnivariateOperator(
f::Function,
f′::Function,
f′′::Union{Nothing,Function} = nothing,
)
return new{typeof(f),typeof(f′),typeof(f′′)}(f, f′, f′′)
end
end
function _UnivariateOperator(op::Symbol, f::Function)
_validate_register_assumptions(f, op, 1)
f′ = _checked_derivative(f, op)
return _UnivariateOperator(op, f, f′)
end
function _UnivariateOperator(op::Symbol, f::Function, f′::Function)
try
_validate_register_assumptions(f′, op, 1)
f′′ = _checked_derivative(f′, op)
return _UnivariateOperator(f, f′, f′′)
catch
return _UnivariateOperator(f, f′, nothing)
end
end
function _UnivariateOperator(::Symbol, f::Function, f′::Function, f′′::Function)
return _UnivariateOperator(f, f′, f′′)
end
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{_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(MOI.Nonlinear.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),
MOI.Nonlinear._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
function eval_logic_function(
::OperatorRegistry,
op::Symbol,
lhs::T,
rhs::T,
)::Bool where {T}
if op == :&&
return lhs && rhs
else
@assert op == :||
return lhs || rhs
end
end
function _generate_eval_univariate()
exprs = map(Nonlinear.DEFAULT_UNIVARIATE_OPERATORS) do op
return :(
return (
value_deriv_and_second($op, x)[1],
value_deriv_and_second($op, x)[2],
)
)
end
return Nonlinear._create_binary_switch(1:length(exprs), exprs)
end
@eval @inline function _eval_univariate(id, x::T) where {T}
$(_generate_eval_univariate())
return error("Invalid id for univariate operator: $id")
end
function _generate_eval_univariate_2nd_deriv()
exprs = map(
arg -> :(return value_deriv_and_second($(arg), x)[3]),
Nonlinear.DEFAULT_UNIVARIATE_OPERATORS,
)
return Nonlinear._create_binary_switch(1:length(exprs), exprs)
end
@eval @inline function _eval_univariate_2nd_deriv(id, x::T) where {T}
$(_generate_eval_univariate_2nd_deriv())
return error("Invalid id for univariate operator: $id")
end
function eval_multivariate_function(
registry::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 eval_multivariate_hessian(
registry::OperatorRegistry,
op::Symbol,
H,
x::AbstractVector{T},
) where {T}
if op in (:+, :-, :ifelse)
return false
end
if op == :*
# f(x) = *(x[i] for i in 1:N)
#
# ∇fᵢ(x) = *(x[j] for j in 1:N if i != j)
#
# ∇fᵢⱼ(x) = *(x[k] for k in 1:N if i != k & j != k)
N = length(x)
if N == 1
# Hessian is zero
elseif N == 2
H[2, 1] = one(T)
else
for i in 1:N, j in (i+1):N
H[j, i] =
prod(x[k] for k in 1:N if k != i && k != j; init = one(T))
end
end
elseif op == :^
# f(x) = x[1]^x[2]
#
# ∇f(x) = x[2]*x[1]^(x[2]-1)
# x[1]^x[2]*log(x[1])
#
# ∇²f(x) = x[2]*(x[2]-1)*x[1]^(x[2]-2)
# x[1]^(x[2]-1)*(x[2]*log(x[1])+1) x[1]^x[2]*log(x[1])^2
ln = x[1] > 0 ? log(x[1]) : NaN
if x[2] == one(T)
H[2, 1] = _nan_to_zero(ln + one(T))
H[2, 2] = _nan_to_zero(x[1] * ln^2)
elseif x[2] == T(2)
H[1, 1] = T(2)
H[2, 1] = _nan_to_zero(x[1] * (T(2) * ln + one(T)))
H[2, 2] = _nan_to_zero(ln^2 * x[1]^2)
else
H[1, 1] = _nan_to_zero(x[2] * (x[2] - 1) * _nan_pow(x[1], x[2] - 2))
H[2, 1] = _nan_to_zero(_nan_pow(x[1], x[2] - 1) * (x[2] * ln + 1))
H[2, 2] = _nan_to_zero(ln^2 * _nan_pow(x[1], x[2]))
end
elseif op == :/
# f(x) = x[1]/x[2]
#
# ∇f(x) = 1/x[2]
# -x[1]/x[2]^2
#
# ∇²(x) = 0.0
# -1/x[2]^2 2x[1]/x[2]^3
d = 1 / x[2]^2
H[2, 1] = -d
H[2, 2] = 2 * x[1] * d / x[2]
elseif op == :atan
# f(x) = atan(y, x)
#
# ∇f(x) = +x/(x^2+y^2)
# -y/(x^2+y^2)
#
# ∇²(x) = -(2xy)/(x^2+y^2)^2
# (y^2-x^2)/(x^2+y^2)^2 (2xy)/(x^2+y^2)^2
base = (x[1]^2 + x[2]^2)^2
H[1, 1] = -2 * x[2] * x[1] / base
H[2, 1] = (x[1]^2 - x[2]^2) / base
H[2, 2] = 2 * x[2] * x[1] / base
elseif op == :min
_, i = findmin(x)
H[i, i] = one(T)
elseif op == :max
_, i = findmax(x)
H[i, i] = one(T)
else
id = registry.multivariate_operator_to_id[op]
offset = id - registry.multivariate_user_operator_start
operator = registry.registered_multivariate_operators[offset]
if operator.∇²f === nothing
error("Hessian is not defined for operator $op")
end
@assert length(x) == operator.N
operator.∇²f(H, x)
end
return true
end
function eval_univariate_function(operator::_UnivariateOperator, x::T) where {T}
ret = operator.f(x)
check_return_type(T, ret)
return ret::T
end
function eval_univariate_gradient(operator::_UnivariateOperator, x::T) where {T}
ret = operator.f′(x)
check_return_type(T, ret)
return ret::T
end
function eval_univariate_hessian(operator::_UnivariateOperator, x::T) where {T}
ret = operator.f′′(x)
check_return_type(T, ret)
return ret::T
end
function eval_univariate_function_and_gradient(
operator::_UnivariateOperator,
x::T,
) where {T}
ret_f = eval_univariate_function(operator, x)
ret_f′ = eval_univariate_gradient(operator, x)
return ret_f, ret_f′
end
function eval_univariate_function_and_gradient(
registry::OperatorRegistry,
id::Integer,
x::T,
) where {T}
if id <= registry.univariate_user_operator_start
return _eval_univariate(id, x)::Tuple{T,T}
end
offset = id - registry.univariate_user_operator_start
operator = registry.registered_univariate_operators[offset]
return eval_univariate_function_and_gradient(operator, x)
end
function eval_multivariate_gradient(
registry::OperatorRegistry,
op::Symbol,
g::AbstractVector{T},
x::AbstractVector{T},
) where {T}
@assert length(g) == length(x)
if op == :+
fill!(g, one(T))
elseif op == :-
g[1] = one(T)
g[2] = -one(T)
elseif op == :*
# Special case performance optimizations for common cases.
if length(x) == 1
g[1] = one(T)
elseif length(x) == 2
g[1] = x[2]
g[2] = x[1]
else
total = prod(x)
if iszero(total)
for i in eachindex(x)
g[i] = prod(x[j] for j in eachindex(x) if i != j)
end
else
for i in eachindex(x)
g[i] = total / x[i]
end
end
end
elseif op == :^
@assert length(x) == 2
if x[2] == one(T)
g[1] = one(T)
elseif x[2] == T(2)
g[1] = T(2) * x[1]
else
g[1] = x[2] * _nan_pow(x[1], x[2] - one(T))
end
if x[1] > zero(T)
g[2] = _nan_pow(x[1], x[2]) * log(x[1])
else
g[2] = T(NaN)
end
elseif op == :/
@assert length(x) == 2
g[1] = one(T) / x[2]
g[2] = -x[1] / x[2]^2
elseif op == :ifelse
@assert length(x) == 3
g[1] = zero(T) # It doesn't matter what this is.
g[2] = x[1] == one(T)
g[3] = x[1] == zero(T)
elseif op == :atan
@assert length(x) == 2
base = x[1]^2 + x[2]^2
g[1] = x[2] / base
g[2] = -x[1] / base
elseif op == :min
fill!(g, zero(T))
_, i = findmin(x)
g[i] = one(T)
elseif op == :max
fill!(g, zero(T))
_, i = findmax(x)
g[i] = one(T)
else
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
operator.∇f(g, x)
end
return
end
function eval_comparison_function(
::OperatorRegistry,
op::Symbol,
lhs::T,
rhs::T,
)::Bool where {T}
if op == :<=
return lhs <= rhs
elseif op == :>=
return lhs >= rhs
elseif op == :(==)
return lhs == rhs
elseif op == :<
return lhs < rhs
else
@assert op == :>
return lhs > rhs
end
end