eckity.genetic_encodings.ga.int_vector
This module implements the IntVector class.
1""" 2This module implements the IntVector class. 3""" 4 5from random import randint 6 7from eckity.genetic_encodings.ga.vector_individual import Vector 8 9MIN_BOUND = 2**31 - 1 10MAX_BOUND = -(2**31) 11 12 13class IntVector(Vector): 14 """ 15 An Integer Vector individual representation for Genetic Algorithms operations. 16 It is represented by a list of integers. 17 18 Parameters 19 ---------- 20 fitness : Fitness 21 Fitness handler class, responsible of keeping the fitness value of the individual. 22 23 length : int 24 Vector length - the number of cells in the vector. 25 26 bounds : tuple or list of tuples 27 Min/Max values for each vector cell (if of length n), or the minimum and maximum (if of length 1). 28 """ 29 30 def __init__( 31 self, 32 fitness, 33 length, 34 bounds=(MIN_BOUND, MAX_BOUND), 35 vector=None, 36 update_parents=False, 37 ): 38 super().__init__( 39 fitness, 40 length=length, 41 bounds=bounds, 42 vector=vector, 43 update_parents=update_parents, 44 ) 45 46 def get_random_number_in_bounds(self, index): 47 """ 48 Return a random number from possible cell values, according to bounds. 49 50 Parameters 51 ---------- 52 index : int 53 cell index 54 55 Returns 56 ------- 57 float 58 random value according to bounds field 59 """ 60 if type(self.bounds) == tuple: 61 return randint(self.bounds[0], self.bounds[1]) 62 return randint(self.bounds[index][0], self.bounds[index][1]) 63 64 65# end class int vector
MIN_BOUND =
2147483647
MAX_BOUND =
-2147483648
14class IntVector(Vector): 15 """ 16 An Integer Vector individual representation for Genetic Algorithms operations. 17 It is represented by a list of integers. 18 19 Parameters 20 ---------- 21 fitness : Fitness 22 Fitness handler class, responsible of keeping the fitness value of the individual. 23 24 length : int 25 Vector length - the number of cells in the vector. 26 27 bounds : tuple or list of tuples 28 Min/Max values for each vector cell (if of length n), or the minimum and maximum (if of length 1). 29 """ 30 31 def __init__( 32 self, 33 fitness, 34 length, 35 bounds=(MIN_BOUND, MAX_BOUND), 36 vector=None, 37 update_parents=False, 38 ): 39 super().__init__( 40 fitness, 41 length=length, 42 bounds=bounds, 43 vector=vector, 44 update_parents=update_parents, 45 ) 46 47 def get_random_number_in_bounds(self, index): 48 """ 49 Return a random number from possible cell values, according to bounds. 50 51 Parameters 52 ---------- 53 index : int 54 cell index 55 56 Returns 57 ------- 58 float 59 random value according to bounds field 60 """ 61 if type(self.bounds) == tuple: 62 return randint(self.bounds[0], self.bounds[1]) 63 return randint(self.bounds[index][0], self.bounds[index][1])
An Integer Vector individual representation for Genetic Algorithms operations. It is represented by a list of integers.
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):
47 def get_random_number_in_bounds(self, index): 48 """ 49 Return a random number from possible cell values, according to bounds. 50 51 Parameters 52 ---------- 53 index : int 54 cell index 55 56 Returns 57 ------- 58 float 59 random value according to bounds field 60 """ 61 if type(self.bounds) == tuple: 62 return randint(self.bounds[0], self.bounds[1]) 63 return randint(self.bounds[index][0], self.bounds[index][1])
Return a random number from possible cell values, according to bounds.
Parameters
- index (int): cell index
Returns
- float: random value according to bounds field