Sequence¶
Sequence ¶
Sequence(stages, *, shares=None, pop_size=None, **kwargs)
Runs stages (algorithms) one after another, as one run.
All stages share the evaluations, the best solution, the trace and the budget, and the
run gives one Result. Each stage starts from the solutions the previous stage ended with,
passed as its x0: the best pop_size of its final population, or the best one when
the next stage takes a single solution or chooses its own population size. x0 of the
Sequence goes to the first stage.
A stage ends at its own max_iter, max_nfe (counted within the stage), max_time
or stop request (for example a hill climbing local optimum), or when its share of the
budget is used. The last stage takes the rest of the budget. A stage with no limit and no
share runs the default 100 iterations, except the last one. max_nfe, max_iter,
target and max_time of the Sequence are limits of the whole run. Iterations are
counted across the stages.
Parameters
stages: list of algorithms, run in this ordershares: None (default, each stage ends by its own limits) or one fraction of the Sequence'smax_nfeper stage, summing to 1, for example [0.8, 0.2]
Result.extra["stages"] lists each stage with the iterations and evaluations where it
started and ended, its stop reason, and its own extra results.
Example: ypea.Sequence([ypea.GA(), ypea.HillClimbing()], shares=[0.8, 0.2], max_nfe=20_000)
A typical use is a global method followed by a local one: a population method explores, and a local search or CMA-ES refines what it found.
Example¶
import ypea
problem = ypea.benchmarks.problem("rastrigin", 10)
stages = [
ypea.DE(),
ypea.CMAES(),
]
alg = ypea.Sequence(stages, shares=[0.7, 0.3], max_nfe=50_000)
result = alg.solve(problem, seed=1)
print(result.summary())
for stage in result.extra["stages"]:
print(stage["algorithm"], stage["start_nfe"], stage["end_nfe"], stage["stop_reason"])
Sequence on rastrigin_d10
best objective value: 4.9748
error: 4.9748
feasible: True (violation 0)
evaluations: 50000, iterations: 1849, time: 0.315 s, stopped by: max_nfe
DE 0 35000 max_nfe
CMA-ES 35000 50000 max_nfe
Differs from Python: MATLAB draws its own random numbers, so the best value differs from Python's. The stages go in braces.
problem = ypea.benchmarks.problem("rastrigin", 10);
stages = {
ypea.DE()
ypea.CMAES()
};
alg = ypea.Sequence(stages, shares=[0.7 0.3], max_nfe=50000);
result = alg.solve(problem, seed=1);
disp(result.summary())
ran = result.extra.stages;
for k = 1:numel(ran)
stage = ran(k);
fprintf("%s %d %d %s\n", stage.algorithm, stage.start_nfe, stage.end_nfe, stage.stop_reason);
end
Sequence on rastrigin_d10
best objective value: 14.9244
error: 14.9244
feasible: true (violation 0)
evaluations: 50000, iterations: 1849, time: 2.18 s, stopped by: max_nfe
DE 0 35000 max_nfe
CMA-ES 35000 50000 max_nfe