Skip to content

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 to Algorithm, or a list of algorithms labeled by their short_name (repeated names get " (2)", " (3)" and so on).
  • problems (list of Problem): Problem names must be unique. With n_jobs > 1 the 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 run i gets its random numbers. With "numpy", run i uses numpy.random.SeedSequence(seed, spawn_key=(a, p, i)), where a and p are stable 32-bit hashes of the label and the problem name: every run has its own independent stream. With "studio", run i uses the Studio generator with the seed ypea.derive_seed(seed, i) (see the guide Algorithms), so run i gets the same seed for every algorithm and problem (common random numbers). With either rule a result does not depend on n_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 and metadata.json. With None, results stay in memory.
  • checkpoints (sequence of float or None): Fractions of max_nfe at which the best objective value (and the error, when the problem has an optimum) 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.

algorithms instance-attribute

algorithms = {str(k): v for k, v in algorithms.items()}

problems instance-attribute

problems = list(problems)

checkpoints instance-attribute

checkpoints = tuple(CEC_CHECKPOINTS if checkpoints is None else checkpoints)

_records instance-attribute

_records = {}

_indicators instance-attribute

_indicators = {p.name: indicator_setup(p) for p in self.problems}

_indicator_info instance-attribute

_indicator_info = {k: v and _indicator_info(v) for k, v in self._indicators.items()}

_algs instance-attribute

_algs = {k: self._with_budget(a) for k, a in self.algorithms.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.

_with_budget

_with_budget(alg)

_seed

_seed(label, problem, i)

The seed of one run and a plain description of it (part of the run key).

_tasks

_tasks()

metadata

metadata()

The settings of the experiment and the versions and machine it runs on, as saved in metadata.json.

run

run()

Run every missing run, skipping finished ones with the same settings. Returns self.

report

report()

A Report of the stored runs (from the output folder, or from memory).

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).

zero_below class-attribute instance-attribute

zero_below = None

labels instance-attribute

labels = list(metadata['algorithms'])

problems instance-attribute

problems = [p['name'] for p in metadata['problems']]

_problem instance-attribute

_problem = {p['name']: p for p in metadata['problems']}

_runs instance-attribute

_runs = {}

load classmethod

load(path)

records

records(label, problem)

The run records of one algorithm on one problem, in run order.

_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.

_floor

_floor(errors)

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.

_grid

_grid(problem, points=200)

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.

_ylabel

_ylabel(problem, metric, final=False)

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.

_cells

_cells(bold, marks, esc=lambda s: s)

to_csv

to_csv(path=None)

The table as CSV text, also written to path when given.

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.

to_dataframe

to_dataframe()

A pandas DataFrame of the cell texts (needs pandas).

__str__

__str__()

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).