def test_eq(self): """ Ensures that only the chromosome is used for equality. """ x = Genome([1, 2, 3]) y = Genome([1, 2, 3]) z = Genome([3, 2, 1]) self.assertEqual(x, y) self.assertNotEqual(x, z)
def test_return_parents_if_same(self): """ If both parents have the same chromosome then just return the parents. """ mum = Genome([1, 2, 3]) dad = Genome([1, 2, 3]) result = crossover(mum, dad, Genome) self.assertEqual(mum, result[0], "Child expected to be the same as parent.") self.assertEqual(mum, result[0], "Child expected to be the same as parent.")
def test_generate_function_returns_list(self): """ Ensures the new population is a list. """ generate_function = make_generate_function(7, 0.2, CANTUS_FIRMUS) g1 = Genome([1, 2, 3]) g1.fitness = 1 g2 = Genome([1, 2, 3]) g2.fitness = 2 seed_population = [g1, g2] result = generate_function(seed_population) self.assertTrue(list, type(result))
def test_init(self): """ Ensures the chromosome and fitness are initialised correctly. """ x = Genome([1, 2, 3]) self.assertEqual(None, x.fitness) self.assertEqual([1, 2, 3], x.chromosome)
def test_creates_two_babies(self): """ If the parents are different ensures that the children are the result of an expected crossover. """ mum = Genome([1, 2, 3, 4]) dad = Genome(['a', 'b', 'c', 'd']) result = crossover(mum, dad, Genome) # There must be two children. self.assertEqual(2, len(result)) # The randint call in crossover will choose 2 given random.seed(5). expected1 = Genome([1, 2, 'c', 'd']) expected2 = Genome(['a', 'b', 3, 4]) self.assertEqual(expected1, result[0], "Expected: %r Got: %r" % (expected1, result[0])) self.assertEqual(expected2, result[1], "Expected: %r Got: %r" % (expected2, result[1]))
def test_generate_function_returns_list_of_correct_length(self): """ Ensure the new population is the correct length. """ generate_function = make_generate_function(7, 0.2, CANTUS_FIRMUS) g1 = Genome([1, 2, 3]) g1.fitness = 1 g2 = Genome([1, 2, 3]) g2.fitness = 2 g3 = Genome([1, 2, 3]) g3.fitness = 3 seed_population = [g1, g2, g3] result = generate_function(seed_population) self.assertTrue(3, len(result))
def test_getitem(self): """ The __getitem__ function returns the corresponding result from the chromosome. """ x = Genome([1, 2, 3]) self.assertEqual(1, x[0]) self.assertEqual(2, x[1]) self.assertEqual(3, x[2])
def test_returns_genome(self): """ Ensures that the roulette wheel returns a selected genome. """ class Genome(object): """ A simple representation of a genome. """ def __init__(self, fitness): self.fitness = fitness population = [Genome(random.uniform(0.1, 10.0)) for i in range(4)] result = roulette_wheel_selection(population) self.assertIsInstance( result, Genome, "Expected result of roulette_wheel_selection is not a Genome")
def test_returns_genome_with_unfit_population(self): """ Ensures that the roulette wheel returns a randomly selected genome if none of them have a positive fitness score. """ class Genome(object): """ A simple representation of a genome. """ def __init__(self, fitness): self.fitness = fitness population = [Genome(0.0) for i in range(4)] result = roulette_wheel_selection(population) self.assertIsInstance( result, Genome, "Expected result of roulette_wheel_selection is not a Genome")
def test_breed(self): """ Ensures that the crossover function is called as a result of calling the breed method. """ x = Genome([1, 2, 3, 4]) mate = Genome(['a', 'b', 'c', 'd']) mock_crossover = MagicMock(return_value=(Genome([1, 2, 'a', 'b']), Genome(['a', 'b', 3, 4]))) with patch('foox.ga.crossover', mock_crossover): x.breed(mate) self.assertEqual( 1, mock_crossover.call_count, "Breed does not call crossover function expected number of times.") mock_crossover.assert_called_with(x, mate, Genome)
def test_repr(self): """ The __repr__ of the Genome is that of the chromosome. """ x = Genome([1, 2, 3]) self.assertEqual(repr([1, 2, 3]), repr(x))
def test_len(self): """ The __len__ function returns the length of the chromosome. """ x = Genome([1, 2, 3]) self.assertEqual(3, len(x))
def test_mutate(self): """ Ensurs the mutate method returns NotImplemented. """ x = Genome([1, 2, 3]) self.assertEqual(NotImplemented, x.mutate(1, 2, [1, 2, 3]))