Skip to content

Commit 23dadb4

Browse files
Automatic build\nPublished by build of: SciML/SciMLBenchmarks.jl@7a43b1a
1 parent 58b3f13 commit 23dadb4

9 files changed

Lines changed: 339 additions & 70 deletions

markdown/GlobalOptimization/blackbox_global_optimizers.md

Lines changed: 230 additions & 68 deletions
Large diffs are not rendered by default.
149 KB
Loading
202 KB
Loading
126 KB
Loading
53 KB
Loading
-147 KB
Loading
169 KB
Loading
7.42 KB
Loading

script/GlobalOptimization/blackbox_global_optimizers.jl

Lines changed: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,90 @@
11

22
using BlackBoxOptimizationBenchmarking, Plots, Optimization, Memoize, Statistics
3-
import BlackBoxOptimizationBenchmarking.Chain
3+
import BlackBoxOptimizationBenchmarking: Chain, BenchmarkSetup, BenchmarkResults,
4+
BBOBFunction, FunctionCallsCounter, solve_problem, pinit, compute_CI
45
const BBOB = BlackBoxOptimizationBenchmarking
56

67
using OptimizationBBO, OptimizationOptimJL, OptimizationEvolutionary, OptimizationNLopt
78
using OptimizationMetaheuristics, OptimizationNOMAD, OptimizationPRIMA, OptimizationOptimisers, OptimizationSciPy, OptimizationPyCMA
89

910

