Skip to content

Fitness shaping

Fitness shaping functions for evolutionary algorithms.

This module provides various fitness shaping functions that can be used to transform raw fitness values. Fitness shaping can help improve the convergence properties of evolutionary algorithms by normalizing or standardizing fitness values, or by adding regularization terms like weight decay.

Every fitness shaping function has the signature (population, fitness, state, params) -> shaped_fitness, where population is the flat population of shape (population_size, num_dims), fitness the raw fitness of shape (population_size,), and state and params the state and hyperparameters of the algorithm. Pass one as fitness_shaping_fn when constructing an algorithm. Lower shaped fitness is better, except for weights_fitness_shaping_fn, which returns recombination weights where the best member receives the largest weight.

WeightedFitnessParams

Bases: Protocol

Parameters required for weighted fitness shaping.

normalize

normalize(a: Array, axis: int = -1, minval: float = -1.0, maxval: float = 1.0) -> jax.Array

Rescale an array linearly to the range [minval, maxval].

NaN values are ignored when computing the minimum and maximum. If all values are equal, the midpoint of the range is returned.

Parameters:

  • a (Array) –

    Array to normalize.

  • axis (int, default: -1 ) –

    Axis along which to normalize.

  • minval (float, default: -1.0 ) –

    Lower bound of the output range.

  • maxval (float, default: 1.0 ) –

    Upper bound of the output range.

Returns:

  • Array –

    The rescaled array, with the same shape as a.

add_weight_decay

add_weight_decay(fitness_shaping_fn, weight_decay=0.001)

Add weight decay to any fitness shaping function.

The returned function adds the L2 penalty weight_decay * sum(population**2, axis=-1) to the raw fitness and then applies fitness_shaping_fn.

Parameters:

  • fitness_shaping_fn (Callable) –

    Fitness shaping function to wrap.

  • weight_decay (float, default: 0.001 ) –

    Coefficient of the L2 penalty.

Returns:

  • wrapped_fitness_fn ( Callable ) –

    A fitness shaping function with the same signature.

identity_fitness_shaping_fn

identity_fitness_shaping_fn(population: Population, fitness: Array, state: State, params: Params) -> Fitness

Return the raw fitness unchanged.

standardize_fitness_shaping_fn

standardize_fitness_shaping_fn(population: Population, fitness: Array, state: State, params: Params) -> Fitness

Return fitness standardized to zero mean and unit variance.

NaN values are excluded from the mean and variance.

normalize_fitness_shaping_fn

normalize_fitness_shaping_fn(population: Population, fitness: Fitness, state: State, params: Params) -> Fitness

Return fitness rescaled linearly to [-1, 1].

NaN values are ignored when computing the range. Constant fitness maps to 0.

centered_rank_fitness_shaping_fn

centered_rank_fitness_shaping_fn(population: Population, fitness: Fitness, state: State, params: Params) -> Fitness

Return centered ranks in [-0.5, 0.5] according to fitness.

The member with the lowest fitness receives -0.5 and the one with the highest fitness 0.5. NaN values are ranked as the worst.

weights_fitness_shaping_fn

weights_fitness_shaping_fn(population: Population, fitness: Fitness, state: State, params: WeightedFitnessParams) -> Fitness

Return params.weights indexed by the fitness rank of each member.

The member with the lowest fitness receives params.weights[0]. NaN values are ranked as the worst; if every fitness value is NaN, all weights are zero. Requires hyperparameters with a weights field, as used by e.g. CMA-ES.