freneticlib.core.mutation.crossovers

Module Contents

Classes

AbstractCrossoverOperator

Abstract parent of all crossover operators.

ChromosomeCrossover

Crossover operator that creates two new roads by randomly selecting roads from both parents.

SinglePointCrossover

Crossover operator that creates two new roads by (roughly) splitting both roads in half,

AbstractCrossover

A container class for the crossover operators, selection strategy is implemented in __call__()

ChooseRandomCrossoverOperator

A container class that randomly chooses between a list of crossover operators.

Functions

calculate_test_similarity(→ float)

Calculate the similarity of two roads.

combine_parents_info(→ Dict)

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.ABC

Abstract 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.

__str__()[source]

Return str(self).

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: AbstractCrossoverOperator

Crossover 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: AbstractCrossoverOperator

Crossover 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] and R2 = [1, 2, 3, 4, 5, 6, 7]

then R1 x R2 yields, eg. C1 = [A, B, C, D, 5, 6, 7] and C2 = [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.ABC

A 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: AbstractCrossover

A container class that randomly chooses between a list of crossover operators.

By default, the operator is chosen randomly from ChromosomeCrossover and SinglePointCrossover.

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

is_applicable(parent_candidates: list) bool[source]

Only apply crossover if there are enough parent_candidates available.

Parameters:

parent_candidates – The parent_candidates to check.

Returns:

Evaluates len(parent_candidates) >= self.min_number_candidates_for_crossover

Return type:

(bool)