Base classes¶
Every algorithm derives from EvolutionaryAlgorithm through one of two families.
The methods documented here make up the public interface of an algorithm: init, ask
and tell, the default_params property, and the accessors get_mean or
get_best_solution and get_population. The individual algorithm pages list the
constructor arguments.
EvolutionaryAlgorithm ¶
EvolutionaryAlgorithm(population_size: int, solution: Solution, fitness_shaping_fn: Callable = identity_fitness_shaping_fn, metrics_fn: Callable = metrics_fn)
Base class for evolutionary algorithms.
Every algorithm follows the same ask-eval-tell loop: init creates the state,
ask proposes a population of candidate solutions, the user evaluates them,
and tell updates the state from the resulting fitness values. init, ask
and tell are pure, jit-compiled functions of an explicit state and
params, so they compose with jax.vmap and jax.lax.scan.
Fitness is minimized. To maximize an objective, negate it before calling
tell.
Subclasses implement _init, _ask, _tell and _default_params.
Parameters:
-
population_size(int) –Number of candidate solutions sampled and evaluated per generation.
-
solution(Solution) –Example solution PyTree. Only its structure and leaf shapes are used: they set the number of search dimensions and how flat search vectors are unravelled into the PyTrees returned by
ask. The one exception are the restart controllers inevosax.core.restart, which restart from the values ofsolutionunlesscopy_mean=True. -
fitness_shaping_fn(Callable, default:identity_fitness_shaping_fn) –Function
(population, fitness, state, params) -> shaped_fitnessapplied to the raw fitness intellbefore the algorithm update. Seeevosax.core.fitness_shaping. -
metrics_fn(Callable, default:metrics_fn) –Function
(key, population, fitness, state, params) -> metricsevaluated intell; its output is returned alongside the new state.
default_params
property
¶
Default hyperparameters of the algorithm.
Returns a Params dataclass. Use params.replace(...) to override
individual hyperparameters before passing it to init, ask and tell.
init ¶
Initialize the algorithm state.
Parameters:
-
key(Array) –PRNG key.
-
params(Any) –Algorithm hyperparameters, e.g.
default_params.
Returns:
-
Any–The initial algorithm state.
ask ¶
Ask for a population of candidate solutions to evaluate.
Parameters:
-
key(Array) –PRNG key.
-
state(Any) –Current algorithm state.
-
params(Any) –Algorithm hyperparameters.
Returns:
-
tuple[Population, Any]–A tuple
(population, state).populationis a PyTree with the structure ofsolutionand a leading axis of sizepopulation_size.
tell ¶
tell(key: Array, population: Population, fitness: Fitness, state: Any, params: Any) -> tuple[Any, Metrics]
Update the algorithm state with the fitness of a population.
Parameters:
-
key(Array) –PRNG key.
-
population(Population) –Population returned by
ask. -
fitness(Fitness) –Fitness of each population member, shape
(population_size,). Lower is better. NaN values mark failed evaluations and never become the best solution. -
state(Any) –Algorithm state returned by
ask. -
params(Any) –Algorithm hyperparameters.
Returns:
-
tuple[Any, Metrics]–A tuple
(state, metrics)with the updated state and the output ofmetrics_fn.
DistributionBasedAlgorithm ¶
DistributionBasedAlgorithm(population_size: int, solution: Solution, fitness_shaping_fn: Callable = identity_fitness_shaping_fn, metrics_fn: Callable = metrics_fn)
Bases: EvolutionaryAlgorithm
Base class for distribution-based algorithms.
A distribution-based algorithm maintains a search distribution, typically a
Gaussian with mean state.mean. ask samples a population from it and
tell updates it from the fitness of that population. The state is
initialized from an initial mean.
Parameters:
-
population_size(int) –Number of candidate solutions sampled and evaluated per generation.
-
solution(Solution) –Example solution PyTree. Only its structure and leaf shapes are used: they set the number of search dimensions and how flat search vectors are unravelled into the PyTrees returned by
ask. -
fitness_shaping_fn(Callable, default:identity_fitness_shaping_fn) –Function
(population, fitness, state, params) -> shaped_fitnessapplied to the raw fitness intellbefore the algorithm update. Seeevosax.core.fitness_shaping. -
metrics_fn(Callable, default:metrics_fn) –Function
(key, population, fitness, state, params) -> metricsevaluated intell; its output is returned alongside the new state.
init ¶
Initialize the algorithm state from an initial mean.
Parameters:
-
key(Array) –PRNG key.
-
mean(Solution) –Initial mean of the search distribution, a PyTree with the structure of
solution. -
params(Params) –Algorithm hyperparameters, e.g.
default_params.
Returns:
-
State–The initial algorithm state.
get_mean ¶
Return the mean of the search distribution as a solution PyTree.
PopulationBasedAlgorithm ¶
PopulationBasedAlgorithm(population_size: int, solution: Solution, fitness_shaping_fn: Callable = identity_fitness_shaping_fn, metrics_fn: Callable = metrics_fn)
Bases: EvolutionaryAlgorithm
Base class for population-based algorithms.
A population-based algorithm stores a population and its fitness in the
state. ask derives new candidates from the stored population and tell
updates the stored population from their fitness. The state is initialized
from an already evaluated initial population.
Parameters:
-
population_size(int) –Number of candidate solutions sampled and evaluated per generation.
-
solution(Solution) –Example solution PyTree. Only its structure and leaf shapes are used: they set the number of search dimensions and how flat search vectors are unravelled into the PyTrees returned by
ask. -
fitness_shaping_fn(Callable, default:identity_fitness_shaping_fn) –Function
(population, fitness, state, params) -> shaped_fitnessapplied to the raw fitness intellbefore the algorithm update. Seeevosax.core.fitness_shaping. -
metrics_fn(Callable, default:metrics_fn) –Function
(key, population, fitness, state, params) -> metricsevaluated intell; its output is returned alongside the new state.
init ¶
Initialize the algorithm state from an evaluated population.
Parameters:
-
key(Array) –PRNG key.
-
population(Population) –Initial population, a PyTree with the structure of
solutionand a leading population axis on every leaf. -
fitness(Fitness) –Raw fitness of the initial population. Lower is better. NaN values mark failed evaluations and never become the best solution.
-
params(Params) –Algorithm hyperparameters, e.g.
default_params.
Returns:
-
State–The initial algorithm state.
get_best_solution ¶
Return the best member of the stored population as a solution PyTree.
This is the member with the lowest shaped fitness in the current
population; the best solution seen over the whole run is kept in
state.best_solution. Unlike state.best_solution, this does not skip
NaN fitness values.
get_population ¶
Return the stored population as a PyTree with a leading population axis.
SV_ES ¶
SV_ES(population_size: int, num_populations: int, solution: Solution, kernel: Callable = kernel_rbf, fitness_shaping_fn: Callable = identity_fitness_shaping_fn, metrics_fn: Callable = metrics_fn)
Bases: DistributionBasedAlgorithm
Base class for Stein Variational Evolution Strategies.
Runs num_populations search distributions in parallel and couples their
updates through a kernel between their means. Every state field carries a
leading axis of size num_populations, ask returns
num_populations * population_size candidates, and tell expects one
fitness value for each of them.
Parameters:
-
population_size(int) –Number of candidate solutions sampled per search distribution in each generation.
-
num_populations(int) –Number of search distributions run in parallel.
-
solution(Solution) –Example solution PyTree. Only its structure and leaf shapes are used: they set the number of search dimensions and how flat search vectors are unravelled into the PyTrees returned by
ask. -
kernel(Callable, default:kernel_rbf) –Function
(x, y, params) -> similaritybetween the means of two search distributions. Seeevosax.core.kernel. -
fitness_shaping_fn(Callable, default:identity_fitness_shaping_fn) –Function
(population, fitness, state, params) -> shaped_fitnessapplied per search distribution to the raw fitness intellbefore the algorithm update. Seeevosax.core.fitness_shaping. -
metrics_fn(Callable, default:metrics_fn) –Function
(key, population, fitness, state, params) -> metricsevaluated per search distribution intell; its output is returned alongside the new state.
init ¶
Initialize the algorithm state from one initial mean per distribution.
Parameters:
-
key(Array) –PRNG key.
-
means(Solution) –Initial means, a PyTree with the structure of
solutionand a leading axis of sizenum_populationson every leaf. -
params(Params) –Algorithm hyperparameters, e.g.
default_params.
Returns:
-
State–The initial algorithm state.
ask ¶
Ask for a population of candidate solutions to evaluate.
Parameters:
-
key(Array) –PRNG key.
-
state(State) –Current algorithm state.
-
params(Params) –Algorithm hyperparameters.
Returns:
-
tuple[Population, State]–A tuple
(population, state).populationis a PyTree with the structure ofsolutionand a leading axis of sizenum_populations * population_size.
tell ¶
tell(key: Array, population: Population, fitness: Fitness, state: State, params: Params) -> tuple[State, Metrics]
Update the algorithm state with the fitness of a population.
Parameters:
-
key(Array) –PRNG key.
-
population(Population) –Population returned by
ask. -
fitness(Fitness) –Fitness of each population member, shape
(num_populations * population_size,). Lower is better. -
state(State) –Algorithm state returned by
ask. -
params(Params) –Algorithm hyperparameters.
Returns:
-
tuple[State, Metrics]–A tuple
(state, metrics)with the updated state and the output ofmetrics_fnfor each search distribution.
get_mean ¶
Return the means of all search distributions as a PyTree.
Every leaf has a leading axis of size num_populations.
State and metrics¶
State ¶
Bases: State
State shared by all evolutionary algorithms.
Attributes:
-
best_solution(Solution) –Flat vector of the best solution evaluated so far.
-
best_fitness(float) –Fitness of
best_solution(lower is better). -
generation_counter(int) –Number of completed
tellcalls.
metrics_fn ¶
metrics_fn(key: Array, population: Population, fitness: Fitness, state: State, params: Params) -> Metrics
Compute the default metrics returned by tell.
Parameters:
-
key(Array) –PRNG key (unused by the default implementation).
-
population(Population) –Flat population of shape
(population_size, num_dims). -
fitness(Fitness) –Raw fitness of shape
(population_size,). -
state(State) –Algorithm state, with the best solution already updated for this generation.
-
params(Params) –Algorithm hyperparameters.
Returns:
-
Metrics–Dictionary with the generation counter, the best fitness and solution of this generation, and the best fitness, solution and solution norm so far. The entries for this generation are taken with a plain
argminover the raw fitness, so they are NaN if the population contains a NaN fitness; the entries for the best so far ignore NaN values.