forked from SciML/Optimization.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolve.jl
656 lines (522 loc) · 23.9 KB
/
solve.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
struct NullData end
const DEFAULT_DATA = Iterators.cycle((NullData(),))
Base.iterate(::NullData, i=1) = nothing
Base.length(::NullData) = 0
get_maxiters(data) = Iterators.IteratorSize(typeof(DEFAULT_DATA)) isa Iterators.IsInfinite ||
Iterators.IteratorSize(typeof(DEFAULT_DATA)) isa Iterators.SizeUnknown ?
typemax(Int) : length(data)
#=
function update!(x::AbstractArray, x̄::AbstractArray{<:ForwardDiff.Dual})
x .-= x̄
end
function update!(x::AbstractArray, x̄)
x .-= getindex.(ForwardDiff.partials.(x̄),1)
end
function update!(opt, x, x̄)
x .-= Flux.Optimise.apply!(opt, x, x̄)
end
function update!(opt, x, x̄::AbstractArray{<:ForwardDiff.Dual})
x .-= Flux.Optimise.apply!(opt, x, getindex.(ForwardDiff.partials.(x̄),1))
end
function update!(opt, xs::Flux.Zygote.Params, gs)
update!(opt, xs[1], gs)
end
=#
maybe_with_logger(f, logger) = logger === nothing ? f() : Logging.with_logger(f, logger)
function default_logger(logger)
Logging.min_enabled_level(logger) ≤ ProgressLogging.ProgressLevel && return nothing
if Sys.iswindows() || (isdefined(Main, :IJulia) && Main.IJulia.inited)
progresslogger = ConsoleProgressMonitor.ProgressLogger()
else
progresslogger = TerminalLoggers.TerminalLogger()
end
logger1 = LoggingExtras.EarlyFilteredLogger(progresslogger) do log
log.level == ProgressLogging.ProgressLevel
end
logger2 = LoggingExtras.EarlyFilteredLogger(logger) do log
log.level != ProgressLogging.ProgressLevel
end
LoggingExtras.TeeLogger(logger1, logger2)
end
macro withprogress(progress, exprs...)
quote
if $progress
$maybe_with_logger($default_logger($Logging.current_logger())) do
$ProgressLogging.@withprogress $(exprs...)
end
else
$(exprs[end])
end
end |> esc
end
function __solve(prob::OptimizationProblem, opt, data = DEFAULT_DATA;
maxiters::Number, cb = (args...) -> (false),
progress = false, save_best = true, kwargs...)
if maxiters <= 0.0
error("The number of maxiters has to be a non-negative and non-zero number.")
else
maxiters = convert(Int, maxiters)
end
# Flux is silly and doesn't have an abstract type on its optimizers, so assume
# this is a Flux optimizer
θ = copy(prob.u0)
ps = Flux.params(θ)
if data != DEFAULT_DATA
maxiters = length(data)
else
data = take(data, maxiters)
end
t0 = time()
local x, min_err, _loss
min_err = typemax(eltype(prob.u0)) #dummy variables
min_opt = 1
f = instantiate_function(prob.f,prob.u0,prob.f.adtype,prob.p)
@withprogress progress name="Training" begin
for (i,d) in enumerate(data)
gs = Flux.Zygote.gradient(ps) do
x = prob.f(θ,prob.p, d...)
first(x)
end
x = f.f(θ, prob.p, d...)
cb_call = cb(θ, x...)
if !(typeof(cb_call) <: Bool)
error("The callback should return a boolean `halt` for whether to stop the optimization process. Please see the sciml_train documentation for information.")
elseif cb_call
break
end
msg = @sprintf("loss: %.3g", x[1])
progress && ProgressLogging.@logprogress msg i/maxiters
Flux.update!(opt, ps, gs)
if save_best
if first(x) < first(min_err) #found a better solution
min_opt = opt
min_err = x
end
if i == maxiters #Last iteration, revert to best.
opt = min_opt
cb(θ,min_err...)
end
end
end
end
_time = time()
SciMLBase.build_solution(prob, opt, θ, x[1])
# here should be build_solution to create the output message
end
decompose_trace(trace::Optim.OptimizationTrace) = last(trace)
decompose_trace(trace) = trace
function __solve(prob::OptimizationProblem, opt::Optim.AbstractOptimizer,
data = DEFAULT_DATA;
maxiters = nothing,
cb = (args...) -> (false),
progress = false,
kwargs...)
local x, cur, state
if data != DEFAULT_DATA
maxiters = length(data)
end
cur, state = iterate(data)
function _cb(trace)
cb_call = opt == NelderMead() ? cb(decompose_trace(trace).metadata["centroid"],x...) : cb(decompose_trace(trace).metadata["x"],x...)
if !(typeof(cb_call) <: Bool)
error("The callback should return a boolean `halt` for whether to stop the optimization process.")
end
cur, state = iterate(data, state)
cb_call
end
if !(isnothing(maxiters)) && maxiters <= 0.0
error("The number of maxiters has to be a non-negative and non-zero number.")
elseif !(isnothing(maxiters))
maxiters = convert(Int, maxiters)
end
f = instantiate_function(prob.f,prob.u0,prob.f.adtype,prob.p)
!(opt isa Optim.ZerothOrderOptimizer) && f.grad === nothing && error("Use OptimizationFunction to pass the derivatives or automatically generate them with one of the autodiff backends")
_loss = function(θ)
x = f.f(θ, prob.p, cur...)
return first(x)
end
fg! = function (G,θ)
if G !== nothing
f.grad(G, θ, cur...)
end
return _loss(θ)
end
if opt isa Optim.KrylovTrustRegion
optim_f = Optim.TwiceDifferentiableHV(_loss, fg!, (H,θ,v) -> f.hv(H,θ,v,cur...), prob.u0)
else
optim_f = TwiceDifferentiable(_loss, (G, θ) -> f.grad(G, θ, cur...), fg!, (H,θ) -> f.hess(H,θ,cur...), prob.u0)
end
original = Optim.optimize(optim_f, prob.u0, opt,
!(isnothing(maxiters)) ?
Optim.Options(;extended_trace = true,
callback = _cb,
iterations = maxiters,
kwargs...) :
Optim.Options(;extended_trace = true,
callback = _cb, kwargs...))
SciMLBase.build_solution(prob, opt, original.minimizer,
original.minimum; original=original)
end
function __solve(prob::OptimizationProblem, opt::Union{Optim.Fminbox,Optim.SAMIN},
data = DEFAULT_DATA;
maxiters = nothing,
cb = (args...) -> (false),
progress = false,
kwargs...)
local x, cur, state
if data != DEFAULT_DATA
maxiters = length(data)
end
cur, state = iterate(data)
function _cb(trace)
cb_call = !(opt isa Optim.SAMIN) && opt.method == NelderMead() ? cb(decompose_trace(trace).metadata["centroid"],x...) : cb(decompose_trace(trace).metadata["x"],x...)
if !(typeof(cb_call) <: Bool)
error("The callback should return a boolean `halt` for whether to stop the optimization process.")
end
cur, state = iterate(data, state)
cb_call
end
if !(isnothing(maxiters)) && maxiters <= 0.0
error("The number of maxiters has to be a non-negative and non-zero number.")
elseif !(isnothing(maxiters))
maxiters = convert(Int, maxiters)
end
f = instantiate_function(prob.f,prob.u0,prob.f.adtype,prob.p)
!(opt isa Optim.ZerothOrderOptimizer) && f.grad === nothing && error("Use OptimizationFunction to pass the derivatives or automatically generate them with one of the autodiff backends")
_loss = function(θ)
x = f.f(θ, prob.p, cur...)
return first(x)
end
fg! = function (G,θ)
if G !== nothing
f.grad(G, θ, cur...)
end
return _loss(θ)
end
optim_f = OnceDifferentiable(_loss, f.grad, fg!, prob.u0)
original = Optim.optimize(optim_f, prob.lb, prob.ub, prob.u0, opt,
!(isnothing(maxiters)) ? Optim.Options(;
extended_trace = true, callback = _cb,
iterations = maxiters, kwargs...) :
Optim.Options(;extended_trace = true,
callback = _cb, kwargs...))
SciMLBase.build_solution(prob, opt, original.minimizer,
original.minimum; original=original)
end
function __solve(prob::OptimizationProblem, opt::Optim.ConstrainedOptimizer,
data = DEFAULT_DATA;
maxiters = nothing,
cb = (args...) -> (false),
progress = false,
kwargs...)
local x, cur, state
if data != DEFAULT_DATA
maxiters = length(data)
end
cur, state = iterate(data)
function _cb(trace)
cb_call = cb(decompose_trace(trace).metadata["x"],x...)
if !(typeof(cb_call) <: Bool)
error("The callback should return a boolean `halt` for whether to stop the optimization process.")
end
cur, state = iterate(data, state)
cb_call
end
if !(isnothing(maxiters)) && maxiters <= 0.0
error("The number of maxiters has to be a non-negative and non-zero number.")
elseif !(isnothing(maxiters))
maxiters = convert(Int, maxiters)
end
f = instantiate_function(prob.f,prob.u0,prob.f.adtype,prob.p,prob.ucons === nothing ? 0 : length(prob.ucons))
f.cons_j ===nothing && error("Use OptimizationFunction to pass the derivatives or automatically generate them with one of the autodiff backends")
_loss = function(θ)
x = f.f(θ, prob.p, cur...)
return x[1]
end
fg! = function (G,θ)
if G !== nothing
f.grad(G, θ, cur...)
end
return _loss(θ)
end
optim_f = TwiceDifferentiable(_loss, (G, θ) -> f.grad(G, θ, cur...), fg!, (H,θ) -> f.hess(H, θ, cur...), prob.u0)
cons! = (res, θ) -> res .= f.cons(θ);
cons_j! = function(J, x)
f.cons_j(J, x)
end
cons_hl! = function (h, θ, λ)
res = [similar(h) for i in 1:length(λ)]
f.cons_h(res, θ)
for i in 1:length(λ)
h .+= λ[i]*res[i]
end
end
lb = prob.lb === nothing ? [] : prob.lb
ub = prob.ub === nothing ? [] : prob.ub
optim_fc = TwiceDifferentiableConstraints(cons!, cons_j!, cons_hl!, lb, ub, prob.lcons, prob.ucons)
original = Optim.optimize(optim_f, optim_fc, prob.u0, opt,
!(isnothing(maxiters)) ? Optim.Options(;
extended_trace = true, callback = _cb,
iterations = maxiters, kwargs...) :
Optim.Options(;extended_trace = true,
callback = _cb, kwargs...))
SciMLBase.build_solution(prob, opt, original.minimizer,
original.minimum; original=original)
end
function __init__()
@require BlackBoxOptim="a134a8b2-14d6-55f6-9291-3336d3ab0209" begin
decompose_trace(opt::BlackBoxOptim.OptRunController) = BlackBoxOptim.best_candidate(opt)
struct BBO
method::Symbol
BBO(method) = new(method)
end
BBO() = BBO(:adaptive_de_rand_1_bin_radiuslimited) # the recommended optimizer as default
function __solve(prob::OptimizationProblem, opt::BBO, data = DEFAULT_DATA;
cb = (args...) -> (false), maxiters = nothing,
progress = false, kwargs...)
local x, cur, state
if data != DEFAULT_DATA
maxiters = length(data)
end
cur, state = iterate(data)
function _cb(trace)
cb_call = cb(decompose_trace(trace),x...)
if !(typeof(cb_call) <: Bool)
error("The callback should return a boolean `halt` for whether to stop the optimization process.")
end
if cb_call == true
BlackBoxOptim.shutdown_optimizer!(trace) #doesn't work
end
cur, state = iterate(data, state)
cb_call
end
if !(isnothing(maxiters)) && maxiters <= 0.0
error("The number of maxiters has to be a non-negative and non-zero number.")
elseif !(isnothing(maxiters))
maxiters = convert(Int, maxiters)
end
_loss = function(θ)
x = prob.f(θ, prob.p, cur...)
return first(x)
end
bboptre = !(isnothing(maxiters)) ? BlackBoxOptim.bboptimize(_loss;Method = opt.method, SearchRange = [(prob.lb[i], prob.ub[i]) for i in 1:length(prob.lb)], MaxSteps = maxiters, CallbackFunction = _cb, CallbackInterval = 0.0, kwargs...) : BlackBoxOptim.bboptimize(_loss;Method = opt.method, SearchRange = [(prob.lb[i], prob.ub[i]) for i in 1:length(prob.lb)], CallbackFunction = _cb, CallbackInterval = 0.0, kwargs...)
SciMLBase.build_solution(prob, opt, BlackBoxOptim.best_candidate(bboptre),
BlackBoxOptim.best_fitness(bboptre); original=bboptre)
end
function __solve(prob::EnsembleOptimizationProblem, opt::BBO, data = DEFAULT_DATA;
cb = (args...) -> (false), maxiters = nothing,
progress = false, FitnessScheme=nothing, kwargs...)
local x, cur, state
if data != DEFAULT_DATA
maxiters = length(data)
end
cur, state = iterate(data)
function _cb(trace)
cb_call = cb(decompose_trace(trace),x...)
if !(typeof(cb_call) <: Bool)
error("The callback should return a boolean `halt` for whether to stop the optimization process.")
end
if cb_call == true
BlackBoxOptim.shutdown_optimizer!(trace) #doesn't work
end
cur, state = iterate(data, state)
cb_call
end
if !(isnothing(maxiters)) && maxiters <= 0.0
error("The number of maxiters has to be a non-negative and non-zero number.")
elseif !(isnothing(maxiters))
maxiters = convert(Int, maxiters)
end
if !(opt.method == :borg_moea)
error("Multi-Objective optimisation is only possible with BorgMOEA algorithm(:borg_moea).")
end
_loss = function(θ)
x = ntuple(i->first(prob.prob[i].f(θ, prob.prob[i].p, cur...)),length(prob.prob))
return x
end
if any([prob.prob[1].lb != i.lb || prob.prob[1].ub != i.ub for i in prob.prob[2:end]])
error("Lower or upper bounds are not consistent between OptimizationProblem.")
else
multi_bounds = (lb=prob.prob[1].lb,ub=prob.prob[1].ub)
end
if isnothing(FitnessScheme)
println("Warning: No FitnessScheme was defined, using default scheme: FitnessScheme=ParetoFitnessScheme{length(prob.prob)}(is_minimizing=true).")
FitnessScheme=BlackBoxOptim.ParetoFitnessScheme{length(prob.prob)}(is_minimizing=true)
end
bboptre = !(isnothing(maxiters)) ? BlackBoxOptim.bboptimize(_loss;Method = opt.method, SearchRange = [(multi_bounds.lb[i], multi_bounds.ub[i]) for i in 1:length(multi_bounds.lb)], MaxSteps = maxiters, CallbackFunction = _cb, CallbackInterval = 0.0, FitnessScheme=FitnessScheme, kwargs...) : BlackBoxOptim.bboptimize(_loss;Method = opt.method, SearchRange = [(multi_bounds.lb[i], multi_bounds.ub[i]) for i in 1:length(multi_bounds.lb)], CallbackFunction = _cb, CallbackInterval = 0.0, FitnessScheme=FitnessScheme, kwargs...)
SciMLBase.build_solution(prob, opt, BlackBoxOptim.best_candidate(bboptre),
BlackBoxOptim.best_fitness(bboptre); original=bboptre)
end
end
@require NLopt="76087f3c-5699-56af-9a33-bf431cd00edd" begin
function __solve(prob::OptimizationProblem, opt::NLopt.Opt;
maxiters = nothing, nstart = 1,
local_method = nothing,
progress = false, kwargs...)
local x
if !(isnothing(maxiters)) && maxiters <= 0.0
error("The number of maxiters has to be a non-negative and non-zero number.")
elseif !(isnothing(maxiters))
maxiters = convert(Int, maxiters)
end
f = instantiate_function(prob.f,prob.u0,prob.f.adtype,prob.p)
_loss = function(θ)
x = f.f(θ, prob.p)
return x[1]
end
fg! = function (θ,G)
if length(G) > 0
f.grad(G, θ)
end
return _loss(θ)
end
NLopt.min_objective!(opt, fg!)
if prob.ub !== nothing
NLopt.upper_bounds!(opt, prob.ub)
end
if prob.lb !== nothing
NLopt.lower_bounds!(opt, prob.lb)
end
if !(isnothing(maxiters))
NLopt.maxeval!(opt, maxiters)
end
if nstart > 1 && local_method !== nothing
NLopt.local_optimizer!(opt, local_method)
if !(isnothing(maxiters))
NLopt.maxeval!(opt, nstart * maxiters)
end
end
t0 = time()
(minf,minx,ret) = NLopt.optimize(opt, prob.u0)
_time = time()
SciMLBase.build_solution(prob, opt, minx, minf; original=nothing)
end
end
@require MultistartOptimization = "3933049c-43be-478e-a8bb-6e0f7fd53575" begin
function __solve(prob::OptimizationProblem, opt::MultistartOptimization.TikTak;
local_method, local_maxiters = nothing,
progress = false, kwargs...)
local x, _loss
if !(isnothing(local_maxiters)) && local_maxiters <= 0.0
error("The number of local_maxiters has to be a non-negative and non-zero number.")
else !(isnothing(local_maxiters))
local_maxiters = convert(Int, local_maxiters)
end
_loss = function(θ)
x = prob.f(θ, prob.p)
return first(x)
end
t0 = time()
P = MultistartOptimization.MinimizationProblem(_loss, prob.lb, prob.ub)
multistart_method = opt
if !(isnothing(local_maxiters))
local_method = MultistartOptimization.NLoptLocalMethod(local_method, maxeval = local_maxiters)
else
local_method = MultistartOptimization.NLoptLocalMethod(local_method)
end
p = MultistartOptimization.multistart_minimization(multistart_method, local_method, P)
t1 = time()
SciMLBase.build_solution(prob, opt, p.location, p.value; original=p)
end
end
@require QuadDIRECT = "dae52e8d-d666-5120-a592-9e15c33b8d7a" begin
export QuadDirect
struct QuadDirect
end
function __solve(prob::OptimizationProblem, opt::QuadDirect; splits = nothing, maxiters = nothing, kwargs...)
local x, _loss
if !(isnothing(maxiters)) && maxiters <= 0.0
error("The number of maxiters has to be a non-negative and non-zero number.")
elseif !(isnothing(maxiters))
maxiters = convert(Int, maxiters)
end
if splits === nothing
error("You must provide the initial locations at which to evaluate the function in `splits` (a list of 3-vectors with values in strictly increasing order and within the specified bounds).")
end
_loss = function(θ)
x = prob.f(θ, prob.p)
return first(x)
end
t0 = time()
root, x0 = !(isnothing(maxiters)) ? QuadDIRECT.analyze(_loss, splits, prob.lb, prob.ub; maxevals = maxiters, kwargs...) : QuadDIRECT.analyze(_loss, splits, prob.lb, prob.ub; kwargs...)
box = minimum(root)
t1 = time()
SciMLBase.build_solution(prob, opt, QuadDIRECT.position(box, x0), QuadDIRECT.value(box); original=root)
end
end
@require Evolutionary="86b6b26d-c046-49b6-aa0b-5f0f74682bd6" begin
decompose_trace(trace::Evolutionary.OptimizationTrace) = last(trace)
function Evolutionary.trace!(record::Dict{String,Any}, objfun, state, population, method::Evolutionary.AbstractOptimizer, options)
record["x"] = population
end
function __solve(prob::OptimizationProblem, opt::Evolutionary.AbstractOptimizer, data = DEFAULT_DATA;
cb = (args...) -> (false), maxiters = nothing,
progress = false, kwargs...)
local x, cur, state
if data != DEFAULT_DATA
maxiters = length(data)
end
cur, state = iterate(data)
function _cb(trace)
cb_call = cb(decompose_trace(trace).metadata["x"],trace.value...)
if !(typeof(cb_call) <: Bool)
error("The callback should return a boolean `halt` for whether to stop the optimization process.")
end
cur, state = iterate(data, state)
cb_call
end
if !(isnothing(maxiters)) && maxiters <= 0.0
error("The number of maxiters has to be a non-negative and non-zero number.")
elseif !(isnothing(maxiters))
maxiters = convert(Int, maxiters)
end
_loss = function(θ)
x = prob.f(θ, prob.p, cur...)
return first(x)
end
t0 = time()
result = Evolutionary.optimize(_loss, prob.u0, opt, !isnothing(maxiters) ? Evolutionary.Options(;iterations = maxiters, callback = _cb, kwargs...)
: Evolutionary.Options(;callback = _cb, kwargs...))
t1 = time()
SciMLBase.build_solution(prob, opt, Evolutionary.minimizer(result), Evolutionary.minimum(result); original=result)
end
end
@require CMAEvolutionStrategy="8d3b24bd-414e-49e0-94fb-163cc3a3e411" begin
struct CMAEvolutionStrategyOpt end
function __solve(prob::OptimizationProblem, opt::CMAEvolutionStrategyOpt, data = DEFAULT_DATA;
cb = (args...) -> (false), maxiters = nothing,
progress = false, kwargs...)
local x, cur, state
if data != DEFAULT_DATA
maxiters = length(data)
end
cur, state = iterate(data)
function _cb(trace)
cb_call = cb(decompose_trace(trace).metadata["x"],trace.value...)
if !(typeof(cb_call) <: Bool)
error("The callback should return a boolean `halt` for whether to stop the optimization process.")
end
cur, state = iterate(data, state)
cb_call
end
_loss = function(θ)
x = prob.f(θ, prob.p, cur...)
return first(x)
end
if !(isnothing(maxiters)) && maxiters <= 0.0
error("The number of maxiters has to be a non-negative and non-zero number.")
elseif !(isnothing(maxiters))
maxiters = convert(Int, maxiters)
end
result = CMAEvolutionStrategy.minimize(_loss, prob.u0, 0.1; lower = prob.lb, upper = prob.ub, maxiter = maxiters, kwargs...)
CMAEvolutionStrategy.print_header(result)
CMAEvolutionStrategy.print_result(result)
println("\n")
criterion = true
if (result.stop.reason === :maxtime) #this is an arbitrary choice of convergence (based on the stop.reason values)
criterion = false
end
SciMLBase.build_solution(prob, opt, result.logger.xbest[end], result.logger.fbest[end]; original=result)
end
end
end