1.3. Selection¶
Table of Contents
Simple functions for encapsulating performing population selection.
1.3.1. Custom functions¶
Custom selection functions can be used in islands.
def selection_random(individuals : list, n : int = 4, island=None) -> list:
return random.choice(individuals, size=n).tolist()
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
Important: Parent selection functions yield
lists of individuals instead of returning them.
def selection_parents_two(individuals : list, n : int = 4, island=None) -> list:
for parent_1, parent_2 in zip(individuals[::2], individuals[1::2]):
yield [parent_1, parent_2]
Survivor selection is similar to elite selection.
def selection_survivors_random(individuals : list, n : int = 4, island=None) -> list:
return random.choice(individuals, size=n).tolist()
1.3.2. Parent selection¶
1.3.2.1. Tournament selection¶
- natural_selection.genetic_algorithms.operators.selection.selection_tournament(individuals: list, n: int = 4, tournament_size: int = 5, with_replacement: bool = True, island=None) → list¶
Classic tournament selection. Given a number of selection rounds (n), select a random list of individuals of tournament_size and select the top individual from the random selection.
- Parameters
individuals (list) – A list of Individuals.
n (int) – The number of tournaments to run, effectively the number of selected individuals to return (default = 4).
tournament_size (int) – The number of random individuals to select during each tournament (default = 5).
with_replacement (bool) – Whether the sample is with or without replacement (default = True).
island (Island) – The Island calling the method (default = None).
- Returns
Top n Individuals from tournaments.
- Return type
list
- natural_selection.genetic_algorithms.operators.selection.selection_tournament_unique(individuals: list, n: int = 4, tournament_size: int = 5, with_replacement: bool = True, max_step: int = 100, island=None) → list¶
Classic tournament selection but ensures a unique list of selected individuals. Given a number of selection rounds (n), select a random list of individuals of tournament_size and select the top individual from the random selection.
- Parameters
individuals (list) – A list of Individuals.
n (int) – The number of tournaments to run, effectively the number of selected individuals to return (default = 4).
tournament_size (int) – The number of random individuals to select during each tournament (default = 5).
max_step (int) – In the unlikely event that a unique list of size n can not be achieved, break out of the loop after this amount of steps (default = 100).
with_replacement (bool) – Whether the sample is with or without replacement (default = True).
island (Island) – The Island calling the method (default = None).
- Returns
Top n Individuals from tournaments.
- Return type
list
1.3.2.2. Random selection¶
- natural_selection.genetic_algorithms.operators.selection.selection_random(individuals: list, n: int = 4, with_replacement: bool = True, island=None) → list¶
Completely random selection.
- Parameters
individuals (list) – A list of Individuals.
n (int) – Number to select (default = 4).
with_replacement (bool) – Whether the sample is with or without replacement (default = True).
island (Island) – The Island calling the method (default = None).
- Returns
Random n Individuals.
- Return type
list
1.3.2.3. Top N selection (elites)¶
- natural_selection.genetic_algorithms.operators.selection.selection_elites_top_n(individuals: list, n: int = 4, desc: bool = True, island=None) → list¶
A Classic top N selection function, sorted on fitness.
- Parameters
individuals (list) – A list of Individuals.
n (int) – Number to select (default = 4).
desc (bool) – In descending order, only used if Island is None, else maximise_function overrides (default = True).
island (Island) – The Island calling the method (default = None).
- Returns
Top n Individuals.
- Return type
list
1.3.2.4. Roulette selection¶
- natural_selection.genetic_algorithms.operators.selection.selection_roulette(individuals: list, n: int = 4, with_replacement: bool = True, island=None) → list¶
Classic roulette wheel selection, or also known as Fitness proportionate selection. This method selects individuals based on their fitness in proportion to the whole population.
Note
Can only be used for function maximisation.
- Parameters
individuals (list) – A list of Individuals.
n (int) – The number of selected individuals to return (default = 4).
with_replacement (bool) – Whether the sample is with or without replacement (default = True).
island (Island) – The Island calling the method (default = None).
- Raises
ValueError – If the island is set for function minimisation.
- Returns
n Individuals from roulette selection.
- Return type
list
- natural_selection.genetic_algorithms.operators.selection.selection_almost_roulette_minimisation(individuals: list, n: int = 4, with_replacement: bool = True, island=None) → list¶
Almost roulette wheel selection selection but for function minimisation. This method selects individuals based on their fitness in proportion to the whole population.
Note
Can only be used for function minimisation.
- Parameters
individuals (list) – A list of Individuals.
n (int) – The number of selected individuals to return (default = 4).
with_replacement (bool) – Whether the sample is with or without replacement (default = True).
island (Island) – The Island calling the method (default = None).
- Raises
ValueError – If the island is set for function maximisation.
- Returns
n Individuals from roulette selection.
- Return type
list
1.3.3. Parent combination¶
1.3.3.1. Two parents¶
- natural_selection.genetic_algorithms.operators.selection.selection_parents_two(individuals: list, island=None)¶
Simple function to select two parents at a time, sequentially. Parental selection always yields.
- Parameters
individuals (list) – A list of Individuals, specifically the selected “elites”.
island (Island) – The Island calling the method (default = None).
- Yields
list – Containing the two individuals selected for crossover.
1.3.3.2. Two parents shuffled¶
- natural_selection.genetic_algorithms.operators.selection.selection_parents_two_shuffled(individuals: list, island=None)¶
Simple function to select two parents at a time, randomly shuffled. Parental selection always yields.
- Parameters
individuals (list) – A list of Individuals, specifically the selected “elites”.
island (Island) – The Island calling the method (default = None).
- Yields
list – Containing the two individuals selected for crossover.
1.3.3.3. N-gram parents¶
- natural_selection.genetic_algorithms.operators.selection.selection_parents_n_gram(individuals: list, n: int = 2, island=None)¶
Simple function to select two parents at a time, sequentially, and yields n individuals at a time.
Yielded result is a n-gram generated result. The list
l = [a,b,c,d]
withn = 2
will yield[a,b]
,[b,c]
,[c,d]
.- Parameters
individuals (list) – A list of Individuals, specifically the selected “elites”.
n (int) – N size of grams (default = 2).
island (Island) – The Island calling the method (default = None).
- Yields
list – Containing n individuals selected for crossover.
1.3.4. Survivor selection¶
1.3.4.1. All (default)¶
- natural_selection.genetic_algorithms.operators.selection.selection_survivors_all(individuals: list, island=None) → list¶
Simply passes on the individuals as is.
- Parameters
individuals (list) – A list of Individuals.
island (Island) – The Island calling the method (default = None).
- Returns
All individuals.
- Return type
list
1.3.4.2. Random survivors¶
- natural_selection.genetic_algorithms.operators.selection.selection_survivors_random(individuals: list, n: int = 4, island=None) → list¶
Selects a list of random individuals to survive.
- Parameters
individuals (list) – A list of Individuals.
n (int) – Number of survivors (default = 4).
island (Island) – The Island calling the method (default = None).
- Returns
Surviving individuals.
- Return type
list