Replacement

evolib.operators.replacement.replace_generational(pop, offspring, max_age=0, fitness_maximization=False)[source]

Replace the population with offspring, preserving elites and optionally applying age-based filtering. Resulting population is sorted by fitness.

This function implements generational replacement with elitism and optional aging. The final population size will be at most pop.parent_pool_size.

Parameters:
  • pop (Pop) – The population object.

  • offspring (List[Indiv]) – Newly generated offspring.

  • max_age (int) – Maximum allowed individual age (0 = disabled).

  • fitness_maximization (bool) – If True, higher fitness is better.

Raises:

ValueError – On invalid configuration or population state.

Return type:

None

evolib.operators.replacement.replace_mu_comma_lambda(pop, offspring, fitness_maximization=False)[source]

(mu, lambda) replacement: ONLY offspring compete; keep best mu offspring, top num_elites parents are preserved. Parents do not compete and cannot survive solely due to elitism.

Precondition:
  • Fitness of offspring has been evaluated.

  • Parents may have fitness values, but they are not considered here.

Parameters:
  • pop (Pop) – Population object (current parents are in pop.indivs).

  • offspring (list[Indiv]) – Newly generated and evaluated offspring.

  • fitness_maximization (bool) – Whether fitness is to be maximized.

Return type:

None

evolib.operators.replacement.replace_mu_plus_lambda(pop, offspring, fitness_maximization=False)[source]

(μ + λ) replacement: parents and offspring compete together; keep best μ.

Precondition:
  • Fitness of both parents and offspring has been evaluated (same environmental context).

Effect:
  • No explicit ‘elitism’ branch needed; if a parent is truly elite, it simply ranks among the top μ and survives.

Parameters:
  • pop (Pop) – Population object (parents in pop.indivs).

  • offspring (List[Indiv]) – Newly generated offspring.

  • fitness_maximization (bool) – Whether fitness is to be maximized.

Return type:

None

evolib.operators.replacement.replace_random(pop, offspring)[source]

Replace random non-elite individuals in the population with new offspring.

Elites are preserved using pop.get_elites() and marked via is_elite = True.

Parameters:
  • pop (Pop) – The population object.

  • offspring (List[Indiv]) – New offspring individuals.

Raises:

ValueError – If offspring is empty or replacement is not possible.

Return type:

None

evolib.operators.replacement.replace_steady_state(pop, offspring, num_replace=0, fitness_maximization=False)[source]

Replace the worst individuals in the population with offspring, preserving elite individuals. Implements steady-state replacement.

Parameters:
  • pop (Pop) – Current population.

  • offspring (List[Indiv]) – New individuals to insert.

  • num_replace (int) – Number of individuals to replace. If 0, replaces len(offspring).

  • fitness_maximization (bool) – Whether higher fitness is better.

Raises:

ValueError – If replacement configuration is invalid.

Return type:

None

evolib.operators.replacement.replace_truncation(pop, pool, fitness_maximization=False)[source]

Generic truncation replacement selecting the top μ individuals from a given pool. Pass a pool that already contains what should compete (e.g., parents+offspring for μ+λ, or offspring only for μ,λ).

Parameters:
  • pop (Pop) – Population handle (μ taken from pop.parent_pool_size).

  • pool (List[Indiv]) – Candidate list to pick survivors from.

  • fitness_maximization (bool) – If True, higher fitness is better.

Return type:

None

evolib.operators.replacement.replace_weighted_stochastic(pop, offspring, temperature=1.0, fitness_maximization=False)[source]

Replace individuals in the population using inverse-fitness-weighted softmax sampling, preserving elite individuals.

Parameters:
  • pop (Pop) – The population object.

  • offspring (List[Indiv]) – List of new individuals.

  • temperature (float) – Softmax temperature (> 0).

  • fitness_maximization (bool) – Whether higher fitness is better.

Raises:

ValueError – On invalid input or if not enough non-elites are available.

Return type:

None