Experiments¶
Guide: Experiments and statistics.
Experiment ¶
Experiment(algorithms, problems, runs=30, *, max_nfe=None, max_iter=None, seed=0, seeds='numpy', n_jobs=1, output=None, checkpoints=None)
Run every algorithm on every problem runs times, save each run, and report.
Parameters
algorithms(dict or list): A dict from label toAlgorithm, or a list of algorithms labeled by theirshort_name(repeated names get " (2)", " (3)" and so on).problems(list of Problem): Problem names must be unique. Withn_jobs > 1the problems must be picklable (module level objective functions, no lambdas).runs(int): Number of runs of each algorithm on each problem.max_nfe,max_iter(int or None): The budget of every run. If either is given, both replace the limits of each algorithm (a copy is made, the caller's objects are not changed). If both are None, each algorithm keeps its own limits.seed(int): Base seed.seeds("numpy" or "studio"): How runigets its random numbers. With "numpy", runiusesnumpy.random.SeedSequence(seed, spawn_key=(a, p, i)), whereaandpare stable 32-bit hashes of the label and the problem name: every run has its own independent stream. With "studio", runiuses the Studio generator with the seedypea.derive_seed(seed, i)(see the guide Algorithms), so runigets the same seed for every algorithm and problem (common random numbers). With either rule a result does not depend onn_jobs, on the order of the algorithms or problems, or on which of them are included.n_jobs(int): Number of worker processes. Results are the same for any value.output(path or None): Folder for the run files andmetadata.json. With None, results stay in memory.checkpoints(sequence of float or None): Fractions ofmax_nfeat which the best objective value (and the error, when the problem has anoptimum) is recorded. None gives the CEC 2017 rule (0.01, 0.02, 0.03, 0.05, 0.1, 0.2, ..., 1.0). Runs without an evaluation budget record no checkpoints, only their final values. With several objectives the checkpoints hold IGD and hypervolume instead (see below).
Several objectives. Each run also stores the IGD and hypervolume of its final front
and of its front at each checkpoint. The problem needs info["pareto_front"] or
info["ref_point"]. The guide Experiments and statistics explains how both
indicators are computed.
Each run file holds Result.to_dict(), the checkpoint values, the problem sense and
a run key. The key is a hash of the algorithm class and settings, the problem name,
the budget, the seed rule and the checkpoints. run() skips runs whose file has
the same key, so an interrupted experiment resumes, and a changed setting is run again.
Apart from the measured times (run_time and history.time), the files do not
depend on n_jobs.
checkpoints
instance-attribute
¶
checkpoints = tuple(CEC_CHECKPOINTS if checkpoints is None else checkpoints)
_indicator_info
instance-attribute
¶
_indicator_info = {k: v and _indicator_info(v) for k, v in self._indicators.items()}
sweep
classmethod
¶
sweep(algorithm_class, settings, problems, **kwargs)
An experiment over several settings of one algorithm, for tuning studies.
settings is a dict from parameter name to a list of values (every combination
is run), or a list of dicts. Labels name the settings that vary, for example
"DE(F=0.5, CR=0.9)". Other keyword arguments go to Experiment.
_seed ¶
_seed(label, problem, i)
The seed of one run and a plain description of it (part of the run key).
metadata ¶
metadata()
The settings of the experiment and the versions and machine it runs on, as saved in metadata.json.
ypea.experiment.CEC_CHECKPOINTS holds the CEC reporting points as shares of the budget, for the checkpoints setting: 0.01, 0.02, 0.03, 0.05, then 0.1 to 1.0 in steps of 0.1.
Report ¶
Report(metadata, records, output=None)
Results of an experiment, built from its run records.
Make one with Experiment.report(), or later from the output folder alone with
Report.load(path).
metric="error" uses |f - optimum| (smaller is better). On problems without a
known optimum it falls back to the objective value, compared by the problem sense.
With several objectives the metrics are "igd" (smaller is better) and
"hypervolume" (larger is better), as stored by the runner (see Experiment).
There, metric="error" means IGD, or the hypervolume on problems without a true front.
zero_below: errors below this value count as 0 (default None, the raw values).
_key ¶
_key(problem, metric)
The stored value behind metric on problem: "error", "obj_value", "igd" or
"hypervolume".
smaller_is_better ¶
smaller_is_better(problem, metric='error')
True when smaller values of metric are better on problem.
values ¶
values(label, problem, metric='error')
Final values of the runs of one algorithm on one problem.
curves ¶
curves(label, problem, metric='error', grid=None)
Best value so far of each run at the evaluations in grid (default: 200 points
up to the largest NFE). Returns (grid, array of shape (runs, len(grid))).
With several objectives the values are IGD or hypervolume at the checkpoints (the default grid), and each grid point takes the last checkpoint at or before it.
table ¶
table(stat='mean', metric='error', test='wilcoxon', baseline=None, alpha=0.05, fmt='.3e', stats=('mean', 'std', 'best', 'worst', 'median'))
Comparison table: statistics per algorithm and problem with the best in bold,
Wilcoxon rank sum marks and a win/tie/loss row against the control (baseline,
or the best ranked algorithm), and Friedman average ranks of stat with Holm
adjusted post hoc p values (Derrac et al. 2011). test=None skips the rank sum
test. Returns a Table (to_csv, to_latex, to_markdown, to_html,
to_dataframe).
_shown ¶
_shown(metric)
The metric's name in notes and titles: IGD or hypervolume when every problem has
several objectives and metric="error" resolves to the same one.
plot_convergence ¶
plot_convergence(problem, metric='error', center='median', log=None, ax=None)
Median (or mean) best value against evaluations, with a 25 to 75 percent band.
plot_box ¶
plot_box(problem, metric='error', log=None, ax=None)
Box plots of the final values of each algorithm on one problem.
plot_ranks ¶
plot_ranks(stat='mean', metric='error', ax=None)
Bar chart of the Friedman average ranks.
save_html ¶
save_html(path=None, stat='mean', metric='error', baseline=None, figures=True)
One self-contained HTML page with the settings, the table and the figures
(PNG images embedded in the page). Figures need matplotlib and are skipped without
it. path defaults to report.html in the output folder.
Table ¶
Table(header, rows, groups=(), notes=(), data=None)
A table of results. header is a list of column names, rows a list of rows of
cells (dicts with text, bold and mark), groups the row indices where a
new group starts (drawn as rules), notes lines printed under the table, and
data the numbers behind it.
to_markdown ¶
to_markdown(path=None)
The table as Markdown text with its notes, also written to path when given.
to_latex ¶
to_latex(path=None, caption=None, label=None)
A booktabs table (needs \usepackage{booktabs}).
to_html ¶
to_html(path=None)
The table as an HTML fragment with its notes, also written to path when given.
stats ¶
Nonparametric tests for comparing algorithms, following Derrac et al. (2011).
rank_sum: Wilcoxon rank sum test (the Mann Whitney U test) of two samples of runs on one problem. Needs SciPy.friedman: Friedman average ranks over problems, the Friedman statistic with the tie correction, and the Iman and Davenport statistic. Ranks need only NumPy, p values SciPy.posthoc: the z test of each algorithm against a control (Derrac et al. 2011, eq. 12) with Holm-adjusted p values. NumPy only.
Derrac, J., Garcia, S., Molina, D. and Herrera, F. (2011). A practical tutorial on the use of nonparametric statistical tests as a methodology for comparing evolutionary and swarm intelligence algorithms. Swarm and Evolutionary Computation, 1(1), 3 to 18.
rank_sum ¶
rank_sum(a, b, alpha=0.05, smaller_is_better=True)
Compare sample a with sample b. Returns (mark, p value).
The mark is "+" when a is significantly better than b, "-" when it is
significantly worse, and "=" otherwise. Raises ImportError without SciPy.
friedman ¶
friedman(values, smaller_is_better=True)
Friedman test. values has one row per problem and one column per algorithm.
Returns a dict with ranks (average rank of each algorithm, 1 is best), chi2 and
p (Friedman, with the tie correction as in scipy.stats.friedmanchisquare),
f and p_f (Iman and Davenport). The p values are None without SciPy or with
fewer than two problems or algorithms. Missing values (NaN) rank last.
posthoc ¶
posthoc(ranks, n, control)
Each algorithm against the control (an index into ranks) after a Friedman test.
z = (R_i - R_control) / sqrt(k (k + 1) / (6 n)) (Derrac et al. 2011, eq. 12), with
two-sided normal p values and Holm-adjusted p values,
APV_i = min(1, max over j <= i of (k - j) p_j) for the sorted p values.
Returns dicts from algorithm index to z, p and Holm APV (the control is left out).