Genetic Algorithm¶
This module provides a collection of genetic algorithm implementations and genetic operators.
The genetic algorithms in this module can be used to solve optimization problems by iteratively applying selection, mutation, crossover and other operations to a population of candidate solutions.
The genetic operators provided in this module include:
Mutation: randomly changes the value of one or more genes in an individual.
Crossover (also known as recombination): combines two parent individuals to produce a new offspring individual.
Selection: chooses individuals from a population based on their fitness, which is typically defined by a problem-specific evaluation function.
This module provides a flexible framework for implementing genetic algorithms and allows you to customize the algorithm to your specific optimization problem.
- class pyevopt.genetic.algorithm.GeneticAlgorithm(selection: Selection, crossover: Crossover, mutation: Mutation, survival: Survival)¶
The GeneticAlgorithm class implements a generational genetic algorithm, which is a type of evolutionary algorithm that uses the principles of natural selection and genetics to solve optimization problems.
This algorithm iteratively applies selection, crossover (recombination), mutation, and fitness evaluation to a population of candidate solutions. The goal is to find the optimal solution by evolving the population over multiple generations.
Attributes¶
- selection: genetic.selection.Selection
Selection operator
- crossover: genetic.crossover.Crossover
Crossover operator
- mutation: genetic.mutation.Mutation
Mutation operator
- survival: genetic.survival.Survival
Survival operator
- next(population, objective)¶
Executes a single iteration of the genetic algorithm.
This method applies selection, crossover (recombination), and mutation to the input population, and then evaluates the fitness of each individual in the new population using the provided objective function. The resulting population is then returned as the output of this method.
Parameters¶
- population: Population
Represent the current generation of candidate solutions.
- objective: callable
A callable function that evaluates the quality of each individual in the population.
Returns¶
- pyevopt.base.Population:
The new population of candidate solutions after a single iteration of the genetic algorithm.
- dict:
A dictionary containing information about the current iteration, such as:
evaluations: The number of times the objective function was evaluated during this iteration.
Notes¶
This method is intended to be called repeatedly to iterate through the optimization process. - It is assumed that the objective function is deterministic, meaning it always returns the same output for a given input.
Operators¶
Selection Operators¶
- class pyevopt.genetic.selection.SelectionTournament(k: int = 3)¶
Tournament selection operator for evolutionary algorithms.
This class implements the tournament selection strategy, which selects the fittest individual from a random sample of individuals. The size of the tournament is specified by the user.
Attributes¶
- k: int
Nº of individuals participate in each tournament.
- class pyevopt.genetic.selection.SelectionRouletteWheel¶
Roulette wheel selection operator for evolutionary algorithms.
This class implements the roulette wheel selection strategy, which selects individuals based on their fitness. The fitter an individual is, the higher the probability of being selected.
Crossover Operators¶
- class pyevopt.genetic.crossover.KPointCrossover(k: int = 2, p: float = 1)¶
K-point crossover operator for evolutionary algorithms.
This class implements the k-point crossover strategy, which selects k points at random along the parent individuals and swaps the corresponding segments between them to produce child offspring. The number of points (k) can be set by the user.
Attributes¶
- k: int
The number of points to select for crossover.
- class pyevopt.genetic.crossover.UniformCrossover(p: float = 1)¶
Uniform crossover operator for evolutionary algorithms.
This class implements the uniform crossover strategy, which randomly selects genes from either parent individual to create a child offspring. The gene is selected with equal probability from each parent.
Mutation Operators¶
- class pyevopt.genetic.mutation.NormalMutation(mu: float = 0.0, sigma: float = 0.1, p: float = 0.01)¶
Normal distribution mutation operator.
This mutation operator adds a random value from a normal distribution to each gene in the individual. The mean and standard deviation of the distribution are used to determine the scale of the mutation.
Attributes¶
- mufloat
The mean of the normal distribution.
- sigmafloat
The standard deviation of the normal distribution.
- prob: float
Probability of a mutation occurring.
- class pyevopt.genetic.mutation.FlipMutation(p: float = 0.01)¶
Class to represent a Flip Mutation operator.
Survival Operators¶
- class pyevopt.genetic.survival.BasicSurvival¶
Class to represent a Basic Survival operator.