def test_apply(self):
     candidate = Candidate()
     candidate.data = [1, 2, 3, 4]
     population = Population([candidate])
     probability = Probability(1)
     random = Random()
     random.int = MagicMock(side_effect=[1, 3])
     crossover_operator = ListOrderMutation(probability, random, 1)
     result = crossover_operator.apply(population)
     self.assertEqual(len(result), len(population))
     self.assertEqual(result[0].data, [1, 4, 3, 2])
예제 #2
0
 def test_apply(self):
     candidate1 = Candidate()
     candidate1.data = [1, 2, 3, 4, 5, 6, 7, 8, 9]
     candidate2 = Candidate()
     candidate2.data = [9, 3, 7, 8, 2, 6, 5, 1, 4]
     random = Random()
     random.int = MagicMock(side_effect=[3, 7])
     crossover_operator = ListOrderCrossover(Probability(1), random)
     result = crossover_operator.mate(candidate1, candidate2)
     self.assertEqual(len(result), 2)
     self.assertEqual(result[0].data, [1, 7, 3, 8, 2, 6, 5, 4, 9])
     self.assertEqual(result[1].data, [9, 3, 2, 4, 5, 6, 7, 1, 8])
예제 #3
0
 def test_apply_two_points(self):
     random = Random()
     random.int = MagicMock(side_effect=[1, 3])
     self._test_apply([[[1, 2, 3, 4], [5, 6, 7, 8]],
                       [[1, 6, 7, 4], [5, 2, 3, 8]]], random, 2)
예제 #4
0
 def test_apply_single_point(self):
     random = Random()
     random.int = MagicMock(return_value=2)
     self._test_apply([[[1, 2, 3, 4], [5, 6, 7, 8]],
                       [[1, 2, 7, 8], [5, 6, 3, 4]]], random, 1)
예제 #5
0
 def test_create_population_not_unique(self):
     random = Random()
     random.int = MagicMock(side_effect=[0, 1, 2, 1, 2, 0])
     candidate_factory = ListFactory(random, 3, size=6, is_unique=False)
     result = candidate_factory.create_candidate()
     self.assertEqual(result.data, [0, 1, 2, 1, 2, 0])
예제 #6
0
 def test_apply_four_points(self):
     random = Random()
     random.int = MagicMock(side_effect=[2, 4, 6, 8])
     self._test_apply(
         [['AAAAAAAAAA', 'BBBBBBBBBB'], ['AABBAABBAA', 'BBAABBAABB']],
         random, 4)
예제 #7
0
 def test_apply_single_point(self):
     random = Random()
     random.int = MagicMock(return_value=3)
     self._test_apply([['AAAAAA', 'BBBBBB'], ['AAABBB', 'BBBAAA']], random,
                      1)