eckity.genetic_operators.crossovers.vector_k_point_crossover
1from random import sample 2 3from eckity.genetic_operators.genetic_operator import GeneticOperator 4from eckity.genetic_encodings.ga import Vector 5 6from typing import List, Tuple 7 8 9class VectorKPointsCrossover(GeneticOperator): 10 def __init__(self, probability=1, arity=2, k=1, events=None): 11 """ 12 Vector K Point Crossover. 13 14 Randomly chooses K points in the vector and swaps the parts 15 of two vectors at these points. 16 17 Parameters 18 ---------- 19 probability : float 20 The probability of the mutation operator to be applied 21 22 arity : int 23 The number of individuals this mutation is applied on 24 25 k : int 26 Number of points to cut the vector for the crossover. 27 28 events: list of strings 29 Custom events to be published by the mutation, by default None 30 """ 31 self.individuals = None 32 self.applied_individuals = None 33 self.k = k 34 super().__init__(probability=probability, arity=arity, events=events) 35 36 def apply(self, individuals: List[Vector]) -> List[Vector]: 37 """ 38 Attempt to perform the mutation operator 39 40 Parameters 41 ---------- 42 individuals : List[Vector] 43 individuals to perform crossover on 44 45 Returns 46 ---------- 47 List[Vector] 48 individuals after the crossover 49 """ 50 self.individuals = individuals 51 xo_points = sorted(sample(range(1, individuals[0].size()), self.k)) 52 self._swap_vector_parts( 53 individuals[0].vector, individuals[1].vector, xo_points 54 ) 55 56 self.applied_individuals = individuals 57 return individuals 58 59 def _swap_vector_parts( 60 self, vector1: List[int], vector2: List[int], xo_points: List[int] 61 ) -> Tuple[List[int]]: 62 """ 63 Swap parts of two vectors at the given crossover points. 64 65 Parameters 66 ---------- 67 vector1 : List[int] 68 first vector encoding 69 vector2 : List[int] 70 second vector encoding 71 xo_points : List[int] 72 crossover points 73 74 Returns 75 ------- 76 Tuple[List[int]] 77 _description_ 78 """ 79 if len(xo_points) == self.k: 80 xo_points.append(len(vector1)) 81 82 start_idx = 0 83 for i in range(0, len(xo_points), 2): 84 end_idx = xo_points[i] 85 replaced_part = vector1[start_idx:end_idx] 86 vector1[start_idx:end_idx] = vector2[start_idx:end_idx] 87 vector2[start_idx:end_idx] = replaced_part 88 start_idx = xo_points[i + 1] if end_idx < len(xo_points) else -1
10class VectorKPointsCrossover(GeneticOperator): 11 def __init__(self, probability=1, arity=2, k=1, events=None): 12 """ 13 Vector K Point Crossover. 14 15 Randomly chooses K points in the vector and swaps the parts 16 of two vectors at these points. 17 18 Parameters 19 ---------- 20 probability : float 21 The probability of the mutation operator to be applied 22 23 arity : int 24 The number of individuals this mutation is applied on 25 26 k : int 27 Number of points to cut the vector for the crossover. 28 29 events: list of strings 30 Custom events to be published by the mutation, by default None 31 """ 32 self.individuals = None 33 self.applied_individuals = None 34 self.k = k 35 super().__init__(probability=probability, arity=arity, events=events) 36 37 def apply(self, individuals: List[Vector]) -> List[Vector]: 38 """ 39 Attempt to perform the mutation operator 40 41 Parameters 42 ---------- 43 individuals : List[Vector] 44 individuals to perform crossover on 45 46 Returns 47 ---------- 48 List[Vector] 49 individuals after the crossover 50 """ 51 self.individuals = individuals 52 xo_points = sorted(sample(range(1, individuals[0].size()), self.k)) 53 self._swap_vector_parts( 54 individuals[0].vector, individuals[1].vector, xo_points 55 ) 56 57 self.applied_individuals = individuals 58 return individuals 59 60 def _swap_vector_parts( 61 self, vector1: List[int], vector2: List[int], xo_points: List[int] 62 ) -> Tuple[List[int]]: 63 """ 64 Swap parts of two vectors at the given crossover points. 65 66 Parameters 67 ---------- 68 vector1 : List[int] 69 first vector encoding 70 vector2 : List[int] 71 second vector encoding 72 xo_points : List[int] 73 crossover points 74 75 Returns 76 ------- 77 Tuple[List[int]] 78 _description_ 79 """ 80 if len(xo_points) == self.k: 81 xo_points.append(len(vector1)) 82 83 start_idx = 0 84 for i in range(0, len(xo_points), 2): 85 end_idx = xo_points[i] 86 replaced_part = vector1[start_idx:end_idx] 87 vector1[start_idx:end_idx] = vector2[start_idx:end_idx] 88 vector2[start_idx:end_idx] = replaced_part 89 start_idx = xo_points[i + 1] if end_idx < len(xo_points) else -1
Abstract class for genetic operators. Each operator has a probability of being applied each generation.
Genetic operators are applied in-place.
Parameters
- probability (float, optional): probability of being applied each generation, by default 1.0
- arity (int, optional): number of individuals required for the operator, by default 0
- events (List[str], optional): custom events that the operator publishes, by default None
VectorKPointsCrossover(probability=1, arity=2, k=1, events=None)
11 def __init__(self, probability=1, arity=2, k=1, events=None): 12 """ 13 Vector K Point Crossover. 14 15 Randomly chooses K points in the vector and swaps the parts 16 of two vectors at these points. 17 18 Parameters 19 ---------- 20 probability : float 21 The probability of the mutation operator to be applied 22 23 arity : int 24 The number of individuals this mutation is applied on 25 26 k : int 27 Number of points to cut the vector for the crossover. 28 29 events: list of strings 30 Custom events to be published by the mutation, by default None 31 """ 32 self.individuals = None 33 self.applied_individuals = None 34 self.k = k 35 super().__init__(probability=probability, arity=arity, events=events)
Vector K Point Crossover.
Randomly chooses K points in the vector and swaps the parts of two vectors at these points.
Parameters
- probability (float): The probability of the mutation operator to be applied
- arity (int): The number of individuals this mutation is applied on
- k (int): Number of points to cut the vector for the crossover.
- events (list of strings): Custom events to be published by the mutation, by default None
def
apply( self, individuals: List[eckity.genetic_encodings.ga.vector_individual.Vector]) -> List[eckity.genetic_encodings.ga.vector_individual.Vector]:
37 def apply(self, individuals: List[Vector]) -> List[Vector]: 38 """ 39 Attempt to perform the mutation operator 40 41 Parameters 42 ---------- 43 individuals : List[Vector] 44 individuals to perform crossover on 45 46 Returns 47 ---------- 48 List[Vector] 49 individuals after the crossover 50 """ 51 self.individuals = individuals 52 xo_points = sorted(sample(range(1, individuals[0].size()), self.k)) 53 self._swap_vector_parts( 54 individuals[0].vector, individuals[1].vector, xo_points 55 ) 56 57 self.applied_individuals = individuals 58 return individuals
Attempt to perform the mutation operator
Parameters
- individuals (List[Vector]): individuals to perform crossover on
Returns
- List[Vector]: individuals after the crossover