Selection operators

Submodules

pygenalgo.operators.selection.boltzmann_selector module

class pygenalgo.operators.selection.boltzmann_selector.BoltzmannSelector(select_probability: float = 1.0, k: float = 100.0)[source]

Bases: SelectionOperator

Description:

Boltzmann Selection integrates principles from physical systems into genetic algorithms, using a temperature parameter to influence selection probability. The basic idea is that as the “temperature” decreases, the likelihood of selecting higher-fitness individuals increases, mimicking the cooling process in simulated annealing. At high temperatures, the selection is more random, allowing exploration. As temperature falls, the process becomes more exploitative, favoring superior solutions. This duality makes Boltzmann Selection versatile, although it requires careful tuning of the temperature parameter to maintain a balance between exploration and exploitation over generations.

select(population: list[Chromosome]) list[Chromosome][source]

Select the individuals, from the input population, that will be passed on to the next genetic operations of crossover and mutation to form the new population of solutions.

NOTE: the Boltzmann constant is held in the ‘_items’ variable.

Parameters:

population – a list of chromosomes to select the parents from.

Returns:

the selected parents population (as list of chromosomes).

pygenalgo.operators.selection.linear_rank_selector module

class pygenalgo.operators.selection.linear_rank_selector.LinearRankSelector(select_probability: float = 1.0)[source]

Bases: SelectionOperator

Description:

The LinearRank Selector is a selection mechanism in genetic algorithms that ranks individuals based on their fitness values. It assigns selection probabilities to candidates in a linear fashion, ensuring that higher-ranking individuals are more likely to be chosen for reproduction. The operator helps maintain diversity in the population by avoiding premature convergence, allowing lower-ranked individuals a chance to contribute. This method is particularly effective in scenarios with varying fitness levels, as it balances selection pressure while fostering exploration of the solution space. Overall, it enhances the optimization process in evolutionary computation models.

static probabilities(p_size: int) list[float][source]

Calculate the rank probability distribution over the population size. The function is lru_cached so that repeated calls with the same input should not recompute the same array, since the population size of the chromosomes is not expected to change dynamically.

NOTE: Probabilities are returned in ascending order.

Parameters:

p_size – (int) population size.

Returns:

(list) rank probability distribution in ascending order.

select(population: list[Chromosome]) list[Chromosome][source]

Select the individuals, from the input population, that will be passed on to the next genetic operations of crossover and mutation to form the new population of solutions.

Parameters:

population – a list of chromosomes to select the parents from.

Returns:

the selected parents population (as list of chromosomes).

pygenalgo.operators.selection.random_selector module

class pygenalgo.operators.selection.random_selector.RandomSelector(select_probability: float = 1.0)[source]

Bases: SelectionOperator

Description:

Random Selection involves choosing individuals from a population without any regard to their fitness levels. Each member has an equal chance of being selected, which promotes genetic diversity by allowing exploration of various solutions. While this method is simple and easy to implement, it can lead to inefficient results, especially in populations with varying fitness values. Random selection may overlook high-quality solutions, making it less effective in later stages of evolution. However, it can serve as a useful mechanism in the initial stages or when diversity is paramount for avoiding premature convergence.

select(population: list[Chromosome]) list[Chromosome][source]

Select the individuals, from the input population, that will be passed on to the next genetic operations of crossover and mutation to form the new population of solutions.

Parameters:

population – a list of chromosomes to select the parents from.

Returns:

the selected parents population (as list of chromosomes).

pygenalgo.operators.selection.roulette_wheel_selector module

class pygenalgo.operators.selection.roulette_wheel_selector.RouletteWheelSelector(select_probability: float = 1.0)[source]

Bases: SelectionOperator

Description:

Roulette Wheel Selection employs a probabilistic mechanism where individuals are selected based on their fitness relative to the entire population. Each individual receives a slice of a wheel proportional to its fitness, similar to a casino roulette, where a random number determines the selected individual. This method encourages selection of fitter individuals while allowing lower-fitness individuals a chance to contribute. While effective, it can lead to premature convergence if the population’s fitness is skewed, often referred to as the “selection pressure.”

select(population: list[Chromosome]) list[Chromosome][source]

Select the individuals, from the input population, that will be passed on to the next genetic operations of crossover and mutation to form the new population of solutions.

Parameters:

population – a list of chromosomes to select the parents from.

Returns:

the selected parents population (as list of chromosomes).

pygenalgo.operators.selection.select_operator module

class pygenalgo.operators.selection.select_operator.SelectionOperator(selection_probability: float)[source]

Bases: GeneticOperator

Description:

Provides the base class (interface) for a Selection Operator. Note that even though the operator accepts a probability value, for the moment this operator is applied with 100% ‘probability’.

select(population: list[Chromosome]) list[Chromosome][source]

Abstract method that “reminds” the user that if they want to create a Selection Class that inherits from here they should implement a select method.

Parameters:

population – is a list, with the chromosomes, to select he parents for the next generation

Returns:

Nothing but raising an error.

pygenalgo.operators.selection.select_operator.ensure_positive_fitness(population: list[Chromosome]) list[float][source]

Ensures that the fitness value of each chromosome is a positive number.

