forked from SciML/Optimization.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOptimizationFiniteDiffExt.jl
233 lines (213 loc) · 7.99 KB
/
OptimizationFiniteDiffExt.jl
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
module OptimizationFiniteDiffExt
import Optimization, Optimization.ArrayInterface
import Optimization.SciMLBase: OptimizationFunction
import Optimization.ADTypes: AutoFiniteDiff
using Optimization.LinearAlgebra
isdefined(Base, :get_extension) ? (using FiniteDiff) : (using ..FiniteDiff)
const FD = FiniteDiff
function Optimization.instantiate_function(f, x, adtype::AutoFiniteDiff, p,
num_cons = 0)
_f = (θ, args...) -> first(f.f(θ, p, args...))
updatecache = (cache, x) -> (cache.xmm .= x; cache.xmp .= x; cache.xpm .= x; cache.xpp .= x; return cache)
if f.grad === nothing
gradcache = FD.GradientCache(x, x, adtype.fdtype)
grad = (res, θ, args...) -> FD.finite_difference_gradient!(res, x -> _f(x, args...),
θ, gradcache)
else
grad = (G, θ, args...) -> f.grad(G, θ, p, args...)
end
if f.hess === nothing
hesscache = FD.HessianCache(x, adtype.fdhtype)
hess = (res, θ, args...) -> FD.finite_difference_hessian!(res,
x -> _f(x, args...), θ,
updatecache(hesscache, θ))
else
hess = (H, θ, args...) -> f.hess(H, θ, p, args...)
end
if f.hv === nothing
hv = function (H, θ, v, args...)
T = eltype(θ)
ϵ = sqrt(eps(real(T))) * max(one(real(T)), abs(norm(θ)))
@. θ += ϵ * v
cache2 = similar(θ)
grad(cache2, θ, args...)
@. θ -= 2ϵ * v
cache3 = similar(θ)
grad(cache3, θ, args...)
@. θ += ϵ * v
@. H = (cache2 - cache3) / (2ϵ)
end
else
hv = f.hv
end
if f.cons === nothing
cons = nothing
else
cons = (res, θ) -> f.cons(res, θ, p)
end
cons_jac_colorvec = f.cons_jac_colorvec === nothing ? (1:length(x)) :
f.cons_jac_colorvec
if cons !== nothing && f.cons_j === nothing
cons_j = function (J, θ)
y0 = zeros(num_cons)
jaccache = FD.JacobianCache(copy(x), copy(y0), copy(y0), adtype.fdjtype;
colorvec = cons_jac_colorvec,
sparsity = f.cons_jac_prototype)
FD.finite_difference_jacobian!(J, cons, θ, jaccache)
end
else
cons_j = (J, θ) -> f.cons_j(J, θ, p)
end
if cons !== nothing && f.cons_h === nothing
hess_cons_cache = [FD.HessianCache(copy(x), adtype.fdhtype)
for i in 1:num_cons]
cons_h = function (res, θ)
for i in 1:num_cons#note: colorvecs not yet supported by FiniteDiff for Hessians
FD.finite_difference_hessian!(res[i],
(x) -> (_res = zeros(eltype(θ), num_cons);
cons(_res, x);
_res[i]), θ,
updatecache(hess_cons_cache[i], θ))
end
end
else
cons_h = (res, θ) -> f.cons_h(res, θ, p)
end
if f.lag_h === nothing
lag_hess_cache = FD.HessianCache(copy(x), adtype.fdhtype)
c = zeros(num_cons)
h = zeros(length(x), length(x))
lag_h = let c = c, h = h
lag = function (θ, σ, μ)
f.cons(c, θ, p)
l = μ'c
if !iszero(σ)
l += σ * f.f(θ, p)
end
l
end
function (res, θ, σ, μ)
FD.finite_difference_hessian!(res,
(x) -> lag(x, σ, μ),
θ,
updatecache(lag_hess_cache, θ))
end
end
else
lag_h = (res, θ, σ, μ) -> f.lag_h(res, θ, σ, μ, p)
end
return OptimizationFunction{true}(f, adtype; grad = grad, hess = hess, hv = hv,
cons = cons, cons_j = cons_j, cons_h = cons_h,
cons_jac_colorvec = cons_jac_colorvec,
hess_prototype = f.hess_prototype,
cons_jac_prototype = f.cons_jac_prototype,
cons_hess_prototype = f.cons_hess_prototype,
lag_h, f.lag_hess_prototype)
end
function Optimization.instantiate_function(f, cache::Optimization.ReInitCache,
adtype::AutoFiniteDiff, num_cons = 0)
_f = (θ, args...) -> first(f.f(θ, cache.p, args...))
updatecache = (cache, x) -> (cache.xmm .= x; cache.xmp .= x; cache.xpm .= x; cache.xpp .= x; return cache)
if f.grad === nothing
gradcache = FD.GradientCache(cache.u0, cache.u0, adtype.fdtype)
grad = (res, θ, args...) -> FD.finite_difference_gradient!(res, x -> _f(x, args...),
θ, gradcache)
else
grad = (G, θ, args...) -> f.grad(G, θ, cache.p, args...)
end
if f.hess === nothing
hesscache = FD.HessianCache(cache.u0, adtype.fdhtype)
hess = (res, θ, args...) -> FD.finite_difference_hessian!(res, x -> _f(x, args...),
θ,
updatecache(hesscache, θ))
else
hess = (H, θ, args...) -> f.hess(H, θ, cache.p, args...)
end
if f.hv === nothing
hv = function (H, θ, v, args...)
T = eltype(θ)
ϵ = sqrt(eps(real(T))) * max(one(real(T)), abs(norm(θ)))
@. θ += ϵ * v
cache2 = similar(θ)
grad(cache2, θ, args...)
@. θ -= 2ϵ * v
cache3 = similar(θ)
grad(cache3, θ, args...)
@. θ += ϵ * v
@. H = (cache2 - cache3) / (2ϵ)
end
else
hv = f.hv
end
if f.cons === nothing
cons = nothing
else
cons = (res, θ) -> f.cons(res, θ, cache.p)
end
cons_jac_colorvec = f.cons_jac_colorvec === nothing ? (1:length(cache.u0)) :
f.cons_jac_colorvec
if cons !== nothing && f.cons_j === nothing
cons_j = function (J, θ)
y0 = zeros(num_cons)
jaccache = FD.JacobianCache(copy(cache.u0), copy(y0), copy(y0),
adtype.fdjtype;
colorvec = cons_jac_colorvec,
sparsity = f.cons_jac_prototype)
FD.finite_difference_jacobian!(J, cons, θ, jaccache)
end
else
cons_j = (J, θ) -> f.cons_j(J, θ, cache.p)
end
if cons !== nothing && f.cons_h === nothing
hess_cons_cache = [FD.HessianCache(copy(cache.u0), adtype.fdhtype)
for i in 1:num_cons]
cons_h = function (res, θ)
for i in 1:num_cons#note: colorvecs not yet supported by FiniteDiff for Hessians
FD.finite_difference_hessian!(res[i],
(x) -> (_res = zeros(eltype(θ), num_cons);
cons(_res,
x);
_res[i]),
θ, updatecache(hess_cons_cache[i], θ))
end
end
else
cons_h = (res, θ) -> f.cons_h(res, θ, cache.p)
end
if f.lag_h === nothing
lag_hess_cache = FD.HessianCache(copy(cache.u0), adtype.fdhtype)
c = zeros(num_cons)
h = zeros(length(cache.u0), length(cache.u0))
lag_h = let c = c, h = h
lag = function (θ, σ, μ)
f.cons(c, θ, cache.p)
l = μ'c
if !iszero(σ)
l += σ * f.f(θ, cache.p)
end
l
end
function (res, θ, σ, μ)
FD.finite_difference_hessian!(h,
(x) -> lag(x, σ, μ),
θ,
updatecache(lag_hess_cache, θ))
k = 1
for i in 1:length(cache.u0), j in i:length(cache.u0)
res[k] = h[i, j]
k += 1
end
end
end
else
lag_h = (res, θ, σ, μ) -> f.lag_h(res, θ, σ, μ, cache.p)
end
return OptimizationFunction{true}(f, adtype; grad = grad, hess = hess, hv = hv,
cons = cons, cons_j = cons_j, cons_h = cons_h,
cons_jac_colorvec = cons_jac_colorvec,
hess_prototype = f.hess_prototype,
cons_jac_prototype = f.cons_jac_prototype,
cons_hess_prototype = f.cons_hess_prototype,
lag_h, f.lag_hess_prototype)
end
end