@@ -13,13 +13,27 @@ SciMLBase.allowscallback(::SpeedMappingOpt) = false
1313SciMLBase. has_init (opt:: SpeedMappingOpt ) = true
1414SciMLBase. requiresgradient (opt:: SpeedMappingOpt ) = true
1515
16+ function _retcode (opt_res)
17+ if hasproperty (opt_res, :converged )
18+ return opt_res. converged ? SciMLBase. ReturnCode. Success :
19+ SciMLBase. ReturnCode. Failure
20+ end
21+
22+ status = hasproperty (opt_res, :status ) ? opt_res. status : nothing
23+ status === :first_order && return SciMLBase. ReturnCode. Success
24+ status === :max_time && return SciMLBase. ReturnCode. MaxTime
25+ status in (:max_iter , :max_eval ) && return SciMLBase. ReturnCode. MaxIters
26+ return SciMLBase. ReturnCode. Failure
27+ end
28+
1629function __map_optimizer_args (
17- cache:: OptimizationBase.OptimizationCache , opt:: SpeedMappingOpt ;
30+ cache:: OptimizationBase.OptimizationCache ,
31+ opt:: SpeedMappingOpt ;
1832 callback = nothing ,
1933 maxiters:: Union{Number, Nothing} = nothing ,
2034 maxtime:: Union{Number, Nothing} = nothing ,
2135 abstol:: Union{Number, Nothing} = nothing ,
22- reltol:: Union{Number, Nothing} = nothing
36+ reltol:: Union{Number, Nothing} = nothing ,
2337 )
2438
2539 # add optimiser options from kwargs
@@ -37,14 +51,16 @@ function __map_optimizer_args(
3751 if ! isnothing (abstol)
3852 @SciMLMessage (
3953 lazy " common abstol is currently not used by $(opt)" ,
40- cache. verbose, :unsupported_kwargs
54+ cache. verbose,
55+ :unsupported_kwargs
4156 )
4257 end
4358
4459 if ! isnothing (reltol)
4560 @SciMLMessage (
4661 lazy " common reltol is currently not used by $(opt)" ,
47- cache. verbose, :unsupported_kwargs
62+ cache. verbose,
63+ :unsupported_kwargs
4864 )
4965 end
5066
@@ -66,26 +82,35 @@ function SciMLBase.__solve(cache::OptimizationCache{O}) where {O <: SpeedMapping
6682 maxiters = OptimizationBase. _check_and_convert_maxiters (cache. solver_args. maxiters)
6783 maxtime = OptimizationBase. _check_and_convert_maxtime (cache. solver_args. maxtime)
6884 opt_args = __map_optimizer_args (
69- cache, cache. opt, maxiters = maxiters,
85+ cache,
86+ cache. opt,
87+ maxiters = maxiters,
7088 maxtime = maxtime,
7189 abstol = cache. solver_args. abstol,
72- reltol = cache. solver_args. reltol; cache. solver_args...
90+ reltol = cache. solver_args. reltol;
91+ cache. solver_args... ,
7392 )
7493
7594 t0 = time ()
7695 opt_res = SpeedMapping. speedmapping (
77- cache. u0; f = _loss, (g!) = cache. f. grad,
96+ cache. u0;
97+ f = _loss,
98+ (g!) = cache. f. grad,
7899 lower = cache. lb,
79- upper = cache. ub, opt_args...
100+ upper = cache. ub,
101+ opt_args... ,
80102 )
81103 t1 = time ()
82- opt_ret = opt_res. converged ? SciMLBase. ReturnCode. Success :
83- SciMLBase. ReturnCode. Failure
104+ opt_ret = _retcode (opt_res)
84105 stats = OptimizationBase. OptimizationStats (; time = t1 - t0)
85106 return SciMLBase. build_solution (
86- cache, cache. opt,
87- opt_res. minimizer, _loss (opt_res. minimizer);
88- original = opt_res, retcode = opt_ret, stats = stats
107+ cache,
108+ cache. opt,
109+ opt_res. minimizer,
110+ _loss (opt_res. minimizer);
111+ original = opt_res,
112+ retcode = opt_ret,
113+ stats = stats,
89114 )
90115end
91116
0 commit comments