Skip to content

Add progress info for OptimizationBBO #336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Aug 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/OptimizationBBO/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ BlackBoxOptim = "a134a8b2-14d6-55f6-9291-3336d3ab0209"
Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba"

[compat]
julia = "1"
BlackBoxOptim = "0.6"
Optimization = "3"
julia = "1"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
39 changes: 36 additions & 3 deletions lib/OptimizationBBO/src/OptimizationBBO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,33 @@ for j in string.(BlackBoxOptim.SingleObjectiveMethodNames)
eval(Meta.parse("export BBO_" * j))
end

decompose_trace(opt::BlackBoxOptim.OptRunController) = BlackBoxOptim.best_candidate(opt)
function decompose_trace(opt::BlackBoxOptim.OptRunController, progress)
if progress
maxiters = opt.max_steps
max_time = opt.max_time
msg = "loss: " * sprint(show, best_fitness(opt), context = :compact => true)
if iszero(max_time)
# we stop at either convergence or max_steps
n_steps = BlackBoxOptim.num_steps(opt)
Base.@logmsg(Base.LogLevel(-1), msg, progress=n_steps / maxiters,
_id=:OptimizationBBO)
else
# we stop at either convergence or max_time
elapsed = BlackBoxOptim.elapsed_time(opt)
Base.@logmsg(Base.LogLevel(-1), msg, progress=elapsed / max_time,
_id=:OptimizationBBO)
end
end
return BlackBoxOptim.best_candidate(opt)
end

function __map_optimizer_args(prob::SciMLBase.OptimizationProblem, opt::BBO;
callback = nothing,
maxiters::Union{Number, Nothing} = nothing,
maxtime::Union{Number, Nothing} = nothing,
abstol::Union{Number, Nothing} = nothing,
reltol::Union{Number, Nothing} = nothing,
verbose::Bool = false,
kwargs...)
if !isnothing(reltol)
@warn "common reltol is currently not used by $(opt)"
Expand Down Expand Up @@ -44,6 +63,12 @@ function __map_optimizer_args(prob::SciMLBase.OptimizationProblem, opt::BBO;
mapped_args = (; mapped_args..., MinDeltaFitnessTolerance = abstol)
end

if verbose
mapped_args = (; mapped_args..., TraceMode = :verbose)
else
mapped_args = (; mapped_args..., TraceMode = :silent)
end

return mapped_args
end

Expand All @@ -54,6 +79,7 @@ function SciMLBase.__solve(prob::SciMLBase.OptimizationProblem, opt::BBO,
maxtime::Union{Number, Nothing} = nothing,
abstol::Union{Number, Nothing} = nothing,
reltol::Union{Number, Nothing} = nothing,
verbose::Bool = false,
progress = false, kwargs...)
local x, cur, state

Expand All @@ -64,7 +90,7 @@ function SciMLBase.__solve(prob::SciMLBase.OptimizationProblem, opt::BBO,
cur, state = iterate(data)

function _cb(trace)
cb_call = callback(decompose_trace(trace), x...)
cb_call = callback(decompose_trace(trace, progress), x...)
if !(typeof(cb_call) <: Bool)
error("The callback should return a boolean `halt` for whether to stop the optimization process.")
end
Expand All @@ -85,12 +111,19 @@ function SciMLBase.__solve(prob::SciMLBase.OptimizationProblem, opt::BBO,

opt_args = __map_optimizer_args(prob, opt, callback = _cb, maxiters = maxiters,
maxtime = maxtime, abstol = abstol, reltol = reltol;
kwargs...)
verbose = verbose, kwargs...)

opt_setup = BlackBoxOptim.bbsetup(_loss; opt_args...)

t0 = time()

opt_res = BlackBoxOptim.bboptimize(opt_setup)

if progress
# Set progressbar to 1 to finish it
Base.@logmsg(Base.LogLevel(-1), "", progress=1, _id=:OptimizationBBO)
end

t1 = time()

opt_ret = Symbol(opt_res.stop_reason)
Expand Down
14 changes: 14 additions & 0 deletions lib/OptimizationBBO/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,18 @@ using Test
ub = [0.8, 0.8])
sol = solve(prob, BBO_adaptive_de_rand_1_bin_radiuslimited())
@test 10 * sol.minimum < l1

@test_logs begin
(Base.LogLevel(-1), "loss: 0.0")
min_level = Base.LogLevel(-1)
solve(prob, BBO_adaptive_de_rand_1_bin_radiuslimited(), progress = true)
end

@test_logs begin
(Base.LogLevel(-1), "loss: 0.0")
min_level = Base.LogLevel(-1)
solve(prob, BBO_adaptive_de_rand_1_bin_radiuslimited(),
progress = true,
maxtime = 5)
end
end