evolutionary_optimization.evolutionary_algorithm package
Submodules
evolutionary_optimization.evolutionary_algorithm.ea_data_model module
- class PerformancePlotting(fitness_over_time, phenotype_over_time, genotype_over_time)[source]
Bases:
object
Dataclass storing information about the algorithm’s state overtime.
- __init__(fitness_over_time, phenotype_over_time, genotype_over_time)
- fitness_over_time: List[Union[float, int]]
- genotype_over_time: List[Union[float, int, List[Union[float, int]]]]
- phenotype_over_time: List[Union[float, int]]
evolutionary_optimization.evolutionary_algorithm.ea_utils module
- class CreateGif2D(animation_data_x, animation_data_y, static_plot_data)[source]
Bases:
object
- __init__(animation_data_x, animation_data_y, static_plot_data)[source]
Initialise CreateGif2D class.
This class creates a gif of a two-dimensional phenotype.
- Parameters
animation_data_x (
ndarray
) – genotype value to be lapsed in the gif.animation_data_y (
ndarray
) – phenotype value to be lapsed in the gif.static_plot_data (
PlottingData
) – data to be used in the background of the gif e.g. the phenotype function.
evolutionary_optimization.evolutionary_algorithm.evolution module
- class Evolution(phenotype, fitness_function=<evolutionary_optimization.fitness_functions.implemented_fitness_functions.MaximizeFitnessFunction object>, number_of_individuals=100, number_of_generations=20, ratio_of_elite_individuals=0.1)[source]
Bases:
object
- __init__(phenotype, fitness_function=<evolutionary_optimization.fitness_functions.implemented_fitness_functions.MaximizeFitnessFunction object>, number_of_individuals=100, number_of_generations=20, ratio_of_elite_individuals=0.1)[source]
Initialise Evolution class.
The Evolution class performs evolutionary optimization of a function (a phenotype). It contains a population of individuals that are evaluated at every iteration (generation) of the algorithm.
- Parameters
phenotype (
AbstractPhenotype
) – a phenotype instance with the desired genotype.fitness_function (
AbstractFitnessFunction
) – desired fitness function from interface.number_of_individuals (
int
) – number of phenotype instances to be used within the population.number_of_generations (
int
) – number of times the algorithm will be run.ratio_of_elite_individuals (
float
) – proportion of best scoring phenotypes in population that will be kept to the next generation.
- create_gif(function_data)[source]
Create gif animation of the best phenotype/genotype pair over time.
- evolve()[source]
Perform evolutionary optimisation.
This function performs the evolutionary optimisation. Over number_of_generations it evaluates the population, updates the population (with crossover and/ or mutation as initialised). It then records the best fitness score at each generation.
- plot_phenotype_function_and_best_individuals(function_data)[source]
Plot phenotype function and best individual phenotype values.
- record_performance()[source]
In place addition of fitness score, phenotype and genotype values of the current best individual.
This function performs in place addition of the current best fitness score, phenotype value and genotype value to the performance_over_time attribute. You can visualise fitness over time using the plot_fitness_score_over_time function, and you can visualise phenotype and genotype values over time using plot_phenotype_function_and_best_individuals function.
evolutionary_optimization.evolutionary_algorithm.population module
- class Population(number_of_individuals, phenotype, ratio_of_elite_individuals)[source]
Bases:
object
- __init__(number_of_individuals, phenotype, ratio_of_elite_individuals)[source]
Create and store phenotypes used in the Evolution object.
The Population object is used by the Evolution object to create, store, evaluate and update phenotypes.
- Parameters
number_of_individuals (
int
) – number of phenotype instances i.e. individuals in the desired population.phenotype (
AbstractPhenotype
) – a phenotype instance with the desired genotype.ratio_of_elite_individuals (
float
) – proportion of best scoring phenotypes in population that will be kept to the next generation.
- create_list_of_new_individuals(n_new_individuals)[source]
Create a list of Individual instances.
- Parameters
n_new_individuals (
int
) – number of desired individuals in list.- Return type
List
[AbstractPhenotype
]- Returns
List of random Phenotype instances of length n_new_individuals.
- static crossover_for_population_segment(list_of_parents)[source]
Perform crossover for a list of phenotypes.
This method creates a new list of phenotypes (children) based on the parents’ genotypes by calling the phenotype’s crossover method.
- Parameters
list_of_parents (
List
[AbstractPhenotype
]) – list of phenotypes which should be used to generate offspring.- Return type
List
[AbstractPhenotype
]- Returns
List of new Phenotype objects created from their parents’ genotypes.
- evaluate_population(fitness_function)[source]
Find best_individual in population by calculating fitness scores for all individuals.
For each individual in the population calculates the fitness score and stores the best individual in the population.best_individual attribute.
- Parameters
fitness_function (
AbstractFitnessFunction
) – fitness function used to evaluate the phenotype.
- sort_phenotypes_by_fitness_score(fitness_function)[source]
Sort list of AbstractPhenotype by descending fitness score.
- Return type
List
[AbstractPhenotype
]
- split_elite_individuals(fitness_function)[source]
Split list of individuals into elite and non-elite individuals.
The function will create two lists to separate out elite individuals i.e. ones with the highest fitness scores, from those with lower fitness scores. Top 10% of individuals (or at least 1) will be kept from a list. The rest will be assigned to a separate list, which will be updated using crossover and/or mutation.
- Return type
Tuple
[List
[AbstractPhenotype
],List
[AbstractPhenotype
]]- Returns
- Tuple of List[Phenotype] and List[Phenotype] representing separate groups of
elite and non_elite individuals.
- update_population(fitness_function)[source]
Update population attribute following evaluation.
Once all individuals in the population have been evaluated, the top individuals are kept (elitism), the remaining individuals are updated by a combination of mutation and/or crossover if these were defined in the Evolution instance. If neither was selected, the non-elite individuals will be replaced by randomly generated individuals.
- Parameters
fitness_function (
AbstractFitnessFunction
) – fitness function used to evaluate the phenotype.
- get_score_for_sorting(phenotype_and_fitness_score_tuple)[source]
Key for sorted function used in split_elite_individuals object.
Provides a key for sorting individuals by returning the phenotype’s fitness score from a tuple of phenotype and fitness score. This is used with python’s sorted function.
- Parameters
phenotype_and_fitness_score_tuple (
Tuple
[AbstractPhenotype
,int
]) – tuple of phenotype instance and its fitness score.- Return type
Union
[float
,int
]- Returns
Float or int equal to a phenotype’s fitness score.