freneticlib.core.mutation.crossovers
Module Contents
Classes
Abstract parent of all crossover operators. |
|
Crossover operator that creates two new roads by randomly selecting roads from both parents. |
|
Crossover operator that creates two new roads by (roughly) splitting both roads in half, |
|
A container class for the crossover operators, selection strategy is implemented in |
|
A container class that randomly chooses between a list of crossover operators. |
Functions
|
Calculate the similarity of two roads. |
|
Combine the information of two roads, so we can trace where crossover offspring was generated from. |
- freneticlib.core.mutation.crossovers.calculate_test_similarity(parent_1, parent_2) float[source]
Calculate the similarity of two roads.
- Parameters:
parent_1 – First operand.
parent_2 – Second operand.
- Returns:
The similarity factor.
- Return type:
(float)
- freneticlib.core.mutation.crossovers.combine_parents_info(parent_1_info, parent_2_info) Dict[source]
Combine the information of two roads, so we can trace where crossover offspring was generated from.
- Parameters:
parent_1_info – First parent’s information-dict.
parent_2_info – Second parent’s information-dict.
- Returns:
The combined information of both parents.
- Return type:
(Dict)
- class freneticlib.core.mutation.crossovers.AbstractCrossoverOperator[source]
Bases:
abc.ABCAbstract parent of all crossover operators.
- abstract __call__(representation: freneticlib.representations.abstract_representation.RoadRepresentation, parent_1, parent_2)[source]
Create a new road by combining road points from two parents.
- Parameters:
representation (RoadRepresentation) – The road representation used in this search.
parent_1 – The first operand for the crossover.
parent_2 – The second operand for the crossover.
- Returns:
The offspring, i.e. combination of both parent roads.
- is_applicable(representation: freneticlib.representations.abstract_representation.RoadRepresentation, parent_1, parent_2) bool[source]
Check if test can be mutated.
- Parameters:
test – The road (in a given representation).
- Returns:
Whether the mutation operator is applicable.
- Return type:
(bool)
- class freneticlib.core.mutation.crossovers.ChromosomeCrossover[source]
Bases:
AbstractCrossoverOperatorCrossover operator that creates two new roads by randomly selecting roads from both parents.
- __call__(representation: freneticlib.representations.abstract_representation.RoadRepresentation, parent_1, parent_2) List[source]
Create a new road by combining road points from two parents.
- Parameters:
representation (RoadRepresentation) – The road representation used in this search.
parent_1 – The first operand for the crossover.
parent_2 – The second operand for the crossover.
- Returns:
The offspring, i.e. combination of both parent roads.
- class freneticlib.core.mutation.crossovers.SinglePointCrossover[source]
Bases:
AbstractCrossoverOperatorCrossover operator that creates two new roads by (roughly) splitting both roads in half, and combining each “head” with the respective other tail.
For example, given that
R1 = [A, B, C, D, E, F, G]andR2 = [1, 2, 3, 4, 5, 6, 7]then
R1 x R2yields, eg.C1 = [A, B, C, D, 5, 6, 7]andC2 = [1, 2, 3, 4, E, F, G]- __call__(representation: freneticlib.representations.abstract_representation.RoadRepresentation, parent_1, parent_2) List[source]
Create a new road by combining road points from two parents.
- Parameters:
representation (RoadRepresentation) – The road representation used in this search.
parent_1 – The first operand for the crossover.
parent_2 – The second operand for the crossover.
- Returns:
The offspring, i.e. combination of both parent roads.
- class freneticlib.core.mutation.crossovers.AbstractCrossover(operators: List[AbstractCrossoverOperator] = None)[source]
Bases:
abc.ABCA container class for the crossover operators, selection strategy is implemented in
__call__()- abstract __call__(representation: freneticlib.representations.abstract_representation.RoadRepresentation, parent_candidates: List) List[source]
Apply crossover to a list of parent candidates.
- Parameters:
representation (RoadRepresentation) – The road representation used in this search.
parent_candidates (List) – The parents that we take into consideration for producing offspring.
- Returns:
A list of offspring roads that were created by mating parent_candidates.
- Return type:
(List)
- is_applicable(parent_candidates: List) bool[source]
Check if it is possible to mate the parent_candidates. :param parent_candidates: The parents that we take into consideration for producing offspring. :type parent_candidates: List
- Returns:
If it is possible to mate those parents, or if there is an issue.
- Return type:
(bool)
- class freneticlib.core.mutation.crossovers.ChooseRandomCrossoverOperator(operators: List[AbstractCrossoverOperator] = None, size: int = 20, similarity_threshold: float = 0.95)[source]
Bases:
AbstractCrossoverA container class that randomly chooses between a list of crossover operators.
By default, the operator is chosen randomly from
ChromosomeCrossoverandSinglePointCrossover.- Parameters:
operators (List[AbstractCrossoverOperator]) – The list of crossover operators to choose from.
size (int) – Maximum limit of offspring to create (practically it will be it’s
min(size, len(parent_candidates))similarity_threshold (float) – Don’t create crossovers of parents whose similarity exceeds this threshold.
- __call__(representation: freneticlib.representations.abstract_representation.RoadRepresentation, parent_candidates: List) List[source]
- Parameters:
candidates (list) – A list of candidate tests to be chosen as parents
- Returns:
(list) A list of children of (almost) same size