Skip to content

Results and plots

The result of a run

solve returns a ypea.Result with the best solution, the progress of the run and its settings. summary() gives a short report. The examples on this page minimize the Rastrigin benchmark function in 10 dimensions (best value 0) with L-SHADE and a budget of 20 000 evaluations (calls of the objective function):

import ypea

problem = ypea.benchmarks.problem("rastrigin", 10)
result = ypea.LSHADE(max_nfe=20_000).solve(problem, seed=1)
print(result.summary())
L-SHADE on rastrigin_d10
  best objective value: 4.45623e-07
  error: 4.45623e-07
  feasible: True (violation 0)
  evaluations: 20000, iterations: 430, time: 0.103 s, stopped by: max_nfe

Differs from Python: MATLAB draws its own random numbers, so the values differ from Python's.

problem = ypea.benchmarks.problem("rastrigin", 10);
result = ypea.LSHADE(max_nfe=20000).solve(problem, seed=1);
disp(result.summary())
L-SHADE on rastrigin_d10
  best objective value: 1.11762e-05
  error: 1.11762e-05
  feasible: true (violation 0)
  evaluations: 20000, iterations: 430, time: 0.292 s, stopped by: max_nfe
Field Meaning
best the best solution (an Individual), by the problem's comparison rules
best_feasible the best feasible solution by objective value alone, or None if none was found
front with several objectives, the non-dominated solutions (see Multiple objectives). best and best_feasible are then None
evaluations every evaluated solution, with keep_evaluations=True (see below)
history the progress of the run (see below)
algorithm, params the short name of the algorithm and its settings
problem the name of the problem
seed the seed given to solve
nfe, iterations evaluations and iterations used
run_time seconds
stop_reason which rule stopped the run (see stopping rules)
optimum, error the known optimum and abs(f - optimum), or None
constraint_scale the scale of each constraint, or None
extra information from the algorithm, for example the restarts of IPOP-CMA-ES

The best solution is an Individual. It holds the objective value, the total constraint violation (0 when every constraint holds), the decoded solution with your variables, and its codes. Codes are the internal form of a solution: the algorithms search over numbers in [0, 1], and the problem decodes them into your variables.

import ypea

problem = ypea.benchmarks.problem("rastrigin", 10)
result = ypea.LSHADE(max_nfe=20_000).solve(problem, seed=1)
best = result.best
print(best.obj_value, best.violation, best.feasible)
print(best.solution.x.round(3))  # the decoded solution
print(best.position.shape)       # its codes in [0, 1]
4.456232796932979e-07 0.0 True
[ 0.  0.  0. -0. -0. -0.  0. -0. -0.  0.]
(10,)

Differs from Python: MATLAB draws its own random numbers, so the values differ from Python's.

problem = ypea.benchmarks.problem("rastrigin", 10);
result = ypea.LSHADE(max_nfe=20000).solve(problem, seed=1);
best = result.best;
fprintf("%g %g %d\n", best.obj_value, best.violation, best.feasible);
disp(round(best.solution.x, 3))  % the decoded solution
disp(size(best.position))        % its codes in [0, 1]
1.11762e-05 0 1
     0     0     0     0     0     0     0     0     0     0

     1    10

The values print as 0 and -0 because they are rounded to three decimals.

With the default feasibility rules, best_feasible is best once any feasible solution exists. With constraints="penalty", best may be infeasible, and best_feasible is the best solution that satisfies every constraint. summary() shows the best feasible value when best is infeasible.

All evaluations

solve(problem, keep_evaluations=True), or ask_tell with the same argument, keeps every evaluated solution in result.evaluations. It is off by default to save memory. Use it to study where the algorithm searched or to reuse the evaluated points. This example keeps all 20 000 evaluations, then finds the first feasible solution with a value below 1:

import numpy as np
import ypea

problem = ypea.benchmarks.problem("rastrigin", 10)
result = ypea.LSHADE(max_nfe=20_000).solve(problem, seed=1, keep_evaluations=True)
ev = result.evaluations
print(len(ev), ev.nfe[:3], ev.obj_values.shape, ev.positions.shape)

mask = ev.feasible & (ev.obj_values < 1)
good = ev[mask]  # filter like an array
order = np.argsort(good.nfe)
first = good[order][:1]  # sort by any field
print(first.nfe, first.obj_values.round(3))
20000 [1 2 3] (20000,) (20000, 10)
[18476] [0.947]

Differs from Python: ev.rows(idx) filters and sorts, where Python indexes ev[idx].

