Tuning algorithm settings¶
The defaults come from each algorithm's paper or the standard literature. They are a good start, but the best settings depend on your problems and budget. Tuning searches for better settings by running the algorithm many times on training problems. ypea.tuning offers two tools:
tune: iterated racing, the method of the irace package. It spends most of the budget on promising settings and drops poor ones early.sweep: a grid, a list or random settings, each run on every problem as a normal experiment with the full report.
The statistical tests need SciPy (pip install "ypea[stats]").
When to tune¶
Tune when you will solve many similar problems, when a paper compares algorithms and each should have a fair chance, or when the defaults clearly struggle. Never tune on the problems you report results for: the settings fit those problems, so the results look better than they are.
How racing works¶
A race runs several settings on a stream of instances, each instance being one problem with one seed shared by all settings. On each instance the results are ranked: smallest error first (or best objective value without a known optimum), and infeasible results last. After first_test instances (5 by default), a Friedman test checks whether the average ranks of the settings differ more than chance would explain. If they do, a post hoc test (Holm corrected) drops the settings that are clearly worse than the best. See Statistical tests for these tests. The race goes on, one instance at a time, until few settings are left or its share of the budget is used.
Iterated racing repeats this. The first race starts from random settings. The survivors (elites) keep their results and go on to the next race, together with new settings drawn near them, closer each iteration. Tuning ends when the budget is used.
%%{init: {"flowchart": {"curve": "monotoneY"}}}%%
flowchart TB
D("draw settings<br>from the space") --> R("race: run every setting<br>on the next instance")
R --> F{"Friedman test<br>after first_test instances"}
F -- "clearly worse" --> X("drop")
F -- "race goes on" --> R
F -- "race over" --> E("elites")
E -- "budget left" --> N("draw new settings<br>near the elites")
N --> R
E -- "budget used" --> B("best setting")
Example¶
This example tunes every setting of DE on four shifted and rotated classic functions in 10 dimensions (see Benchmarks). budget=2000 allows 2000 runs in total, each with at most 20 000 evaluations (calls of the objective function). With 4 worker processes it takes one to two minutes. The if __name__ == "__main__": line is needed for worker processes on Windows and macOS:
import ypea
from ypea.benchmarks import problem
from ypea.tuning import tune
if __name__ == "__main__":
train = []
for name in ["rastrigin", "ellipsoid", "rosenbrock", "ackley"]:
train.append(problem(name, 10, shift=True, rotate=True, seed=1))
result = tune(
ypea.DE,
train,
budget=2000,
max_nfe_per_run=20_000,
n_jobs=4,
seed=0,
output="results/tune-de",
)
print(result.best)
first = result.history[0]
print(first["settings"], first["instances"], first["runs"], first["survivors"])
de = result.algorithm(max_nfe=20_000) # the tuned algorithm, ready to use
{'pop_size': 52, 'base_vector': 'best', 'diff_vectors_count': 1, 'crossover_method': 'exp', 'crossover_prob': 0.83, 'beta_min': 0.4706, 'beta_max': 0.5726}
83 6 484 55
Differs from Python: runs serially here (n_jobs=4 needs the Parallel Computing Toolbox) and takes about 8 minutes; MATLAB draws its own random numbers.
names = ["rastrigin", "ellipsoid", "rosenbrock", "ackley"];
train = {};
for name = names
train{end + 1} = ypea.benchmarks.problem(name, 10, shift=true, rotate=true, seed=1); %#ok<SAGROW>
end
result = ypea.tuning.tune( ...
@ypea.DE, ...
train, ...
2000, ...
max_nfe_per_run=20000, ...
seed=0, ...
output="results/tune-de");
disp(result.best)
first = result.history(1);
fprintf("%d %d %d %d\n", first.settings, first.instances, first.runs, first.survivors);
de = result.algorithm(max_nfe=20000); % the tuned algorithm, ready to use
pop_size: 36
base_vector: "best"
diff_vectors_count: 1
crossover_method: "exp"
crossover_prob: 0.6938
beta_min: 0.3656
beta_max: 0.7780
83 6 474 36
The first line is the best setting found. The second describes the first race: 83 random settings ran on 6 instances (484 runs, because settings were dropped along the way), and 55 survived. result.runs is the number of runs used in total, and result.elites holds the surviving settings of the last race, best first.
budget counts runs, not evaluations. With output, every run is saved, and the same call resumes an interrupted tuning. The result depends only on seed, not on n_jobs.
Choosing what to tune¶
ypea.tuning.space(ypea.DE) shows the built-in space, the settings that tune varies: the type, typical range and valid range of each setting, and in .fixed the settings left out and why. Fix settings with fixed, or pass your own space. This example tunes only three settings of DE, with beta_min and beta_max fixed, in a budget of 1000 runs of 10 000 evaluations each (the default max_nfe_per_run). log=True samples the population size on a log scale, so small values are tried as often as large ones:
import ypea
from ypea.benchmarks import problem
from ypea.tuning import Space, categorical, integer, real, tune
train = []
for name in ["rastrigin", "ellipsoid", "rosenbrock", "ackley"]:
train.append(problem(name, 10, shift=True, rotate=True, seed=1))
space = Space({
"crossover_prob": real(0.5, 1.0),
"pop_size": integer(20, 200, log=True),
"base_vector": categorical("rand", "best"),
})
fixed = {"beta_min": 0.3, "beta_max": 0.7}
result = tune(ypea.DE, train, budget=1000, space=space, fixed=fixed)
print(result.summary())
Tuning DE
best setting: {'crossover_prob': 0.6515, 'pop_size': 46, 'base_vector': 'best'}
runs used: 991 of 1000
(the rest is too few for one more race step, which runs every remaining setting once)
Differs from Python: MATLAB draws its own random numbers, so the settings found differ from Python's; this block takes about 3 minutes.
names = ["rastrigin", "ellipsoid", "rosenbrock", "ackley"];
train = {};
for name = names
train{end + 1} = ypea.benchmarks.problem(name, 10, shift=true, rotate=true, seed=1); %#ok<SAGROW>
end
space = ypea.tuning.Space(struct( ...
"crossover_prob", ypea.tuning.real(0.5, 1.0), ...
"pop_size", ypea.tuning.integer(20, 200, log=true), ...
"base_vector", ypea.tuning.categorical("rand", "best")));
fixed = struct("beta_min", 0.3, "beta_max", 0.7);
result = ypea.tuning.tune(@ypea.DE, train, 1000, space=space, fixed=fixed);
disp(result.summary())
Tuning ypea.DE
best setting: {"crossover_prob":0.6961,"pop_size":63,"base_vector":"best"}
runs used: 967 of 1000
(the rest is too few for one more race step, which runs every remaining setting once)
The summary gives the best setting and the runs used. The budget is an upper limit: each race step runs every remaining setting on one more instance, so a few runs too few for a whole step stay unused.
Fewer tuned settings need a smaller budget. When the budget is too small, tuning stops early with a warning, and result.runs shows how many runs it used.
Sweeps¶
A sweep runs every setting on every problem runs times and returns an experiment Report, with the same tables and tests. This example first runs a grid of 3 crossover probabilities and 2 population sizes (6 settings) and prints the mean errors, then 20 random settings from the built-in space, labeled "DE 1" to "DE 20", and saves their table:
import ypea
from ypea.benchmarks import problem
from ypea.tuning import sweep
if __name__ == "__main__":
train = []
for name in ["rastrigin", "ellipsoid", "rosenbrock", "ackley"]:
train.append(problem(name, 10, shift=True, rotate=True, seed=1))
grid = {
"crossover_prob": [0.1, 0.5, 0.9],
"pop_size": [20, 50],
}
report = sweep(
ypea.DE,
grid,
train,
runs=10,
max_nfe=20_000,
n_jobs=4,
)
print(report.table(stats=["mean"], test=None))
# None: random settings from the built-in space
report = sweep(
ypea.DE,
None,
train,
n=20,
runs=5,
max_nfe=20_000,
n_jobs=4,
)
table = report.table()
table.to_markdown("sweep.md")
| Problem | Statistic | DE(crossover_prob=0.1, pop_size=20) | DE(crossover_prob=0.1, pop_size=50) | DE(crossover_prob=0.5, pop_size=20) | DE(crossover_prob=0.5, pop_size=50) | DE(crossover_prob=0.9, pop_size=20) | DE(crossover_prob=0.9, pop_size=50) |
|:---|:---|---:|---:|---:|---:|---:|---:|
| rastrigin_d10_shifted_rotated_s1 | Mean | 1.744e+01 | 2.306e+01 | 2.590e+01 | 3.466e+01 | **1.562e+01** | 3.215e+01 |
| ellipsoid_d10_shifted_rotated_s1 | Mean | 2.275e+06 | 3.730e+06 | 1.260e+06 | 4.167e+06 | 8.356e+05 | **1.255e+03** |
| rosenbrock_d10_shifted_rotated_s1 | Mean | 8.834e+01 | 3.318e+01 | 7.838e+00 | 3.642e+01 | 2.601e+02 | **5.641e+00** |
| ackley_d10_shifted_rotated_s1 | Mean | 3.060e-05 | 1.038e-01 | **1.021e-14** | 1.190e-07 | 2.059e-01 | 1.385e-07 |
| Friedman | Avg. rank | 3.75 | 4.00 | **2.50** | 4.50 | 3.75 | **2.50** |
| | Holm APV | 1 | 1 | control | 0.653 | 1 | 1 |
Friedman test on the mean error over 4 problems: chi2 = 3.857, p = 0.57. Iman and Davenport: F = 0.7168, p = 0.621. Post hoc z test against DE(crossover_prob=0.5, pop_size=20) with Holm adjusted p values (Derrac et al. 2011).
Differs from Python: runs serially here (n_jobs=4 needs the Parallel Computing Toolbox) and takes about 4 minutes; the table goes to results/.
names = ["rastrigin", "ellipsoid", "rosenbrock", "ackley"];
train = {};
for name = names
train{end + 1} = ypea.benchmarks.problem(name, 10, shift=true, rotate=true, seed=1); %#ok<SAGROW>
end
grid = struct( ...
"crossover_prob", [0.1 0.5 0.9], ...
"pop_size", [20 50]);
report = ypea.tuning.sweep( ...
@ypea.DE, ...
grid, ...
train, ...
runs=10, ...
max_nfe=20000);
disp(report.table(stats="mean", test=""))
% []: random settings from the built-in space
report = ypea.tuning.sweep( ...
@ypea.DE, ...
[], ...
train, ...
n=20, ...
runs=5, ...
max_nfe=20000);
table = report.table();
table.to_markdown("results/sweep.md");
| Problem | Statistic | DE(crossover_prob=0.1, pop_size=20) | DE(crossover_prob=0.1, pop_size=50) | DE(crossover_prob=0.5, pop_size=20) | DE(crossover_prob=0.5, pop_size=50) | DE(crossover_prob=0.9, pop_size=20) | DE(crossover_prob=0.9, pop_size=50) |
|:---|:---|---:|---:|---:|---:|---:|---:|
| rastrigin_d10_shifted_rotated_s1 | Mean | 1.736e+01 | 2.804e+01 | 2.893e+01 | 3.241e+01 | **9.603e+00** | 2.963e+01 |
| ellipsoid_d10_shifted_rotated_s1 | Mean | 1.613e+06 | 3.191e+06 | 1.247e+06 | 4.247e+06 | 2.962e+05 | **5.760e+03** |
| rosenbrock_d10_shifted_rotated_s1 | Mean | 9.080e+01 | 1.400e+02 | 1.888e+02 | 1.707e+02 | 1.863e+03 | **4.984e+00** |
| ackley_d10_shifted_rotated_s1 | Mean | 2.155e-04 | 2.490e-02 | **7.017e-15** | 9.467e-08 | 8.093e-01 | 1.638e-07 |
| Friedman | Avg. rank | 3.00 | 4.00 | 3.25 | 4.50 | 3.75 | **2.50** |
| | Holm APV | 1 | 1 | 1 | 0.653 | 1 | control |
Friedman test on the mean error over 4 problems: chi2 = 3, p = 0.7. Iman and Davenport: F = 0.5294, p = 0.751. Post hoc z test against DE(crossover_prob=0.9, pop_size=50) with Holm adjusted p values (Derrac et al. 2011).
No setting wins on every function, and the Friedman p value of 0.57 shows no significant difference between the six on these four problems.
A sweep is easier to explain than a race and shows how each setting behaves, but it needs many more runs for the same coverage.
Reporting tuned settings fairly¶
-
Tune and test on different instances: other shift and rotation seeds (for example
seed=1to tune andseed=2to test), other dimensions or other functions. With CEC suites, tune on one suite and test on another. -
Give every algorithm in a comparison the same tuning budget and
max_nfe_per_run, and tune them all or none. -
Report the setup: the space, the budget,
max_nfe_per_run, the tuning instances, the seed and the ypea version. Theoutputfolder holdstune.jsonwith the result and the history of every iteration. -
Report the tuned values next to the defaults, with test results for both.
References¶
M. Lopez-Ibanez, J. Dubois-Lacoste, L. Perez Caceres, M. Birattari and T. Stutzle (2016). The irace package: Iterated racing for automatic algorithm configuration. Operations Research Perspectives 3, 43 to 58.
M. Birattari, T. Stutzle, L. Paquete and K. Varrentrapp (2002). A racing algorithm for configuring metaheuristics. Proceedings of GECCO 2002, 11 to 18.