metaheuristic_designer.parent_selection.parent_selection_functions module#

Core parent selection functions (tournament, roulette, SUS, best, …) and fitness scaling helpers.

fitness_proportional(fitness, scaling_factor)[source]#

Fitness proportional scaling.

Shift fitness to be non-negative and add a constant offset.

Return type:

ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool]

Parameters:
fitnessVectorLike

Raw fitness values of the population.

scaling_factorScalarLike

Offset added after shifting to ensure all weights are positive.

Returns:
VectorLike

unnormalized selection weights.

Parameters:
  • fitness (ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool])

  • scaling_factor (number | float | int)

sigma_scaling(fitness, scaling_factor)[source]#

Sigma scaling: weight based on standard deviations above the mean.

Values below mean - scaling_factor * std are clamped to zero.

Return type:

ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool]

Parameters:
fitnessVectorLike

Raw fitness values.

scaling_factorScalarLike

Number of standard deviations below the mean to clamp.

Returns:
VectorLike

unnormalized selection weights.

Parameters:
  • fitness (ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool])

  • scaling_factor (number | float | int)

linear_ranking(fitness, scaling_factor)[source]#

Linear ranking: weight proportional to rank.

Rank 0 (worst) receives the smallest weight; rank N-1 (best) the largest. The scaling factor is clamped to at most 2.

Return type:

ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool]

Parameters:
fitnessVectorLike

Raw fitness values.

scaling_factorScalarLike

Selection pressure (clamped to ≤2). Lower values give more extreme emphasis on high ranks.

Returns:
VectorLike

unnormalized selection weights.

Parameters:
  • fitness (ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool])

  • scaling_factor (number | float | int)

exponential_ranking(fitness, scaling_factor)[source]#

Exponential ranking: weight decays exponentially with rank.

Return type:

ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool]

Parameters:
fitnessVectorLike

Raw fitness values.

scaling_factorScalarLike

Not used directly; included for interface consistency.

Returns:
VectorLike

unnormalized selection weights.

Parameters:
  • fitness (ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool])

  • scaling_factor (number | float | int)

flat_ranking(fitness, scaling_factor)[source]#

Flat ranking: every individual receives equal weight.

Return type:

ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool]

Parameters:
fitnessVectorLike

Raw fitness values.

scaling_factorScalarLike

Not used; included for interface consistency.

Returns:
VectorLike

unnormalized weights (all ones).

Parameters:
  • fitness (ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool])

  • scaling_factor (number | float | int)

create_scaling_fn(method, scaling_factor=2)[source]#

Create a callable that computes normalized selection weights.

Return type:

Callable

Parameters:
methodstr

Key into scaling_map (e.g., "fitness_proportional").

scaling_factorfloat, optional

Factor forwarded to the underlying scaling function.

Returns:
callable

A function (fitness) -> weights that returns a normalized probability vector.

Parameters:
  • method (str)

  • scaling_factor (number | float | int)

repeating_selection(fitness, amount, rng=None)[source]#

Chooses the entire population repeated in order duplicated enough times to reach the specified amount.

Return type:

ndarray[tuple[int, ...], integer] | ndarray[tuple[int, ...], uint8 | bool]

Parameters:
population: ndarray

List of individuals from which the parents will be replicated.

amount: int

Amount of individuals to be chosen as parents.

Returns:
parents: ndarray

List of individuals chosen as parents.

Parameters:
  • fitness (ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool])

  • amount (int)

  • rng (int | Generator | None)

select_best(fitness, amount, rng=None)[source]#

Selects the best parent of the population as parents.

Return type:

ndarray[tuple[int, ...], integer] | ndarray[tuple[int, ...], uint8 | bool]

Parameters:
population: ndarray

List of individuals from which the parents will be selected.

amount: int

Amount of individuals to be chosen as parents.

Returns:
parents: ndarray

List of individuals chosen as parents.

Parameters:
  • fitness (ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool])

  • amount (int)

  • rng (int | Generator | None)

prob_tournament(fitness, amount, rng=None, tournament_size=3, prob=1)[source]#

Selects the parents for the next generation by tournament.

Return type:

ndarray[tuple[int, ...], integer] | ndarray[tuple[int, ...], uint8 | bool]

Parameters:
population: ndarray

List of individuals from which the parents will be selected.

tournament_size: int

Amount of individuals that will be chosen for each tournament.

prob: float

Probability that a parent with low fitness will win the tournament.

Returns:
parents: ndarray

List of individuals chosen as parents.

Parameters:
  • fitness (ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool])

  • amount (int)

  • rng (int | Generator | None)

  • tournament_size (int)

  • prob (float)

uniform_selection(fitness, amount, rng=None)[source]#

Chooses a number of individuals from the population at random with replacement.

Return type:

ndarray[tuple[int, ...], integer] | ndarray[tuple[int, ...], uint8 | bool]

Parameters:
population: ndarray

List of individuals from which the parents will be selected.

amount: int

Amount of individuals to be chosen as parents.

Returns:
parents: ndarray

List of individuals chosen as parents.

Parameters:
  • fitness (ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool])

  • amount (int)

  • rng (int | Generator | None)

shuffle_population(fitness, amount, rng=None)[source]#

Chooses a number of individuals from the population at random without replacement if amount < population_size. If we cannot pick without replacement, we at least make sure we pick every individual at least \(\left\lceil \frac{\text{amount}}{\text{population\_size}} \right\rceil\) times

Return type:

ndarray[tuple[int, ...], integer] | ndarray[tuple[int, ...], uint8 | bool]

Parameters:
population: ndarray

List of individuals from which the parents will be selected.

amount: int

Amount of individuals to be chosen as parents.

Returns:
parents: ndarray

List of individuals chosen as parents.

Parameters:
  • fitness (ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool])

  • amount (int)

  • rng (int | Generator | None)

roulette(fitness, amount, rng=None, method='flat_scaling', scaling_factor=None)[source]#

Fitness proportionate parent selection.

Return type:

ndarray[tuple[int, ...], integer] | ndarray[tuple[int, ...], uint8 | bool]

Parameters:
population: ndarray

List of individuals from which the parents will be selected.

amount: int

Amount of individuals to be chosen as parents.

method: str, optional

Indicates how the roulette will be generated.

f: float, optional

Parameter passed to some of the roulette generating methods.

Returns:
parents: ndarray

List of individuals chosen as parents.

Parameters:
  • fitness (ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool])

  • amount (int)

  • rng (int | Generator | None)

  • method (str)

  • scaling_factor (float | None)

sus(fitness, amount, rng=None, method='flat_scaling', scaling_factor=None)[source]#

Stochastic universal sampling parent selection method.

Return type:

ndarray[tuple[int, ...], integer] | ndarray[tuple[int, ...], uint8 | bool]

Parameters:
population: ndarray

List of individuals from which the parents will be selected.

amount: int

Amount of individuals to be chosen as parents.

method: str, optional

Indicates how the roulette will be generated.

f: float, optional

Parameter passed to some of the roulette generating methods.

Returns:
parents: ndarray

List of individuals chosen as parents.

Parameters:
  • fitness (ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool])

  • amount (int)

  • rng (int | Generator | None)

  • method (str)

  • scaling_factor (float | None)