eckity.genetic_encodings.ga.float_vector
This module implements the FloatVector class.
1""" 2This module implements the FloatVector class. 3""" 4 5from random import uniform, gauss 6 7from eckity.genetic_encodings.ga.vector_individual import Vector 8 9 10class FloatVector(Vector): 11 """ 12 A Float Vector individual representation for Genetic Algorithms operations. 13 It is represented by a list of floats. 14 15 Parameters 16 ---------- 17 fitness : Fitness 18 Fitness handler class, responsible of keeping the fitness value of the individual. 19 20 length : int 21 Vector length - the number of cells in the vector. 22 23 bounds : tuple or list of tuples 24 Min/Max values for each vector cell (if of length n), or the minimum and maximum (if of length 1). 25 """ 26 27 def __init__( 28 self, 29 fitness, 30 length, 31 bounds=(0.0, 1.0), 32 vector=None, 33 update_parents=False, 34 ): 35 super().__init__( 36 fitness=fitness, 37 length=length, 38 bounds=bounds, 39 vector=vector, 40 update_parents=update_parents, 41 ) 42 43 def get_random_number_in_bounds(self, index): 44 """ 45 Return a random number from possible cell values. 46 47 Parameters 48 ---------- 49 index : int 50 cell index 51 52 Returns 53 ------- 54 float 55 random value according to bounds field 56 """ 57 if type(self.bounds) == tuple: 58 return uniform(self.bounds[0], self.bounds[1]) 59 return uniform(self.bounds[index][0], self.bounds[index][1]) 60 61 def get_random_number_with_gauss(self, index, mu, sigma): 62 """ 63 Return a random number from possible cell values, with an addition of gaussian noise. 64 65 Parameters 66 ---------- 67 index : int 68 cell index 69 mu : float 70 gaussian mean 71 sigma : float 72 gaussian standard deviation 73 74 Returns 75 ------- 76 float 77 random value according to bounds field and gauss parameters 78 """ 79 return self.cell_value(index) + gauss(mu, sigma) 80 81 82# end class float vector
11class FloatVector(Vector): 12 """ 13 A Float Vector individual representation for Genetic Algorithms operations. 14 It is represented by a list of floats. 15 16 Parameters 17 ---------- 18 fitness : Fitness 19 Fitness handler class, 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), or the minimum and maximum (if of length 1). 26 """ 27 28 def __init__( 29 self, 30 fitness, 31 length, 32 bounds=(0.0, 1.0), 33 vector=None, 34 update_parents=False, 35 ): 36 super().__init__( 37 fitness=fitness, 38 length=length, 39 bounds=bounds, 40 vector=vector, 41 update_parents=update_parents, 42 ) 43 44 def get_random_number_in_bounds(self, index): 45 """ 46 Return a random number from possible cell values. 47 48 Parameters 49 ---------- 50 index : int 51 cell index 52 53 Returns 54 ------- 55 float 56 random value according to bounds field 57 """ 58 if type(self.bounds) == tuple: 59 return uniform(self.bounds[0], self.bounds[1]) 60 return uniform(self.bounds[index][0], self.bounds[index][1]) 61 62 def get_random_number_with_gauss(self, index, mu, sigma): 63 """ 64 Return a random number from possible cell values, with an addition of gaussian noise. 65 66 Parameters 67 ---------- 68 index : int 69 cell index 70 mu : float 71 gaussian mean 72 sigma : float 73 gaussian standard deviation 74 75 Returns 76 ------- 77 float 78 random value according to bounds field and gauss parameters 79 """ 80 return self.cell_value(index) + gauss(mu, sigma)
A Float Vector individual representation for Genetic Algorithms operations. It is represented by a list of floats.
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):
44 def get_random_number_in_bounds(self, index): 45 """ 46 Return a random number from possible cell values. 47 48 Parameters 49 ---------- 50 index : int 51 cell index 52 53 Returns 54 ------- 55 float 56 random value according to bounds field 57 """ 58 if type(self.bounds) == tuple: 59 return uniform(self.bounds[0], self.bounds[1]) 60 return uniform(self.bounds[index][0], self.bounds[index][1])
Return a random number from possible cell values.
Parameters
- index (int): cell index
Returns
- float: random value according to bounds field
def
get_random_number_with_gauss(self, index, mu, sigma):
62 def get_random_number_with_gauss(self, index, mu, sigma): 63 """ 64 Return a random number from possible cell values, with an addition of gaussian noise. 65 66 Parameters 67 ---------- 68 index : int 69 cell index 70 mu : float 71 gaussian mean 72 sigma : float 73 gaussian standard deviation 74 75 Returns 76 ------- 77 float 78 random value according to bounds field and gauss parameters 79 """ 80 return self.cell_value(index) + gauss(mu, sigma)
Return a random number from possible cell values, with an addition of gaussian noise.
Parameters
- index (int): cell index
- mu (float): gaussian mean
- sigma (float): gaussian standard deviation
Returns
- float: random value according to bounds field and gauss parameters