11update_g! (d, state, method) = nothing
22function update_g! (d, state, method:: FirstOrderOptimizer )
33 # Update the function value and gradient
4- value_gradient! (d, state. x)
5- project_tangent! (method. manifold, gradient (d), state. x)
4+ f_x, g_x = value_gradient! (d, state. x)
5+ project_tangent! (method. manifold, g_x, state. x)
6+ state. f_x = f_x
7+ copyto! (state. g_x, g_x)
8+ return nothing
69end
710function update_g! (d, state, method:: Newton )
811 # Update the function value and gradient
9- value_gradient! (d, state. x)
12+ f_x, g_x = value_gradient! (d, state. x)
13+ state. f_x = f_x
14+ copyto! (state. g_x, g_x)
15+ return nothing
1016end
17+
1118update_fg! (d, state, method) = nothing
12- update_fg! (d, state, method:: ZerothOrderOptimizer ) = value! (d, state. x)
19+ function update_fg! (d, state, method:: ZerothOrderOptimizer )
20+ f_x = value! (d, state. x)
21+ state. f_x = f_x
22+ return nothing
23+ end
1324function update_fg! (d, state, method:: FirstOrderOptimizer )
14- value_gradient! (d, state. x)
15- project_tangent! (method. manifold, gradient (d), state. x)
25+ f_x, g_x = value_gradient! (d, state. x)
26+ project_tangent! (method. manifold, g_x, state. x)
27+ state. f_x = f_x
28+ copyto! (state. g_x, g_x)
29+ return nothing
1630end
1731function update_fg! (d, state, method:: Newton )
18- value_gradient! (d, state. x)
32+ f_x, g_x = value_gradient! (d, state. x)
33+ state. f_x = f_x
34+ copyto! (state. g_x, g_x)
35+ return nothing
1936end
2037
2138# Update the Hessian
@@ -24,14 +41,14 @@ update_h!(d, state, method::SecondOrderOptimizer) = hessian!(d, state.x)
2441
2542after_while! (d, state, method, options) = nothing
2643
27- function initial_convergence (d, state, method:: AbstractOptimizer , initial_x, options)
28- gradient! (d, initial_x)
29- stopped = ! isfinite (value (d)) || any (! isfinite, gradient (d))
30- g_residual (d, state) <= options. g_abstol, stopped
44+ function initial_convergence (state:: AbstractOptimizerState , options:: Options )
45+ stopped = ! isfinite (state. f_x) || any (! isfinite, state. g_x)
46+ return g_residual (state) <= options. g_abstol, stopped
3147end
32- function initial_convergence (d, state, method :: ZerothOrderOptimizer , initial_x, options )
48+ function initial_convergence (:: ZerothOrderState , :: Options )
3349 false , false
3450end
51+
3552function optimize (
3653 d:: D ,
3754 initial_x:: Tx ,
@@ -51,7 +68,7 @@ function optimize(
5168 f_limit_reached, g_limit_reached, h_limit_reached = false , false , false
5269 x_converged, f_converged, f_increased, counter_f_tol = false , false , false , 0
5370
54- g_converged, stopped = initial_convergence (d, state, method, initial_x , options)
71+ g_converged, stopped = initial_convergence (state, options)
5572 converged = g_converged || stopped
5673 # prepare iteration counter (used to make "initial state" trace entry)
5774 iteration = 0
@@ -113,11 +130,11 @@ function optimize(
113130 end
114131 end
115132
116- if g_calls (d) > 0 && ! all (isfinite, gradient (d) )
133+ if hasproperty (state, :g_x ) && ! all (isfinite, state . g_x )
117134 options. show_warnings && @warn " Terminated early due to NaN in gradient."
118135 break
119136 end
120- if h_calls (d) > 0 && ! (d isa TwiceDifferentiableHV ) && ! all (isfinite, hessian (d) )
137+ if hasproperty (state, :H_x ) && ! all (isfinite, state . H_x )
121138 options. show_warnings && @warn " Terminated early due to NaN in Hessian."
122139 break
123140 end
@@ -141,7 +158,7 @@ function optimize(
141158 )
142159
143160 termination_code =
144- _termination_code (d, g_residual (d, state), state, stopped_by, options)
161+ _termination_code (d, g_residual (state), state, stopped_by, options)
145162
146163 return MultivariateOptimizationResults{
147164 typeof (method),
@@ -162,10 +179,10 @@ function optimize(
162179 x_relchange (state),
163180 Tf (options. f_abstol),
164181 Tf (options. f_reltol),
165- f_abschange (d, state),
166- f_relchange (d, state),
182+ f_abschange (state),
183+ f_relchange (state),
167184 Tf (options. g_abstol),
168- g_residual (d, state),
185+ g_residual (state),
169186 tr,
170187 f_calls (d),
171188 g_calls (d),
@@ -186,13 +203,13 @@ function _termination_code(d, gres, state, stopped_by, options)
186203 elseif (iszero (options. x_abstol) && x_abschange (state) <= options. x_abstol) ||
187204 (iszero (options. x_reltol) && x_relchange (state) <= options. x_reltol)
188205 TerminationCode. NoXChange
189- elseif (iszero (options. f_abstol) && f_abschange (d, state) <= options. f_abstol) ||
190- (iszero (options. f_reltol) && f_relchange (d, state) <= options. f_reltol)
206+ elseif (iszero (options. f_abstol) && f_abschange (state) <= options. f_abstol) ||
207+ (iszero (options. f_reltol) && f_relchange (state) <= options. f_reltol)
191208 TerminationCode. NoObjectiveChange
192209 elseif x_abschange (state) <= options. x_abstol || x_relchange (state) <= options. x_reltol
193210 TerminationCode. SmallXChange
194- elseif f_abschange (d, state) <= options. f_abstol ||
195- f_relchange (d, state) <= options. f_reltol
211+ elseif f_abschange (state) <= options. f_abstol ||
212+ f_relchange (state) <= options. f_reltol
196213 TerminationCode. SmallObjectiveChange
197214 elseif stopped_by. ls_failed
198215 TerminationCode. FailedLinesearch
@@ -210,11 +227,11 @@ function _termination_code(d, gres, state, stopped_by, options)
210227 TerminationCode. HessianCalls
211228 elseif stopped_by. f_increased
212229 TerminationCode. ObjectiveIncreased
213- elseif f_calls (d) > 0 && ! isfinite (value (d) )
214- TerminationCode. GradientNotFinite
215- elseif g_calls (d) > 0 && ! all (isfinite, gradient (d) )
230+ elseif ! isfinite (state . f_x )
231+ TerminationCode. ObjectiveNotFinite
232+ elseif hasproperty (state, :g_x ) && ! all (isfinite, state . g_x )
216233 TerminationCode. GradientNotFinite
217- elseif h_calls (d) > 0 && ! (d isa TwiceDifferentiableHV ) && ! all (isfinite, hessian (d) )
234+ elseif hasproperty (state, :H_x ) && ! all (isfinite, state . H_x )
218235 TerminationCode. HessianNotFinite
219236 else
220237 TerminationCode. NotImplemented
0 commit comments