def setUpClass(cls): population = [ Individual('10101'), Individual('11000'), Individual('01111') ] cls._selector = Selector(population)
def test_select_parents(self): seed(1) first_parent, second_parent = self._selector.select_parents() expected_first_parent = Individual('01111') expected_second_parent = Individual('10101') self.assertEqual(expected_first_parent, first_parent) self.assertEqual(expected_second_parent, second_parent)
def _uniform_crossover( self, parent1: Individual, parent2: Individual) -> Tuple[Individual, Individual]: child1 = Individual('') child2 = Individual('') for i in range(self._string_size): if randint(0, 1): child1.binary_string += parent1[i] child2.binary_string += parent2[i] else: child1.binary_string += parent2[i] child2.binary_string += parent1[i] return child1, child2
def _single_point_crossover( self, parent1: Individual, parent2: Individual) -> Tuple[Individual, Individual]: child1 = Individual('') child2 = Individual('') k = randint(0, self._string_size - 1) for i in range(k): child1.binary_string += parent1[i] child2.binary_string += parent2[i] for i in range(k, self._string_size): child1.binary_string += parent2[i] child2.binary_string += parent1[i] return child1, child2
def test_create_individual(self): seed(1) expected_binary_string = Individual('00101') length = 5 binary_string = self._creator._create_individual(length) self.assertEqual(length, len(binary_string)) self.assertEqual(expected_binary_string, binary_string)
def test_select_pairs_of_parents(self): seed(1) expected_pairs_of_parents = [ (Individual('01111'), Individual('10101')), (Individual('10101'), Individual('11000')), (Individual('01111'), Individual('10101')) ] pairs_of_parents = self._selector.select_pairs_of_parents(3) self.assertEqual(expected_pairs_of_parents, pairs_of_parents)
def test_replace_population(self): seed(1) expected_new_population = [ Individual('11101'), Individual('11001'), Individual('01100'), Individual('01100'), Individual('10110'), Individual('11001') ] population = self._creator.create_population() new_population = self._creator._replace_population(population) self.assertEqual(expected_new_population, new_population)
def test_select_best_individuals(self): seed(1) expected_best_individuals = [Individual('10101'), Individual('01111')] best_individuals = self._selector.select_best_individuals(2) self.assertEqual(expected_best_individuals, best_individuals)
def test_select_best_individual(self): seed(1) expected_best_individual = Individual('01111') best_individual = self._selector.select_best_individual() self.assertEqual(expected_best_individual, best_individual)
def test_select_parent(self): seed(1) expected_parent = Individual('01111') parent = self._selector._select_parent() self.assertEqual(expected_parent, parent)
def test_select_two_random_individuals(self): seed(1) first, second = self._selector._select_two_random_individuals() self.assertEqual(Individual('10101'), first) self.assertEqual(Individual('01111'), second)
def test_uniform_crossover(self): seed(1) expected_children = (Individual('10001'), Individual('01110')) children = self._breeder._uniform_crossover(Individual('01011'), Individual('10100')) self.assertEqual(expected_children, children)
def _create_individual(size: int) -> Individual: binary_string = '' for _ in range(size): binary_string += str(randint(0, 1)) return Individual(binary_string)
def test_mutate(self): seed(1) expected_child = Individual('00100') child = self._breeder._mutate(Individual('10100')) self.assertEqual(expected_child, child)
def test_select_random_individual(self): seed(1) random_individual = self._selector._select_random_individual() self.assertEqual(Individual('10101'), random_individual)
def setUpClass(cls): population = [Individual('10101'), Individual('1100'), Individual('01111')] cls._fitness_tracker = FitnessTracker(population)
def test_breed(self): seed(10) # Seed with 10 to apply uniform crossover operator expected_children = (Individual('01010'), Individual('00110')) children = self._breeder.breed(Individual('01010'), Individual('00111')) self.assertEqual(expected_children, children)
def test_create_population(self): seed(1) population_size = 3 creator = Creator(population_size=population_size, string_size=5) population = creator.create_population() expected_population = [ Individual('00101'), Individual('11100'), Individual('10110') ] self.assertEqual(population_size, len(population)) self.assertEqual(expected_population, population)