11+
function make_success_tracker(f_raw, f_opt, Δf)
12+
t0 = Ref(time())
13+
time_to_success = Ref(Inf)
14+
function tracked_f(u)
15+
val = f_raw(u)
16+
if val < Δf + f_opt && time_to_success[] == Inf
17+
time_to_success[] = time() - t0[]
18+
end
19+
return val
20+
end
21+
return tracked_f, t0, time_to_success
22+
end
23+
24+
function solve_problem_timed(optimizer::BenchmarkSetup, tracked_f, D::Int, run_length::Int;
25+
u0 = pinit(D))
26+
method = optimizer.method
27+
optf = OptimizationFunction((u, _) -> tracked_f(u), AutoForwardDiff())
28+
if optimizer.isboxed
29+
prob = OptimizationProblem(optf, u0, lb = fill(-5.5, D), ub = fill(5.5, D))
30+
else
31+
prob = OptimizationProblem(optf, u0)
32+
end
33+
sol = solve(prob, method; maxiters = run_length)
34+
sol
35+
end
36+
37+
function solve_problem_timed(m::Chain, tracked_f, D::Int, run_length::Int)
38+
rl1 = round(Int, m.p * run_length)
39+
rl2 = run_length - rl1
40+
sol = solve_problem_timed(m.first, tracked_f, D, rl1)
41+
xinit = sol.u
42+
sol = solve_problem_timed(m.second, tracked_f, D, rl2; u0 = xinit)
43+
end
44+
45+
function benchmark_time_to_success(
46+
optimizer::Union{Chain, BenchmarkSetup}, f::BBOBFunction;
47+
Ntrials::Int = 20, dimension::Int = 3, Δf::Real = 1e-6, max_run_length::Int = 100_000
48+
)
49+
times = Float64[]
50+
for i in 1:Ntrials
51+
tracked_f, t0_ref, tts_ref = make_success_tracker(f.f, f.f_opt, Δf)
52+
try
53+
t0_ref[] = time()
54+
sol = solve_problem_timed(optimizer, tracked_f, dimension, max_run_length)
55+
push!(times, tts_ref[])
56+
catch err
57+
push!(times, Inf)
58+
@warn(string(optimizer, " failed: ", err))
59+
end
60+
end
61+
return times
62+
end
63+
64+
benchmark_time_to_success(optimizer, f; kwargs...) =
65+
benchmark_time_to_success(BenchmarkSetup(optimizer), f; kwargs...)
66+
67+
function benchmark_time_to_success(
68+
optimizer::Union{Chain, BenchmarkSetup}, funcs::Vector{BBOBFunction};
69+
Ntrials::Int = 20, dimension::Int = 3, Δf::Real = 1e-6, max_run_length::Int = 100_000
70+
)
71+
all_times = Float64[]
72+
for f in funcs
73+
append!(all_times, benchmark_time_to_success(
74+
optimizer, f; Ntrials, dimension, Δf, max_run_length))
75+
end
76+
return all_times
77+
end
78+
79+
benchmark_time_to_success(optimizer, funcs::Vector{BBOBFunction}; kwargs...) =
80+
benchmark_time_to_success(BenchmarkSetup(optimizer), funcs; kwargs...)
81+
82+
function success_rate_cdf(all_times::Vector{Float64}, time_thresholds::AbstractVector{Float64})
83+
N = length(all_times)
84+
return [count(x -> x <= t, all_times) / N for t in time_thresholds]
85+
end
86+
87+
1088
chain = (t;
1189
isboxed = false) -> Chain(
1290
BenchmarkSetup(t, isboxed = isboxed),
@@ -20,6 +98,8 @@ run_length = round.(Int, 10 .^ LinRange(1, 5, 30))
2098

2199
@memoize run_bench(algo) = BBOB.benchmark(
22100
setup[algo], test_functions, run_length, Ntrials = 40, dimension = dimension)
101+
@memoize run_tts(algo) = benchmark_time_to_success(
102+
setup[algo], test_functions, Ntrials = 40, dimension = dimension)
23103

24104

25105
setup = Dict(
@@ -39,7 +119,7 @@ setup = Dict(
39119
"BBO_separable_nes" => chain(BBO_separable_nes(), isboxed = true),
40120
"BBO_de_rand_2_bin" => chain(BBO_de_rand_2_bin(), isboxed = true),
41121
#"BBO_xnes" => chain(BBO_xnes(), isboxed=true), # good but slow
42-
#"BBO_dxnes" => chain(BBO_dxnes(), isboxed=true),
122+
#"BBO_dxnes" => chain(BBO_dxnes(), isboxed=true),
43123
"OptimizationMetaheuristics.ECA" => chain(OptimizationMetaheuristics.ECA(), isboxed = true),
44124
#"OptimizationMetaheuristics.CGSA" => () -> chain(OptimizationMetaheuristics.CGSA(), isboxed=true), #give me strange results
45125
"OptimizationMetaheuristics.DE" => chain(OptimizationMetaheuristics.DE(), isboxed=true),
@@ -104,6 +184,33 @@ end
104184
p
105185

106186

187+
tts_results = Dict{String, Vector{Float64}}()
188+
189+
for algo in keys(setup)
190+
tts_results[algo] = run_tts(algo)
191+
end
192+
193+
194+
labels = collect(keys(setup))
195+
196+
# Determine time thresholds from data
197+
all_finite = filter(isfinite, vcat(values(tts_results)...))
198+
t_lo = minimum(all_finite) / 2
199+
t_hi = maximum(all_finite) * 2
200+
time_thresholds = 10 .^ range(log10(t_lo), log10(t_hi), length = 50)
201+
202+
cdfs = Dict(algo => success_rate_cdf(tts_results[algo], time_thresholds) for algo in labels)
203+
idx = sortperm([cdfs[l][end] for l in labels], rev = true)
204+
205+
p = plot(xscale = :log10, legend = :outerright,
206+
size = (700, 350), margin = 10Plots.px, dpi = 200,
207+
xlabel = "Wall time (s)", ylabel = "Success rate", ylim = (0, 1))
208+
for i in idx
209+
plot!(time_thresholds, cdfs[labels[i]], label = labels[i], lw = 2.5)
210+
end
211+
p
212+
213+
107214
success_rate_per_function = reduce(hcat, b.success_rate_per_function for b in results)
108215

109216
idx = sortperm(mean(success_rate_per_function, dims = 1)[:], rev = false)

0 commit comments

Comments
 (0)