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        super().__init__(fitness=fitness,
35                         length=length,
36                         bounds=bounds,
37                         vector=vector)
38
39    def get_random_number_in_bounds(self, index):
40        """
41        Return a random number of available cell values (0 or 1),
42        with equal probability.
43
44        Parameters
45        ----------
46        index : int
47            cell index
48
49        Returns
50        -------
51        int
52            random value according to bounds field
53        """
54        return randint(self.bounds[0], self.bounds[1])
55
56    def bit_flip(self, index):
57        """
58        Flip the bit in the given index.
59        """
60        return self.bounds[1] \
61            if self.cell_value(index) == self.bounds[0] else self.bounds[0]
62
63# end class bit string vector
class BitStringVector(eckity.genetic_encodings.ga.vector_individual.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        super().__init__(fitness=fitness,
36                         length=length,
37                         bounds=bounds,
38                         vector=vector)
39
40    def get_random_number_in_bounds(self, index):
41        """
42        Return a random number of available cell values (0 or 1),
43        with equal probability.
44
45        Parameters
46        ----------
47        index : int
48            cell index
49
50        Returns
51        -------
52        int
53            random value according to bounds field
54        """
55        return randint(self.bounds[0], self.bounds[1])
56
57    def bit_flip(self, index):
58        """
59        Flip the bit in the given index.
60        """
61        return self.bounds[1] \
62            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).
BitStringVector(fitness, length, bounds=(0, 1), vector=None)
30    def __init__(self,
31                 fitness,
32                 length,
33                 bounds=(0, 1),
34                 vector=None):
35        super().__init__(fitness=fitness,
36                         length=length,
37                         bounds=bounds,
38                         vector=vector)
def get_random_number_in_bounds(self, index):
40    def get_random_number_in_bounds(self, index):
41        """
42        Return a random number of available cell values (0 or 1),
43        with equal probability.
44
45        Parameters
46        ----------
47        index : int
48            cell index
49
50        Returns
51        -------
52        int
53            random value according to bounds field
54        """
55        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):
57    def bit_flip(self, index):
58        """
59        Flip the bit in the given index.
60        """
61        return self.bounds[1] \
62            if self.cell_value(index) == self.bounds[0] else self.bounds[0]

Flip the bit in the given index.