Exemple #1
0
 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.")
Exemple #2
0
 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]))