@@ -20,11 +20,9 @@ function Optimization.instantiate_function(f, x, adtype::AutoReverseDiff,
20
20
end
21
21
22
22
if f. hess === nothing
23
+
23
24
hess = function (res, θ, args... )
24
-
25
- res .= SparseDiffTools. forwarddiff_color_jacobian (θ, colorvec = hess_colors, sparsity = hess_sparsity) do θ
26
- ReverseDiff. gradient (x -> _f (x, args... ), θ)
27
- end
25
+ ReverseDiff. hessian! (res, x -> _f (x, args... ), θ)
28
26
end
29
27
else
30
28
hess = (H, θ, args... ) -> f. hess (H, θ, p, args... )
@@ -61,9 +59,7 @@ function Optimization.instantiate_function(f, x, adtype::AutoReverseDiff,
61
59
62
60
cons_h = function (res, θ)
63
61
for i in 1 : num_cons
64
- res[i] .= SparseDiffTools. forwarddiff_color_jacobian (θ, ) do θ
65
- ReverseDiff. gradient (fncs[i], θ)
66
- end
62
+ ReverseDiff. gradient (res[i], fncs[i], θ)
67
63
end
68
64
end
69
65
else
@@ -83,82 +79,82 @@ function Optimization.instantiate_function(f, x, adtype::AutoReverseDiff,
83
79
lag_h, f. lag_hess_prototype)
84
80
end
85
81
86
- # function Optimization.instantiate_function(f, cache::Optimization.ReInitCache,
87
- # adtype::AutoReverseDiff, num_cons = 0)
88
- # _f = (θ, args...) -> first(f.f(θ, cache.p, args...))
89
-
90
- # if f.grad === nothing
91
- # grad = (res, θ, args...) -> ReverseDiff.gradient!(res, x -> _f(x, args...), θ)
92
- # else
93
- # grad = (G, θ, args...) -> f.grad(G, θ, cache.p, args...)
94
- # end
95
-
96
- # if f.hess === nothing
97
- # hess_sparsity = Symbolics.hessian_sparsity(_f, cache.u0)
98
- # hess_colors = SparseDiffTools.matrix_colors(tril(hess_sparsity))
99
- # hess = function (res, θ, args...)
100
- # res .= SparseDiffTools.forwarddiff_color_jacobian(θ, colorvec = hess_colors, sparsity = hess_sparsity) do θ
101
- # ReverseDiff.gradient(x -> _f(x, args...), θ)
102
- # end
103
- # end
104
- # else
105
- # hess = (H, θ, args...) -> f.hess(H, θ, cache.p, args...)
106
- # end
107
-
108
- # if f.hv === nothing
109
- # hv = function (H, θ, v, args...)
110
- # _θ = ForwardDiff.Dual.(θ, v)
111
- # res = similar(_θ)
112
- # grad(res, _θ, args...)
113
- # H .= getindex.(ForwardDiff.partials.(res), 1)
114
- # end
115
- # else
116
- # hv = f.hv
117
- # end
118
-
119
- # if f.cons === nothing
120
- # cons = nothing
121
- # else
122
- # cons = (res, θ) -> f.cons(res, θ, cache.p)
123
- # cons_oop = (x) -> (_res = zeros(eltype(x), num_cons); cons(_res, x); _res)
124
- # end
125
-
126
- # if cons !== nothing && f.cons_j === nothing
127
- # cjconfig = ReverseDiff.JacobianConfig(cache.u0)
128
- # cons_j = function (J, θ)
129
- # ReverseDiff.jacobian!(J, cons_oop, θ, cjconfig)
130
- # end
131
- # else
132
- # cons_j = (J, θ) -> f.cons_j(J, θ, cache.p)
133
- # end
134
-
135
- # if cons !== nothing && f.cons_h === nothing
136
- # fncs = [(x) -> cons_oop(x)[i] for i in 1:num_cons]
137
- # conshess_sparsity = Symbolics.hessian_sparsity.(fncs, Ref(cache.u0))
138
- # conshess_colors = SparseDiffTools.matrix_colors.(conshess_sparsity)
139
- # cons_h = function (res, θ)
140
- # for i in 1:num_cons
141
- # res[i] .= SparseDiffTools.forwarddiff_color_jacobian(θ, colorvec = conshess_colors[i], sparsity = conshess_sparsity[i]) do θ
142
- # ReverseDiff.gradient(fncs[i], θ)
143
- # end
144
- # end
145
- # end
146
- # else
147
- # cons_h = (res, θ) -> f.cons_h(res, θ, cache.p)
148
- # end
149
-
150
- # if f.lag_h === nothing
151
- # lag_h = nothing # Consider implementing this
152
- # else
153
- # lag_h = (res, θ, σ, μ) -> f.lag_h(res, θ, σ, μ, cache.p)
154
- # end
155
-
156
- # return OptimizationFunction{true}(f.f, adtype; grad = grad, hess = hess, hv = hv,
157
- # cons = cons, cons_j = cons_j, cons_h = cons_h,
158
- # hess_prototype = f.hess_prototype,
159
- # cons_jac_prototype = f.cons_jac_prototype,
160
- # cons_hess_prototype = f.cons_hess_prototype,
161
- # lag_h, f.lag_hess_prototype)
162
- # end
82
+ function Optimization. instantiate_function (f, cache:: Optimization.ReInitCache ,
83
+ adtype:: AutoReverseDiff , num_cons = 0 )
84
+ _f = (θ, args... ) -> first (f. f (θ, cache. p, args... ))
85
+
86
+ if f. grad === nothing
87
+ grad = (res, θ, args... ) -> ReverseDiff. gradient! (res, x -> _f (x, args... ), θ)
88
+ else
89
+ grad = (G, θ, args... ) -> f. grad (G, θ, cache. p, args... )
90
+ end
91
+
92
+ if f. hess === nothing
93
+ hess_sparsity = Symbolics. hessian_sparsity (_f, cache. u0)
94
+ hess_colors = SparseDiffTools. matrix_colors (tril (hess_sparsity))
95
+ hess = function (res, θ, args... )
96
+ res .= SparseDiffTools. forwarddiff_color_jacobian (θ, colorvec = hess_colors, sparsity = hess_sparsity) do θ
97
+ ReverseDiff. gradient (x -> _f (x, args... ), θ)
98
+ end
99
+ end
100
+ else
101
+ hess = (H, θ, args... ) -> f. hess (H, θ, cache. p, args... )
102
+ end
103
+
104
+ if f. hv === nothing
105
+ hv = function (H, θ, v, args... )
106
+ _θ = ForwardDiff. Dual .(θ, v)
107
+ res = similar (_θ)
108
+ grad (res, _θ, args... )
109
+ H .= getindex .(ForwardDiff. partials .(res), 1 )
110
+ end
111
+ else
112
+ hv = f. hv
113
+ end
114
+
115
+ if f. cons === nothing
116
+ cons = nothing
117
+ else
118
+ cons = (res, θ) -> f. cons (res, θ, cache. p)
119
+ cons_oop = (x) -> (_res = zeros (eltype (x), num_cons); cons (_res, x); _res)
120
+ end
121
+
122
+ if cons != = nothing && f. cons_j === nothing
123
+ cjconfig = ReverseDiff. JacobianConfig (cache. u0)
124
+ cons_j = function (J, θ)
125
+ ReverseDiff. jacobian! (J, cons_oop, θ, cjconfig)
126
+ end
127
+ else
128
+ cons_j = (J, θ) -> f. cons_j (J, θ, cache. p)
129
+ end
130
+
131
+ if cons != = nothing && f. cons_h === nothing
132
+ fncs = [(x) -> cons_oop (x)[i] for i in 1 : num_cons]
133
+ conshess_sparsity = Symbolics. hessian_sparsity .(fncs, Ref (cache. u0))
134
+ conshess_colors = SparseDiffTools. matrix_colors .(conshess_sparsity)
135
+ cons_h = function (res, θ)
136
+ for i in 1 : num_cons
137
+ res[i] .= SparseDiffTools. forwarddiff_color_jacobian (θ, colorvec = conshess_colors[i], sparsity = conshess_sparsity[i]) do θ
138
+ ReverseDiff. gradient (fncs[i], θ)
139
+ end
140
+ end
141
+ end
142
+ else
143
+ cons_h = (res, θ) -> f. cons_h (res, θ, cache. p)
144
+ end
145
+
146
+ if f. lag_h === nothing
147
+ lag_h = nothing # Consider implementing this
148
+ else
149
+ lag_h = (res, θ, σ, μ) -> f. lag_h (res, θ, σ, μ, cache. p)
150
+ end
151
+
152
+ return OptimizationFunction {true} (f. f, adtype; grad = grad, hess = hess, hv = hv,
153
+ cons = cons, cons_j = cons_j, cons_h = cons_h,
154
+ hess_prototype = f. hess_prototype,
155
+ cons_jac_prototype = f. cons_jac_prototype,
156
+ cons_hess_prototype = f. cons_hess_prototype,
157
+ lag_h, f. lag_hess_prototype)
158
+ end
163
159
164
160
end
0 commit comments