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 def __init__(self, 30 fitness, 31 length, 32 bounds=(MIN_BOUND, MAX_BOUND), 33 vector=None): 34 super().__init__(fitness, length=length, bounds=bounds, vector=vector) 35 36 def get_random_number_in_bounds(self, index): 37 """ 38 Return a random number from possible cell values, according to bounds. 39 40 Parameters 41 ---------- 42 index : int 43 cell index 44 45 Returns 46 ------- 47 float 48 random value according to bounds field 49 """ 50 if type(self.bounds) == tuple: 51 return randint(self.bounds[0], self.bounds[1]) 52 return randint(self.bounds[index][0], self.bounds[index][1]) 53 54# 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 def __init__(self, 31 fitness, 32 length, 33 bounds=(MIN_BOUND, MAX_BOUND), 34 vector=None): 35 super().__init__(fitness, length=length, bounds=bounds, vector=vector) 36 37 def get_random_number_in_bounds(self, index): 38 """ 39 Return a random number from possible cell values, according to bounds. 40 41 Parameters 42 ---------- 43 index : int 44 cell index 45 46 Returns 47 ------- 48 float 49 random value according to bounds field 50 """ 51 if type(self.bounds) == tuple: 52 return randint(self.bounds[0], self.bounds[1]) 53 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):
37 def get_random_number_in_bounds(self, index): 38 """ 39 Return a random number from possible cell values, according to bounds. 40 41 Parameters 42 ---------- 43 index : int 44 cell index 45 46 Returns 47 ------- 48 float 49 random value according to bounds field 50 """ 51 if type(self.bounds) == tuple: 52 return randint(self.bounds[0], self.bounds[1]) 53 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