@@ -9,14 +9,33 @@ for j in string.(BlackBoxOptim.SingleObjectiveMethodNames)
9
9
eval (Meta. parse (" export BBO_" * j))
10
10
end
11
11
12
- decompose_trace (opt:: BlackBoxOptim.OptRunController ) = BlackBoxOptim. best_candidate (opt)
12
+ function decompose_trace (opt:: BlackBoxOptim.OptRunController , progress)
13
+ if progress
14
+ maxiters = opt. max_steps
15
+ max_time = opt. max_time
16
+ msg = " loss: " * sprint (show, best_fitness (opt), context = :compact => true )
17
+ if iszero (max_time)
18
+ # we stop at either convergence or max_steps
19
+ n_steps = BlackBoxOptim. num_steps (opt)
20
+ Base. @logmsg (Base. LogLevel (- 1 ), msg, progress= n_steps / maxiters,
21
+ _id= :OptimizationBBO )
22
+ else
23
+ # we stop at either convergence or max_time
24
+ elapsed = BlackBoxOptim. elapsed_time (opt)
25
+ Base. @logmsg (Base. LogLevel (- 1 ), msg, progress= elapsed / max_time,
26
+ _id= :OptimizationBBO )
27
+ end
28
+ end
29
+ return BlackBoxOptim. best_candidate (opt)
30
+ end
13
31
14
32
function __map_optimizer_args (prob:: SciMLBase.OptimizationProblem , opt:: BBO ;
15
33
callback = nothing ,
16
34
maxiters:: Union{Number, Nothing} = nothing ,
17
35
maxtime:: Union{Number, Nothing} = nothing ,
18
36
abstol:: Union{Number, Nothing} = nothing ,
19
37
reltol:: Union{Number, Nothing} = nothing ,
38
+ verbose:: Bool = false ,
20
39
kwargs... )
21
40
if ! isnothing (reltol)
22
41
@warn " common reltol is currently not used by $(opt) "
@@ -44,6 +63,12 @@ function __map_optimizer_args(prob::SciMLBase.OptimizationProblem, opt::BBO;
44
63
mapped_args = (; mapped_args... , MinDeltaFitnessTolerance = abstol)
45
64
end
46
65
66
+ if verbose
67
+ mapped_args = (; mapped_args... , TraceMode = :verbose )
68
+ else
69
+ mapped_args = (; mapped_args... , TraceMode = :silent )
70
+ end
71
+
47
72
return mapped_args
48
73
end
49
74
@@ -54,6 +79,7 @@ function SciMLBase.__solve(prob::SciMLBase.OptimizationProblem, opt::BBO,
54
79
maxtime:: Union{Number, Nothing} = nothing ,
55
80
abstol:: Union{Number, Nothing} = nothing ,
56
81
reltol:: Union{Number, Nothing} = nothing ,
82
+ verbose:: Bool = false ,
57
83
progress = false , kwargs... )
58
84
local x, cur, state
59
85
@@ -64,7 +90,7 @@ function SciMLBase.__solve(prob::SciMLBase.OptimizationProblem, opt::BBO,
64
90
cur, state = iterate (data)
65
91
66
92
function _cb (trace)
67
- cb_call = callback (decompose_trace (trace), x... )
93
+ cb_call = callback (decompose_trace (trace, progress ), x... )
68
94
if ! (typeof (cb_call) <: Bool )
69
95
error (" The callback should return a boolean `halt` for whether to stop the optimization process." )
70
96
end
@@ -85,12 +111,19 @@ function SciMLBase.__solve(prob::SciMLBase.OptimizationProblem, opt::BBO,
85
111
86
112
opt_args = __map_optimizer_args (prob, opt, callback = _cb, maxiters = maxiters,
87
113
maxtime = maxtime, abstol = abstol, reltol = reltol;
88
- kwargs... )
114
+ verbose = verbose, kwargs... )
89
115
90
116
opt_setup = BlackBoxOptim. bbsetup (_loss; opt_args... )
91
117
92
118
t0 = time ()
119
+
93
120
opt_res = BlackBoxOptim. bboptimize (opt_setup)
121
+
122
+ if progress
123
+ # Set progressbar to 1 to finish it
124
+ Base. @logmsg (Base. LogLevel (- 1 ), " " , progress= 1 , _id= :OptimizationBBO )
125
+ end
126
+
94
127
t1 = time ()
95
128
96
129
opt_ret = Symbol (opt_res. stop_reason)
0 commit comments