@@ -26,6 +26,7 @@ function SciMLBase.requireshessian(opt::Union{
2626 true
2727end
2828SciMLBase. requiresgradient (opt:: Optim.Fminbox ) = true
29+ SciMLBase. allowsfg (opt:: Union{Optim.AbstractOptimizer, Optim.ConstrainedOptimizer, Optim.Fminbox, Optim.SAMIN} ) = true
2930
3031function __map_optimizer_args (cache:: OptimizationCache ,
3132 opt:: Union {Optim. AbstractOptimizer, Optim. Fminbox,
@@ -142,11 +143,11 @@ function SciMLBase.__solve(cache::OptimizationCache{
142143 θ = metadata[cache. opt isa Optim. NelderMead ? " centroid" : " x" ]
143144 opt_state = Optimization. OptimizationState (iter = trace. iteration,
144145 u = θ,
145- objective = x[ 1 ] ,
146+ objective = trace . value ,
146147 grad = get (metadata, " g(x)" , nothing ),
147148 hess = get (metadata, " h(x)" , nothing ),
148149 original = trace)
149- cb_call = cache. callback (opt_state, x ... )
150+ cb_call = cache. callback (opt_state, trace . value )
150151 if ! (cb_call isa Bool)
151152 error (" The callback should return a boolean `halt` for whether to stop the optimization process." )
152153 end
@@ -261,11 +262,11 @@ function SciMLBase.__solve(cache::OptimizationCache{
261262 metadata[" x" ]
262263 opt_state = Optimization. OptimizationState (iter = trace. iteration,
263264 u = θ,
264- objective = x[ 1 ] ,
265+ objective = trace . value ,
265266 grad = get (metadata, " g(x)" , nothing ),
266267 hess = get (metadata, " h(x)" , nothing ),
267268 original = trace)
268- cb_call = cache. callback (opt_state, x ... )
269+ cb_call = cache. callback (opt_state, trace . value )
269270 if ! (cb_call isa Bool)
270271 error (" The callback should return a boolean `halt` for whether to stop the optimization process." )
271272 end
@@ -277,15 +278,21 @@ function SciMLBase.__solve(cache::OptimizationCache{
277278 __x = first (x)
278279 return cache. sense === Optimization. MaxSense ? - __x : __x
279280 end
280- fg! = function (G, θ)
281- if G != = nothing
282- cache. f. grad (G, θ)
283- if cache. sense === Optimization. MaxSense
284- G .*= - one (eltype (G))
281+
282+ if cache. f. fg === nothing
283+ fg! = function (G, θ)
284+ if G != = nothing
285+ cache. f. grad (G, θ)
286+ if cache. sense === Optimization. MaxSense
287+ G .*= - one (eltype (G))
288+ end
285289 end
290+ return _loss (θ)
286291 end
287- return _loss (θ)
292+ else
293+ fg! = cache. f. fg
288294 end
295+
289296
290297 gg = function (G, θ)
291298 cache. f. grad (G, θ)
@@ -344,9 +351,9 @@ function SciMLBase.__solve(cache::OptimizationCache{
344351 u = metadata[" x" ],
345352 grad = get (metadata, " g(x)" , nothing ),
346353 hess = get (metadata, " h(x)" , nothing ),
347- objective = x[ 1 ] ,
354+ objective = trace . value ,
348355 original = trace)
349- cb_call = cache. callback (opt_state, x ... )
356+ cb_call = cache. callback (opt_state, trace . value )
350357 if ! (cb_call isa Bool)
351358 error (" The callback should return a boolean `halt` for whether to stop the optimization process." )
352359 end
@@ -358,15 +365,21 @@ function SciMLBase.__solve(cache::OptimizationCache{
358365 __x = first (x)
359366 return cache. sense === Optimization. MaxSense ? - __x : __x
360367 end
361- fg! = function (G, θ)
362- if G != = nothing
363- cache. f. grad (G, θ)
364- if cache. sense === Optimization. MaxSense
365- G .*= - one (eltype (G))
368+
369+ if cache. f. fg === nothing
370+ fg! = function (G, θ)
371+ if G != = nothing
372+ cache. f. grad (G, θ)
373+ if cache. sense === Optimization. MaxSense
374+ G .*= - one (eltype (G))
375+ end
366376 end
377+ return _loss (θ)
367378 end
368- return _loss (θ)
379+ else
380+ fg! = cache. f. fg
369381 end
382+
370383 gg = function (G, θ)
371384 cache. f. grad (G, θ)
372385 if cache. sense === Optimization. MaxSense
@@ -434,7 +447,7 @@ PrecompileTools.@compile_workload begin
434447 function obj_f (x, p)
435448 A = p[1 ]
436449 b = p[2 ]
437- return sum ((A * x - b) .^ 2 )
450+ return sum ((A * x . - b) .^ 2 )
438451 end
439452
440453 function solve_nonnegative_least_squares (A, b, solver)
0 commit comments