problem = ypea.benchmarks.problem("rastrigin", 10);
result = ypea.LSHADE(max_nfe=20000).solve(problem, seed=1, keep_evaluations=true);
ev = result.evaluations;
fprintf("%d evaluations\n", length(ev));
disp(ev.nfe(1:3)')
disp(size(ev.obj_values))
disp(size(ev.positions))

mask = ev.feasible & (ev.obj_values < 1);
good = ev.rows(mask);  % filter with a mask or indices
[~, order] = sort(good.nfe);
first = good.rows(order(1));  % sort by any field
fprintf("%d %.3f\n", first.nfe, first.obj_values);
20000 evaluations
     1     2     3

       20000           1

       20000          10

18273 0.862

The first line shows the number of rows, the first evaluation numbers and the shapes of the values and codes. The second shows that evaluation 18 476 was the first with a value below 1.

Field Shape Meaning
nfe (n,) the evaluation number of each row, 1, 2, ...
positions (n, code_size) the codes. Decode them with problem.decode(ev.positions)
obj_values (n,) or (n, m) the objective values (non-finite values are stored as the worst value)
violations (n,) the total constraint violations
feasible (n,) True where the violation is 0

History

result.history has one entry per iteration (iteration 0 is the initialization) and one per improvement of the best solution:

Field One value per
iteration, nfe, best_obj_value, best_violation, time iteration
trace_nfe, trace_obj_value, trace_violation improvement, with the exact evaluation count that found it
trace_feasible_nfe, trace_feasible_obj_value improvement of the best feasible solution

history.at_nfe(points) gives the best value found within each budget, which is useful to compare runs at the same cost:

import ypea

problem = ypea.benchmarks.problem("rastrigin", 10)
result = ypea.LSHADE(max_nfe=20_000).solve(problem, seed=1)

checkpoints = [100, 1_000, 10_000, 20_000]
values = result.history.at_nfe(checkpoints)
print(values.round(3))
[103.416  76.667  24.616   0.   ]

Differs from Python: MATLAB draws its own random numbers, so the values differ from Python's.

problem = ypea.benchmarks.problem("rastrigin", 10);
result = ypea.LSHADE(max_nfe=20000).solve(problem, seed=1);

checkpoints = [100, 1000, 10000, 20000];
values = result.history.at_nfe(checkpoints);
disp(round(values, 3))
  110.3690   60.2750   26.6470         0

Saving a result

result.to_dict() returns plain data ready for JSON. Values JSON cannot hold, such as inf, become strings. This example saves a result to result.json:

import json

import ypea

problem = ypea.benchmarks.problem("rastrigin", 10)
result = ypea.LSHADE(max_nfe=20_000).solve(problem, seed=1)
with open("result.json", "w") as f:
    json.dump(result.to_dict(), f)

Differs from Python: jsonencode writes the plain data of to_dict.

problem = ypea.benchmarks.problem("rastrigin", 10);
result = ypea.LSHADE(max_nfe=20000).solve(problem, seed=1);
file = fullfile(tempdir, "result.json");
fid = fopen(file, "w");
fprintf(fid, "%s", jsonencode(result.to_dict()));
fclose(fid);

Plots

ypea.plot needs matplotlib (pip install "ypea[plot]"). ypea.plot.convergence draws the best value of one or more results against the evaluations. This example compares DE and PSO on the Rastrigin function and saves the plot to convergence.png:

import ypea

problem = ypea.benchmarks.problem("rastrigin", 10)

algorithms = [
    ypea.DE(max_nfe=20_000),
    ypea.PSO(max_nfe=20_000),
]

results = []
for alg in algorithms:
    results.append(alg.solve(problem, seed=1))

ax = ypea.plot.convergence(*results, error=True)
ax.set_title("Rastrigin, 10 dimensions")
ax.figure.savefig("convergence.png", dpi=150)

Differs from Python: figures are saved with exportgraphics, here to tempdir.

problem = ypea.benchmarks.problem("rastrigin", 10);

algorithms = {
    ypea.DE(max_nfe=20000)
    ypea.PSO(max_nfe=20000)
    };

results = {};
for k = 1:numel(algorithms)
    results{end + 1} = algorithms{k}.solve(problem, seed=1); %#ok<SAGROW>
end

ax = ypea.plot.convergence(results{:}, error=true);
title(ax, "Rastrigin, 10 dimensions");
exportgraphics(ax.Parent, fullfile(tempdir, "convergence.png"), Resolution=150);

MATLAB figure

Option Meaning
x="nfe" or x="iter" evaluations (default) or iterations on the horizontal axis
error=True draw the distance to the known optimum
log a logarithmic vertical axis (default: when every value is positive)
labels one label per result (default: the algorithm names)
ax draw on existing matplotlib axes
Convergence of DE and PSO on Rastrigin
The plot made by the code above: the error of the best solution of DE and PSO against the evaluations, with seed 1.

Every plot function returns the matplotlib Axes. band, boxes and rank_bars draw the results of many runs, as in experiment reports. ypea.problems.plot_tour and ypea.problems.plot_routes draw tours and vehicle routes (see Test problems).