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    def __init__(self,
27                 fitness,
28                 length,
29                 bounds=(0.0, 1.0),
30                 vector=None):
31        super().__init__(fitness=fitness, length=length, bounds=bounds, vector=vector)
32
33    def get_random_number_in_bounds(self, index):
34        """
35        Return a random number from possible cell values.
36
37        Parameters
38        ----------
39        index : int
40            cell index
41
42        Returns
43        -------
44        float
45            random value according to bounds field
46        """
47        if type(self.bounds) == tuple:
48            return uniform(self.bounds[0], self.bounds[1])
49        return uniform(self.bounds[index][0], self.bounds[index][1])
50
51    def get_random_number_with_gauss(self, index, mu, sigma):
52        """
53        Return a random number from possible cell values, with an addition of gaussian noise.
54
55        Parameters
56        ----------
57        index : int
58            cell index
59        mu : float
60            gaussian mean
61        sigma : float
62            gaussian standard deviation
63
64        Returns
65        -------
66        float
67            random value according to bounds field and gauss parameters
68        """
69        return self.cell_value(index) + gauss(mu, sigma)
70
71# 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    def __init__(self,
28                 fitness,
29                 length,
30                 bounds=(0.0, 1.0),
31                 vector=None):
32        super().__init__(fitness=fitness, length=length, bounds=bounds, vector=vector)
33
34    def get_random_number_in_bounds(self, index):
35        """
36        Return a random number from possible cell values.
37
38        Parameters
39        ----------
40        index : int
41            cell index
42
43        Returns
44        -------
45        float
46            random value according to bounds field
47        """
48        if type(self.bounds) == tuple:
49            return uniform(self.bounds[0], self.bounds[1])
50        return uniform(self.bounds[index][0], self.bounds[index][1])
51
52    def get_random_number_with_gauss(self, index, mu, sigma):
53        """
54        Return a random number from possible cell values, with an addition of gaussian noise.
55
56        Parameters
57        ----------
58        index : int
59            cell index
60        mu : float
61            gaussian mean
62        sigma : float
63            gaussian standard deviation
64
65        Returns
66        -------
67        float
68            random value according to bounds field and gauss parameters
69        """
70        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).
FloatVector(fitness, length, bounds=(0.0, 1.0), vector=None)
27    def __init__(self,
28                 fitness,
29                 length,
30                 bounds=(0.0, 1.0),
31                 vector=None):
32        super().__init__(fitness=fitness, length=length, bounds=bounds, vector=vector)
def get_random_number_in_bounds(self, index):
34    def get_random_number_in_bounds(self, index):
35        """
36        Return a random number from possible cell values.
37
38        Parameters
39        ----------
40        index : int
41            cell index
42
43        Returns
44        -------
45        float
46            random value according to bounds field
47        """
48        if type(self.bounds) == tuple:
49            return uniform(self.bounds[0], self.bounds[1])
50        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):
52    def get_random_number_with_gauss(self, index, mu, sigma):
53        """
54        Return a random number from possible cell values, with an addition of gaussian noise.
55
56        Parameters
57        ----------
58        index : int
59            cell index
60        mu : float
61            gaussian mean
62        sigma : float
63            gaussian standard deviation
64
65        Returns
66        -------
67        float
68            random value according to bounds field and gauss parameters
69        """
70        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