Callbacks¶
A callback is a function that solve calls after every iteration, with the algorithm as its argument. Use one to record progress, show it, or stop a run on your own rule. This example minimizes the sum of squares of 10 variables with DE for 50 iterations and stores the best value after each one:
import numpy as np
import ypea
problem = ypea.Problem(
ypea.Real("x", shape=10, lb=-5, ub=5),
lambda sol: np.sum(sol.x**2),
name="sphere",
)
values = []
def record(alg):
values.append(alg.best.obj_value)
result = ypea.DE(max_iter=50).solve(problem, seed=1, callbacks=[record])
print(len(values), values[-1] == result.best.obj_value)
50 True
Differs from Python: the callback is a nested function, so it can add to values; during a run alg.best holds the population form, so the value is alg.best.F(1).
problem = ypea.Problem( ...
vars=ypea.Real("x", shape=10, lb=-5, ub=5), ...
objective=@(sol) sum(sol.x.^2), ...
name="sphere");
[result, values] = solve_and_record(problem);
fprintf("%d %d\n", numel(values), values(end) == result.best.obj_value);
function [result, values] = solve_and_record(problem)
values = [];
result = ypea.DE(max_iter=50).solve(problem, seed=1, callbacks={@record});
function record(alg)
values(end + 1) = alg.best.F(1);
end
end
50 1
The list has one value per iteration, and the last equals the final best value.
A callback may read, but should not change, the state of the run:
| Attribute | Meaning |
|---|---|
alg.iter |
the current iteration |
alg.nfe |
evaluations so far |
alg.best |
the best solution so far, with best.obj_value, best.violation and its codes best.X[0] |
alg.pop |
the current population (pop.X codes, pop.F objective values, pop.V violations) |
alg.problem |
the problem, for example alg.problem.decode_one(alg.best.X[0]) |
These attributes, together with alg.front in multi-objective runs, alg.rng and alg.seed, are stable. Other attributes that an algorithm keeps during a run (for example sigma in CMA-ES or tau in the ant colony methods) are internal and may change between releases.
Stopping a run¶
A callback that returns True stops the run, with the stop reason "callback". Here the run stops as soon as the best value drops below 0.001:
import numpy as np
import ypea
problem = ypea.Problem(
ypea.Real("x", shape=10, lb=-5, ub=5),
lambda sol: np.sum(sol.x**2),
name="sphere",
)
def good_enough(alg):
return alg.best.obj_value < 1e-3
result = ypea.DE(max_iter=1000).solve(problem, seed=1, callbacks=[good_enough])
print(result.stop_reason, result.iterations)
callback 118
problem = ypea.Problem( ...
vars=ypea.Real("x", shape=10, lb=-5, ub=5), ...
objective=@(sol) sum(sol.x.^2), ...
name="sphere");
result = ypea.DE(max_iter=1000).solve(problem, seed=1, callbacks={@good_enough});
fprintf("%s %d\n", result.stop_reason, result.iterations);
function stop = good_enough(alg)
stop = alg.best.F(1) < 1e-3;
end
callback 118
For a fixed goal, the target setting does this without a callback.
Start and end¶
A callable object may also have on_start(alg) and on_end(alg, result) methods, called before the first iteration and after the last. Such a class can update a plot or write a log. ypea.callbacks.Print is one. It prints the best value and NFE (the number of function evaluations used), here every 10 iterations:
import numpy as np
import ypea
problem = ypea.Problem(
ypea.Real("x", shape=10, lb=-5, ub=5),
lambda sol: np.sum(sol.x**2),
name="sphere",
)
printer = ypea.callbacks.Print(every=10)
result = ypea.DE(max_iter=30).solve(problem, seed=1, callbacks=[printer])
Differential Evolution (DE) started on sphere
Iteration 10: Best Obj. Value = 10.9784, NFE = 1100
Iteration 20: Best Obj. Value = 5.83877, NFE = 2100
Iteration 30: Best Obj. Value = 2.09417, NFE = 3100
Finished: max_iter, best = 2.09417
Differs from Python: MATLAB draws its own random numbers, so the values differ from Python's.
problem = ypea.Problem( ...
vars=ypea.Real("x", shape=10, lb=-5, ub=5), ...
objective=@(sol) sum(sol.x.^2), ...
name="sphere");
printer = ypea.callbacks.Print(every=10);
result = ypea.DE(max_iter=30).solve(problem, seed=1, callbacks={printer});
Differential Evolution (DE) started on sphere
Iteration 10: Best Obj. Value = 13.8199, NFE = 1100
Iteration 20: Best Obj. Value = 4.56594, NFE = 2100
Iteration 30: Best Obj. Value = 1.00748, NFE = 3100
Finished: max_iter, best = 1.00748