def test_apply_one(self): inputs, outputs = ('0110', '1010') candidate = Candidate() candidate.data = inputs population = Population() population.append(candidate) probability = Probability(1) random = Random() random.float = MagicMock(side_effect=[0.6, 0.1, 0.9, 0.4]) mutation_operator = BitStringMutation(probability, random) result = mutation_operator.apply(population) self.assertEqual(len(result), len(population)) self.assertEqual(result[0].data, outputs)
def test_create_population(self): random = Random() random.float = MagicMock(side_effect=[0, 1, 1, 0]) candidate_factory = BitStringFactory(random, 4) result = candidate_factory.create_candidate() self.assertEqual(result.data, '0110')