1.2. Crossover operations¶
Table of Contents
Simple functions for performing crossover operations.
1.2.1. Custom crossover¶
Crossover typically involves two parents and a crossover strength. Custom functions can be written and added to the Island.
All crossover functions have to take as input at least a list of individuals and the island, even if the island isn’t used. Custom functions also have to return a list of individuals, and all custom parameters should have default values defined.
def crossover_custom(individuals : list, min_fitness : float = 0.8,
max_attempts : int = 3, island = None):
import copy
assert len(individuals) > 1, "Not enough individuals given!"
mother = individuals[0]
father = individuals[1]
other = island.create_individual(mother.fitness_function, chromosome=copy.deepcopy(mother.chromosome))
attempts = 0
while other.evaluate(island.function_params) < min_fitness and attempts < max_attempts:
for i in len(mother.chromosome):
mate = random.choice([mother, father], size=1)[0]
other.chromosome[i] = mate.chromosome[i]
attempts += 1
return [other]
Note the following in the above example:
The function takes a list of individuals
The function returns a list of individuals
The functions takes the island as a param
The custom parameters have default values defined
All attributes and methods of the Island object are accessible
1.2.2. Crossover operators¶
1.2.2.1. Uniform Random¶
- natural_selection.genetic_algorithms.operators.crossover.crossover_two_uniform(individuals: list, prob: float = 0.5, island=None) → list¶
A Classic crossover function, taking a list of 2 individuals and swapping positional genes based on the prob strength of crossover. Returns these two individuals with modified genomes.
- Parameters
individuals (list) – A list (length of 2) of Individuals to perform crossover.
prob (float) – The probability of swapping genes.
island (Island) – The Island calling the method (default = None).
- Returns
Two new Individuals.
- Return type
list
1.2.2.2. Random Point¶
- natural_selection.genetic_algorithms.operators.crossover.crossover_two_one_point(individuals: list, island=None) → list¶
Classic One Point crossover.
- Parameters
individuals (list) – A list (length of 2) of Individuals to perform crossover.
island (Island) – The Island calling the method (default = None).
- Returns
Two new Individuals.
- Return type
list
- natural_selection.genetic_algorithms.operators.crossover.crossover_two_two_point(individuals: list, island=None) → list¶
Classic Two Point crossover.
- Parameters
individuals (list) – A list (length of 2) of Individuals to perform crossover.
island (Island) – The Island calling the method (default = None).
- Returns
Two new Individuals.
- Return type
list
- natural_selection.genetic_algorithms.operators.crossover.crossover_two_n_point(individuals: list, n_points: int = 1, prob: float = 0.5, island=None) → list¶
Classic crossover method to randomly select N points for crossover.
- Parameters
individuals (list) – A list (length of 2) of Individuals to perform crossover.
n_points (int) – The amount of random points to split at (default = 1).
prob (float) – The probability of swapping genes.
island (Island) – The Island calling the method (default = None).
- Returns
Two new Individuals.
- Return type
list
1.2.2.3. Miscellaneous¶
- natural_selection.genetic_algorithms.operators.crossover.crossover_one_binary_union(individuals: list, inherit_from_mother: bool = True, island=None) → list¶
Crossover of two binary string chromosomes, producing one offspring where the chromosome is the union the two parents. If two parents have the following chromosomes [0, 0, 1] and [1, 0, 1], the child chromosome is [1, 0, 1]. The gene value must be of type
bool
. The island must be supplied to create offspring.Note
See this paper for more, Nagae, Satsuki, Shin Kawai, and Hajime Nobuhara. “Transfer learning layer selection using genetic algorithm.” 2020 IEEE Congress on Evolutionary Computation (CEC). IEEE, 2020.
- Raises
AssertionError – If the gene value is not of type
bool
.- Parameters
individuals (list) – A list (length of 2) of Individuals to perform crossover.
inherit_from_mother (bool) – Whether the offspring inherits all properties from the first or the second parent (default = True).
island (Island) – The Island calling the method (default = None).
- Returns
One new Individual.
- Return type
list