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.vector_individual import Vector 5 6 7class VectorKPointsCrossover(GeneticOperator): 8 def __init__(self, probability=1, arity=2, k=1, events=None): 9 """ 10 Vector N Point Mutation. 11 12 Randomly chooses N vector cells and performs a small change in their values. 13 14 Parameters 15 ---------- 16 probability : float 17 The probability of the mutation operator to be applied 18 19 arity : int 20 The number of individuals this mutation is applied on 21 22 k : int 23 Number of points to cut the vector for the crossover. 24 25 events: list of strings 26 Events to publish before/after the mutation operator 27 """ 28 self.individuals = None 29 self.applied_individuals = None 30 self.k = k 31 self.points = None 32 super().__init__(probability=probability, arity=arity, events=events) 33 34 def apply(self, individuals): 35 """ 36 Attempt to perform the mutation operator 37 38 Parameters 39 ---------- 40 individuals : list of individuals 41 individuals to perform crossover on 42 43 Returns 44 ---------- 45 list of individuals 46 individuals after the crossover 47 """ 48 self.individuals = individuals 49 self.points = sorted(sample(range(1, individuals[0].size()), self.k)) 50 51 start_index = 0 52 for i in range(0, len(self.points), 2): 53 end_point = self.points[i] 54 replaced_part = individuals[0].get_vector_part(start_index, end_point) 55 replaced_part = individuals[1].replace_vector_part(replaced_part, start_index) 56 individuals[0].replace_vector_part(replaced_part, start_index) 57 start_index = end_point 58 59 # replace the last part (from last point to end) 60 replaced_part = individuals[0].get_vector_part(self.points[-1], individuals[0].size()) 61 replaced_part = individuals[1].replace_vector_part(replaced_part, self.points[-1]) 62 individuals[0].replace_vector_part(replaced_part, self.points[-1]) 63 64 self.applied_individuals = individuals 65 return individuals
8class VectorKPointsCrossover(GeneticOperator): 9 def __init__(self, probability=1, arity=2, k=1, events=None): 10 """ 11 Vector N Point Mutation. 12 13 Randomly chooses N vector cells and performs a small change in their values. 14 15 Parameters 16 ---------- 17 probability : float 18 The probability of the mutation operator to be applied 19 20 arity : int 21 The number of individuals this mutation is applied on 22 23 k : int 24 Number of points to cut the vector for the crossover. 25 26 events: list of strings 27 Events to publish before/after the mutation operator 28 """ 29 self.individuals = None 30 self.applied_individuals = None 31 self.k = k 32 self.points = None 33 super().__init__(probability=probability, arity=arity, events=events) 34 35 def apply(self, individuals): 36 """ 37 Attempt to perform the mutation operator 38 39 Parameters 40 ---------- 41 individuals : list of individuals 42 individuals to perform crossover on 43 44 Returns 45 ---------- 46 list of individuals 47 individuals after the crossover 48 """ 49 self.individuals = individuals 50 self.points = sorted(sample(range(1, individuals[0].size()), self.k)) 51 52 start_index = 0 53 for i in range(0, len(self.points), 2): 54 end_point = self.points[i] 55 replaced_part = individuals[0].get_vector_part(start_index, end_point) 56 replaced_part = individuals[1].replace_vector_part(replaced_part, start_index) 57 individuals[0].replace_vector_part(replaced_part, start_index) 58 start_index = end_point 59 60 # replace the last part (from last point to end) 61 replaced_part = individuals[0].get_vector_part(self.points[-1], individuals[0].size()) 62 replaced_part = individuals[1].replace_vector_part(replaced_part, self.points[-1]) 63 individuals[0].replace_vector_part(replaced_part, self.points[-1]) 64 65 self.applied_individuals = individuals 66 return individuals
VectorKPointsCrossover(probability=1, arity=2, k=1, events=None)
9 def __init__(self, probability=1, arity=2, k=1, events=None): 10 """ 11 Vector N Point Mutation. 12 13 Randomly chooses N vector cells and performs a small change in their values. 14 15 Parameters 16 ---------- 17 probability : float 18 The probability of the mutation operator to be applied 19 20 arity : int 21 The number of individuals this mutation is applied on 22 23 k : int 24 Number of points to cut the vector for the crossover. 25 26 events: list of strings 27 Events to publish before/after the mutation operator 28 """ 29 self.individuals = None 30 self.applied_individuals = None 31 self.k = k 32 self.points = None 33 super().__init__(probability=probability, arity=arity, events=events)
Vector N Point Mutation.
Randomly chooses N vector cells and performs a small change in their values.
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): Events to publish before/after the mutation operator
def
apply(self, individuals):
35 def apply(self, individuals): 36 """ 37 Attempt to perform the mutation operator 38 39 Parameters 40 ---------- 41 individuals : list of individuals 42 individuals to perform crossover on 43 44 Returns 45 ---------- 46 list of individuals 47 individuals after the crossover 48 """ 49 self.individuals = individuals 50 self.points = sorted(sample(range(1, individuals[0].size()), self.k)) 51 52 start_index = 0 53 for i in range(0, len(self.points), 2): 54 end_point = self.points[i] 55 replaced_part = individuals[0].get_vector_part(start_index, end_point) 56 replaced_part = individuals[1].replace_vector_part(replaced_part, start_index) 57 individuals[0].replace_vector_part(replaced_part, start_index) 58 start_index = end_point 59 60 # replace the last part (from last point to end) 61 replaced_part = individuals[0].get_vector_part(self.points[-1], individuals[0].size()) 62 replaced_part = individuals[1].replace_vector_part(replaced_part, self.points[-1]) 63 individuals[0].replace_vector_part(replaced_part, self.points[-1]) 64 65 self.applied_individuals = individuals 66 return individuals
Attempt to perform the mutation operator
Parameters
- individuals (list of individuals): individuals to perform crossover on
Returns
- list of individuals: individuals after the crossover