This is useful because some of the selection methods require a positive fitness to operate. Also in minimization problems the fitness values are negated, therefore by using this transformation the methods that require positive values are guaranteed to work.

Parameters:

population – (list) of chromosomes.

Returns:

(list) of positive fitness values.

pygenalgo.operators.selection.stochastic_universal_selector module

class pygenalgo.operators.selection.stochastic_universal_selector.StochasticUniversalSelector(select_probability: float = 1.0)[source]

Bases: SelectionOperator

Description:

Stochastic Universal Selector is an extension of fitness proportionate selection (i.e. RouletteWheelSelection) which exhibits no bias and minimal spread. Where RWS chooses several solutions from the population by repeated random sampling, SUS uses a single random value to sample all the solutions by choosing them at evenly spaced intervals. This gives weaker members of the population (according to their fitness) a chance to be chosen.

select(population: list[Chromosome]) list[Chromosome][source]

Select the individuals, from the input population, that will be passed on to the next genetic operations of crossover and mutation to form the new population of solutions.

Parameters:

population – a list of chromosomes to select the parents from.

Returns:

the selected parents population (as list of chromosomes).

pygenalgo.operators.selection.tournament_selector module

class pygenalgo.operators.selection.tournament_selector.TournamentSelector(select_probability: float = 1.0, k: int = 5)[source]

Bases: SelectionOperator

Description:

Tournament Selection operates by randomly selecting a subset of individuals from the population and competing them against each other based on their fitness. The individual with the highest fitness in the mini-group is chosen for reproduction. This method provides a balance between selection pressure and diversity, as larger tournament sizes increase the likelihood of selecting fitter individuals, while smaller sizes maintain population diversity. The size of the tournament can be adjusted depending on the desired selection pressure. This technique is computationally efficient and effective, especially in dynamic environments or populations with diverse fitness landscapes.

select(population: list[Chromosome]) list[Chromosome][source]

Select the individuals, from the input population that will be passed on to the next genetic operations of crossover and mutation to form the new population of solutions.

Parameters:

population – a list of chromosomes to select the parents from.

Returns:

the selected parents population (as list of chromosomes).

pygenalgo.operators.selection.truncation_selector module

class pygenalgo.operators.selection.truncation_selector.TruncationSelector(select_probability: float = 1.0, p: float = 0.3)[source]

Bases: SelectionOperator

Description:

Truncation Selection is a straightforward method where only a predetermined percentage of the best individuals are selected based on fitness, while the rest are discarded. This ensures a consistent evolution of stronger solutions, accelerating convergence toward optimal fitness levels. While effective in honing in on high-quality individuals, truncation selection can create a loss of genetic diversity, risking premature convergence if the population becomes too homogeneous. The selection proportion can be adjusted to control the pressure, making this method adaptable for various scenarios but necessitating monitoring to retain essential diversity.

select(population: list[Chromosome]) list[Chromosome][source]

Select the individuals, from the input population, that will be passed on to the next genetic operations of crossover and mutation to form the new population of solutions.

Parameters:

population – a list of chromosomes to select the parents from.

Returns:

the selected parents population (as list of chromosomes).

pygenalgo.operators.selection.neighborhood_selector module

class pygenalgo.operators.selection.neighborhood_selector.NeighborhoodSelector(select_probability: float = 1.0, n_nearest: int = 5)[source]

Bases: SelectionOperator

Description:

Neighborhood Selection focuses on leveraging local relationships within the population to guide the selection process. Unlike global methods it emphasizes individuals that are closely related in the solution space. This operator selects candidates based on their proximity, fostering local exploration and potentially discovering niche solutions. Neighborhood Selection can lead to a more cohesive search through similar solutions, promoting the evolution of specialized characteristics. While it can enhance diversity within clusters of solutions, the method also risks missing out on globally optimal solutions if too localized, necessitating a balance between exploration and exploitation in broader landscapes.

select(population: list[Chromosome]) list[Chromosome][source]

Select the individuals, from the input population that will be passed on to the next genetic operations of crossover and mutation to form the new population of solutions.

Parameters:

population – a list of chromosomes to select the parents from.

Returns:

the selected parents population (as list of chromosomes).

pygenalgo.operators.selection.meta_selector module

class pygenalgo.operators.selection.meta_selector.MetaSelector(select_probability: float = 1.0)[source]

Bases: SelectionOperator

Description:

Meta-selector, selects the chromosomes for the new population by applying randomly the predefined selectors (one at a time), with equal probability.

NOTE: In the future the equal probabilities can be amended.

property all_counters: dict

Accessor (getter) of the application counter from all the internal selectors. This is mostly to verify that everything is working as expected.

Returns:

a dictionary with the counter calls for all selector methods.

reset_counter() None[source]

Sets ALL the counters to ‘zero’. We have to override the super().reset_counter() method, because we have to call explicitly the reset_counter on all the internal operators.

Returns:

None.

select(population: list[Chromosome]) list[Chromosome][source]

Select the individuals, from the input population, that will be passed on to the next genetic operations of crossover and mutation to form the new population of solutions.

Parameters:

population – a list of chromosomes to select the parents from.

Returns:

the selected parents population (as list of chromosomes).

Module contents