Skip to content

Commit 1d073d3

Browse files
Run SciPy optimizers sequentially and add error handling to benchmark
Signed-off-by: AdityaPandeyCN <[email protected]>
1 parent 0c86ed9 commit 1d073d3

File tree

1 file changed

+46
-17
lines changed

1 file changed

+46
-17
lines changed

benchmarks/GlobalOptimization/blackbox_global_optimizers.jmd

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ difficult global optima.
1515
```julia
1616
using BlackBoxOptimizationBenchmarking, Plots, Optimization, Memoize, Statistics
1717
import BlackBoxOptimizationBenchmarking.Chain
18+
using BlackBoxOptimizationBenchmarking: BenchmarkSetup
1819
const BBOB = BlackBoxOptimizationBenchmarking
1920

2021
using OptimizationBBO, OptimizationOptimJL, OptimizationEvolutionary, OptimizationNLopt
21-
using OptimizationMetaheuristics, OptimizationNOMAD, OptimizationPRIMA, OptimizationOptimisers, OptimizationSciPy, OptimizationPyCMA
22+
using OptimizationMetaheuristics, OptimizationNOMAD, OptimizationPRIMA, OptimizationOptimisers, OptimizationSciPy
2223
```
2324

2425
```julia
@@ -32,7 +33,15 @@ test_functions = BBOB.list_functions()
3233
dimension = 3
3334
run_length = round.(Int, 10 .^ LinRange(1,5,30))
3435

35-
@memoize run_bench(algo) = BBOB.benchmark(setup[algo], test_functions, run_length, Ntrials=40, dimension = dimension)
36+
@memoize function run_bench(algo)
37+
return BBOB.benchmark(setup[algo], test_functions, run_length; Ntrials = 40, dimension = dimension)
38+
end
39+
40+
const python_optimizers = Set([
41+
"ScipyDifferentialEvolution", "ScipyBasinhopping", "ScipyDualAnnealing",
42+
"ScipyShgo", "ScipyDirect", "ScipyBrute"
43+
])
44+
3645
```
3746

3847
```julia
@@ -60,11 +69,11 @@ setup = Dict(
6069
"Optimisers.RMSProp" => chain(Optimisers.RMSProp(), isboxed=false),
6170
# SciPy global optimizers
6271
"ScipyDifferentialEvolution" => chain(ScipyDifferentialEvolution(), isboxed=true),
63-
"ScipyBasinhopping" => chain(ScipyBasinhopping(), isboxed=true),
64-
"ScipyDualAnnealing" => chain(ScipyDualAnnealing(), isboxed=true),
65-
"ScipyShgo" => chain(ScipyShgo(), isboxed=true),
66-
"ScipyDirect" => chain(ScipyDirect(), isboxed=true),
67-
"ScipyBrute" => chain(ScipyBrute(), isboxed=true),
72+
"ScipyBasinhopping" => chain(ScipyBasinhopping(), isboxed=true),
73+
"ScipyDualAnnealing" => chain(ScipyDualAnnealing(), isboxed=true),
74+
"ScipyShgo" => chain(ScipyShgo(), isboxed=true),
75+
"ScipyDirect" => chain(ScipyDirect(), isboxed=true),
76+
"ScipyBrute" => chain(ScipyBrute(), isboxed=true),
6877
# "NOMADOpt" => chain(NOMADOpt()), too much printing
6978
# "OptimizationPRIMA.UOBYQA()" => chain(OptimizationPRIMA.UOBYQA()), :StackOverflowError?
7079
# "OptimizationPRIMA.NEWUOA()" => OptimizationPRIMA.UOBYQA(),
@@ -91,30 +100,52 @@ f = test_functions[3]
91100

92101
single_setup = BenchmarkSetup(NLopt.GN_CRS2_LM(), isboxed=true)
93102

94-
sol = [BBOB.solve_problem(single_setup, f, 3, 5_000) for in in 1:10]
95-
@info [sol.objective < Δf + f.f_opt for sol in sol]
103+
sol = [BBOB.solve_problem(single_setup, f, 3, 5_000) for _ in 1:10]
104+
@info [s.objective < Δf + f.f_opt for s in sol]
96105

97106
p = plot(f, size = (600,600), zoom = 1.5)
98-
for sol in sol
99-
scatter!(sol.u[1:1], sol.u[2:2], label="", c="blue", marker = :xcross, markersize=5, markerstrokewidth=0)
107+
for s in sol
108+
scatter!(s.u[1:1], s.u[2:2], label = "", c = "blue", marker = :xcross, markersize = 5, markerstrokewidth = 0)
100109
end
101110
p
102111
```
103112

104113
## Test all
105114

106115
```julia
107-
results = Array{BBOB.BenchmarkResults}(undef, length(setup))
116+
results = Vector{Union{BBOB.BenchmarkResults, Nothing}}(undef, length(setup))
117+
118+
all_keys = collect(keys(setup))
119+
120+
for (i, algo) in enumerate(all_keys)
121+
if algo in python_optimizers
122+
try
123+
results[i] = run_bench(algo)
124+
catch err
125+
@warn "Benchmark for $algo failed – skipping" exception = err
126+
end
127+
end
128+
end
108129

109-
Threads.@threads for (i,algo) in collect(enumerate(keys(setup)))
110-
results[i] = run_bench(algo)
130+
Threads.@threads for (i, algo) in collect(enumerate(all_keys))
131+
if !(algo in python_optimizers)
132+
try
133+
results[i] = run_bench(algo)
134+
catch err
135+
@warn "Benchmark for $algo failed – skipping" exception = err
136+
end
137+
end
111138
end
112139

140+
labels = all_keys
141+
valid_idx = [i for i in eachindex(results) if results[i] !== nothing]
142+
results = results[valid_idx]
143+
labels = labels[valid_idx]
144+
113145
results
114146
```
115147

116148
```julia
117-
labels = collect(keys(setup))
118149
idx = sortperm([b.success_rate[end] for b in results], rev=true)
119150

120151
p = plot(xscale = :log10, legend = :outerright, size = (700,350), margin=10Plots.px, dpi=200)
@@ -142,7 +173,6 @@ p = heatmap(
142173
```
143174

144175
```julia
145-
labels = collect(keys(setup))
146176
idx = sortperm([b.distance_to_minimizer[end] for b in results], rev=false)
147177

148178
p = plot(xscale = :log10, legend = :outerright, size = (900,500), margin=10Plots.px, ylim = (0,5))
@@ -167,4 +197,3 @@ bar(
167197
legend = false, margin = 25Plots.px
168198
)
169199
```
170-

0 commit comments

Comments
 (0)