@@ -18,7 +18,8 @@ SciMLBase.requiresbounds(opt::Optim.SAMIN) = true
1818end
1919@static if isdefined (OptimizationBase, :supports_opt_cache_interface )
2020 OptimizationBase. supports_opt_cache_interface (opt:: Optim.AbstractOptimizer ) = true
21- OptimizationBase. supports_opt_cache_interface (opt:: Union{Optim.Fminbox, Optim.SAMIN} ) = true
21+ OptimizationBase. supports_opt_cache_interface (opt:: Union {
22+ Optim. Fminbox, Optim. SAMIN}) = true
2223 OptimizationBase. supports_opt_cache_interface (opt:: Optim.ConstrainedOptimizer ) = true
2324end
2425function SciMLBase. requiresgradient (opt:: Optim.AbstractOptimizer )
@@ -149,14 +150,17 @@ function SciMLBase.__solve(cache::OptimizationBase.OptimizationCache{
149150 trace_state = decompose_trace (trace)
150151 metadata = trace_state. metadata
151152 θ = metadata[cache. opt isa Optim. NelderMead ? " centroid" : " x" ]
153+ # Extract scalar value from potentially Dual-valued trace (issue #1073)
154+ # Using SciMLBase.value to handle ForwardDiff.Dual numbers from Fminbox
155+ loss_val = SciMLBase. value (trace_state. value)
152156 opt_state = OptimizationBase. OptimizationState (iter = trace_state. iteration,
153157 u = θ,
154158 p = cache. p,
155- objective = trace_state . value ,
159+ objective = loss_val ,
156160 grad = get (metadata, " g(x)" , nothing ),
157161 hess = get (metadata, " h(x)" , nothing ),
158162 original = trace)
159- cb_call = cache. callback (opt_state, trace_state . value )
163+ cb_call = cache. callback (opt_state, loss_val )
160164 if ! (cb_call isa Bool)
161165 error (" The callback should return a boolean `halt` for whether to stop the optimization process." )
162166 end
@@ -270,14 +274,17 @@ function SciMLBase.__solve(cache::OptimizationBase.OptimizationCache{
270274 θ = ! (cache. opt isa Optim. SAMIN) && cache. opt. method == Optim. NelderMead () ?
271275 metadata[" centroid" ] :
272276 metadata[" x" ]
277+ # Extract scalar value from potentially Dual-valued trace (issue #1073)
278+ # Using SciMLBase.value to handle ForwardDiff.Dual numbers from Fminbox
279+ loss_val = SciMLBase. value (trace_state. value)
273280 opt_state = OptimizationBase. OptimizationState (iter = trace_state. iteration,
274281 u = θ,
275282 p = cache. p,
276- objective = trace_state . value ,
283+ objective = loss_val ,
277284 grad = get (metadata, " g(x)" , nothing ),
278285 hess = get (metadata, " h(x)" , nothing ),
279286 original = trace)
280- cb_call = cache. callback (opt_state, trace_state . value )
287+ cb_call = cache. callback (opt_state, loss_val )
281288 if ! (cb_call isa Bool)
282289 error (" The callback should return a boolean `halt` for whether to stop the optimization process." )
283290 end
@@ -357,14 +364,17 @@ function SciMLBase.__solve(cache::OptimizationBase.OptimizationCache{
357364
358365 function _cb (trace)
359366 metadata = decompose_trace (trace). metadata
367+ # Extract scalar value from potentially Dual-valued trace (issue #1073)
368+ # Using SciMLBase.value to handle ForwardDiff.Dual numbers from Fminbox
369+ loss_val = SciMLBase. value (trace. value)
360370 opt_state = OptimizationBase. OptimizationState (iter = trace. iteration,
361371 u = metadata[" x" ],
362372 p = cache. p,
363373 grad = get (metadata, " g(x)" , nothing ),
364374 hess = get (metadata, " h(x)" , nothing ),
365- objective = trace . value ,
375+ objective = loss_val ,
366376 original = trace)
367- cb_call = cache. callback (opt_state, trace . value )
377+ cb_call = cache. callback (opt_state, loss_val )
368378 if ! (cb_call isa Bool)
369379 error (" The callback should return a boolean `halt` for whether to stop the optimization process." )
370380 end
0 commit comments