Skip to content

Commit 1669486

Browse files
committed
updated the repeated_bboptimize and compare_optimizers since they still used old bboptimize interface; this fixes the ackley_100dim example
1 parent 50e7815 commit 1669486

File tree

4 files changed

+36
-23
lines changed

4 files changed

+36
-23
lines changed

examples/ackley_100dim.jl

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
using BlackBoxOptim
22

3-
p = BlackBoxOptim.example_problems["Ackley"]
4-
5-
BlackBoxOptim.repeated_bboptimize(5, p, 100, [
3+
problem = BlackBoxOptim.example_problems["Ackley"]
4+
numrepeats = 5
5+
dim = 100
6+
methods = [
67
:generating_set_search,
78
:probabilistic_descent,
89
:adaptive_de_rand_1_bin_radiuslimited,
910
:random_search,
10-
:separable_nes],
11-
20.0)
11+
:separable_nes]
12+
max_time = 10.0
13+
ftol = 1e-5
14+
15+
BlackBoxOptim.repeated_bboptimize(numrepeats, problem, dim, methods, max_time, ftol)

src/compare_optimizers.jl

+18-17
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,10 @@ function report_from_result_dict(statsdict)
109109
report_on_values("Fitness", statsdict[:fitnesses], " ")
110110
report_on_values("Time", statsdict[:times], " ")
111111
report_on_values("Num function evals", statsdict[:numevals], " ")
112-
println(" Success rate: ", round(pdict["Within fitness tolerance of optimum"], 3), "%\n")
113-
pdict["Within fitness tolerance of optimum"]
112+
113+
success_rate = round(get(pdict, "Within fitness tolerance of optimum", 0.0), 3)
114+
println(" Success rate: ", success_rate, "%\n")
115+
success_rate
114116
end
115117

116118
function rank_result_dicts_by(result_dicts, byfunc, desc; rev = false,
@@ -139,42 +141,41 @@ function report_on_methods_results_on_one_problem(problem, result_dicts, numrepe
139141
rank_result_dicts_by(result_dicts, (d) -> median(d[:fitnesses]), "fitness"; descsummary = "median")
140142
rank_result_dicts_by(result_dicts, (d) -> median(d[:times]), "time (in seconds)";
141143
descsummary = "median", rpad = " secs")
142-
rank_result_dicts_by(result_dicts, (d) -> int(median(d[:numevals])), "num function evals"; descsummary = "median")
144+
rank_result_dicts_by(result_dicts, (d) -> round(Int, median(d[:numevals])), "num function evals"; descsummary = "median")
143145

144146
for rd in result_dicts
145147
report_from_result_dict(rd)
146148
end
147149

148150
end
149151

150-
function repeated_bboptimize(numrepeats, problem, dim, methods, max_time, ftol = 1e-5, parameters::Parameters = EMPTY_PARAMS)
152+
function repeated_bboptimize(numrepeats, problem, dim, methods, max_time, ftol = 1e-5, extraparams::Parameters = EMPTY_PARAMS)
151153

152154
fp = instantiate(problem, dim)
153155
result_dicts = ParamsDict[]
154156

155157
# Just so they are declared
156158
ps = best_so_far = nothing
157159

158-
params = chain(parameters, ParamsDict(:FitnessTolerance => ftol))
160+
params = chain(extraparams, ParamsDict(:FitnessTolerance => ftol))
159161

160162
for m in methods
161163

162164
ts, fs, nes = zeros(numrepeats), zeros(numrepeats), zeros(Int, numrepeats)
163-
rcounts = Dict{String,Int}("Within fitness tolerance of optimum" => 0)
165+
rcounts = Dict{String,Int}()
164166

165-
for i in 1:numrepeats
166-
p = fp # BlackBoxOptim.ShiftedAndBiasedProblem(fp)
167-
best, fs[i], reason, ts[i], ps, nes[i] = bboptimize(p; max_time = max_time,
168-
method = m, parameters = params)
169-
rcounts[reason] = 1 + get(rcounts, reason, 0)
170-
end
167+
results = map(1:numrepeats) do i
168+
result = bboptimize(fp, params; MaxTime = max_time, Method = m)
171169

172-
if best_so_far == nothing
173-
best_so_far = worst_fitness(ps[:Evaluator])
174-
end
170+
# Save key data about this run:
171+
ts[i] = elapsed_time(result)
172+
fs[i] = best_fitness(result)
173+
nes[i] = f_calls(result)
175174

176-
# ???
177-
best_so_far =
175+
# And count only the general stop reasons
176+
reason = general_stop_reason(result)
177+
rcounts[reason] = 1 + get(rcounts, reason, 0)
178+
end
178179

179180
rdict = ParamsDict(:method => m, :fitnesses => fs, :times => ts, :numevals => nes, :reasoncounts => rcounts)
180181
rdict[:success_rate] = report_from_result_dict(rdict)

test/test_bboptimize.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ end
1818
end
1919

2020
@testset "example #3" begin
21-
res = bboptimize(rosenbrock2d; SearchRange = (-5.0, 5.0), NumDimensions = 2, method = :de_rand_1_bin, TraceMode = :silent)
21+
res = bboptimize(rosenbrock2d; SearchRange = (-5.0, 5.0), NumDimensions = 2, Method = :de_rand_1_bin, TraceMode = :silent)
2222
@test best_fitness(res) < 0.001
2323
end
2424

test/test_parameters.jl

+8
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@
8585
dc = chain(d3, d1, d2)
8686
@test (dc[:a], dc[:b], dc[:c]) == (2, 4, 5)
8787
end
88+
89+
@testset "Check chaining from real use case" begin
90+
# This sequence is used in compare_optimizers.jl:repeated_bboptimize
91+
parameters = BlackBoxOptim.EMPTY_PARAMS
92+
ftol = 1e-5
93+
params = chain(parameters, ParamsDict(:FitnessTolerance => ftol))
94+
@test params[:FitnessTolerance] == ftol
95+
end
8896
end
8997

9098
@testset "converting to Dict" begin

0 commit comments

Comments
 (0)