eckity.genetic_encodings.ga.bit_string_vector
This module implements the BitStringVector class.
1""" 2This module implements the BitStringVector class. 3""" 4 5from random import randint 6 7from eckity.genetic_encodings.ga.vector_individual import Vector 8 9 10class BitStringVector(Vector): 11 """ 12 A Bit Vector individual representation for Genetic Algorithms operations. 13 It is represented by a list of ones and zeros. 14 15 Parameters 16 ---------- 17 fitness : Fitness 18 Fitness handler class. 19 Responsible of keeping the fitness value of the individual. 20 21 length : int 22 Vector length - the number of cells in the vector. 23 24 bounds : tuple or list of tuples 25 Min/Max values for each vector cell (if of length n), 26 or the minimum and maximum (if of length 1). 27 """ 28 29 def __init__(self, 30 fitness, 31 length, 32 bounds=(0, 1), 33 vector=None, 34 update_parents=False): 35 super().__init__(fitness=fitness, 36 length=length, 37 bounds=bounds, 38 vector=vector, 39 update_parents=update_parents) 40 41 def get_random_number_in_bounds(self, index): 42 """ 43 Return a random number of available cell values (0 or 1), 44 with equal probability. 45 46 Parameters 47 ---------- 48 index : int 49 cell index 50 51 Returns 52 ------- 53 int 54 random value according to bounds field 55 """ 56 return randint(self.bounds[0], self.bounds[1]) 57 58 def bit_flip(self, index): 59 """ 60 Flip the bit in the given index. 61 """ 62 return self.bounds[1] \ 63 if self.cell_value(index) == self.bounds[0] else self.bounds[0] 64 65# end class bit string vector
11class BitStringVector(Vector): 12 """ 13 A Bit Vector individual representation for Genetic Algorithms operations. 14 It is represented by a list of ones and zeros. 15 16 Parameters 17 ---------- 18 fitness : Fitness 19 Fitness handler class. 20 Responsible of keeping the fitness value of the individual. 21 22 length : int 23 Vector length - the number of cells in the vector. 24 25 bounds : tuple or list of tuples 26 Min/Max values for each vector cell (if of length n), 27 or the minimum and maximum (if of length 1). 28 """ 29 30 def __init__(self, 31 fitness, 32 length, 33 bounds=(0, 1), 34 vector=None, 35 update_parents=False): 36 super().__init__(fitness=fitness, 37 length=length, 38 bounds=bounds, 39 vector=vector, 40 update_parents=update_parents) 41 42 def get_random_number_in_bounds(self, index): 43 """ 44 Return a random number of available cell values (0 or 1), 45 with equal probability. 46 47 Parameters 48 ---------- 49 index : int 50 cell index 51 52 Returns 53 ------- 54 int 55 random value according to bounds field 56 """ 57 return randint(self.bounds[0], self.bounds[1]) 58 59 def bit_flip(self, index): 60 """ 61 Flip the bit in the given index. 62 """ 63 return self.bounds[1] \ 64 if self.cell_value(index) == self.bounds[0] else self.bounds[0]
A Bit Vector individual representation for Genetic Algorithms operations. It is represented by a list of ones and zeros.
Parameters
- fitness (Fitness): Fitness handler class. Responsible of keeping the fitness value of the individual.
- length (int): Vector length - the number of cells in the vector.
- bounds (tuple or list of tuples): Min/Max values for each vector cell (if of length n), or the minimum and maximum (if of length 1).
def
get_random_number_in_bounds(self, index):
42 def get_random_number_in_bounds(self, index): 43 """ 44 Return a random number of available cell values (0 or 1), 45 with equal probability. 46 47 Parameters 48 ---------- 49 index : int 50 cell index 51 52 Returns 53 ------- 54 int 55 random value according to bounds field 56 """ 57 return randint(self.bounds[0], self.bounds[1])
Return a random number of available cell values (0 or 1), with equal probability.
Parameters
- index (int): cell index
Returns
- int: random value according to bounds field
def
bit_flip(self, index):
59 def bit_flip(self, index): 60 """ 61 Flip the bit in the given index. 62 """ 63 return self.bounds[1] \ 64 if self.cell_value(index) == self.bounds[0] else self.bounds[0]
Flip the bit in the given index.