예제 #1
0
    def test_validation(self):
        ListFactory(Random(), 3, size=3, is_unique=True)

        with self.assertRaises(ValidationException):
            ListFactory(Random(), 2, size=4, is_unique=True)
        with self.assertRaises(ValidationException):
            ListFactory(Random(), 3, size=5, is_unique=True)

        ListFactory(Random(), 4, size=2)
 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])
예제 #3
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])
 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)
예제 #5
0
 def test_apply_one(self):
     alphabet = 'abcd'
     inputs, outputs = ('aaaa', 'abcd')
     candidate = Candidate()
     candidate.data = inputs
     population = Population()
     population.append(candidate)
     probability = Probability(1)
     random = Random()
     random.choice = MagicMock(side_effect=['a', 'b', 'c', 'd'])
     crossover_operator = StringMutation(probability, random, alphabet)
     result = crossover_operator.apply(population)
     self.assertEqual(len(result), len(population))
     self.assertEqual(result[0].data, outputs)
 def test_create_population(self):
     random = Random()
     size = 4
     candidate_factory = CandidateFactory(random)
     candidate_factory.create_candidate = MagicMock()
     result = candidate_factory.create_population(size)
     self.assertEqual(candidate_factory.create_candidate.call_count, size)
     self.assertEqual(len(result), size)
예제 #7
0
 def test_validate(self):
     crossover_operator = StringCrossover(Probability(1), Random(), 1)
     candidate1 = Candidate()
     candidate1.data = 'AAAA'
     candidate2 = Candidate()
     candidate2.data = 'BBBB'
     crossover_operator.validate_parents(candidate1, candidate2)
     candidate2.data = 'BBBBB'
     with self.assertRaises(ValidationException):
         crossover_operator.validate_parents(candidate1, candidate2)
예제 #8
0
    def test_validate(self):
        crossover_operator = ListCrossover(Probability(1), Random(), 1)
        candidate1 = Candidate()
        candidate1.data = [1, 2, 3, 4]
        candidate2 = Candidate()
        candidate2.data = [5, 6, 7, 8]
        crossover_operator.validate_parents(candidate1, candidate2)

        candidate2.data = [1, 2, 3, 4, 5]
        with self.assertRaises(ValidationException):
            crossover_operator.validate_parents(candidate1, candidate2)
예제 #9
0
 def setUp(self):
     random = Random()
     factory = CandidateFactory(random)
     factory.create_candidate = MagicMock(side_effect=lambda: Candidate())
     operator = Operator()
     fitness_evaluator = FitnessEvaluator()
     fitness_evaluator.get_fitness = MagicMock(
         side_effect=lambda c, p: Fitness(5))
     selection_strategy = SelectionStrategy()
     self.engine = EvolutionEngine()
     self.engine.create(factory, operator, fitness_evaluator,
                        selection_strategy)
 def test_apply_zero(self):
     inputs, outputs = ('aaaa', 'aaaa')
     candidate = Candidate()
     candidate.data = inputs
     population = Population()
     population.append(candidate)
     probability = Probability(0)
     random = Random()
     mutation_operator = BitStringMutation(probability, random)
     result = mutation_operator.apply(population)
     self.assertEqual(len(result), len(population))
     self.assertEqual(result[0].data, outputs)
예제 #11
0
 def setUp(self):
     random = Random()
     factory = CandidateFactory(random)
     evolutionary_operator = Operator()
     evolutionary_operator.apply = MagicMock(side_effect=lambda p: p)
     fitness_evaluator = FitnessEvaluator()
     fitness_evaluator.get_fitness = MagicMock(return_value=5)
     selection_strategy = SelectionStrategy()
     selection_strategy.validate = MagicMock()
     selection_strategy.select = MagicMock(side_effect=lambda p, s: p[0:s])
     self.engine = GenerationalEvolutionEngine()
     self.engine.create(factory, evolutionary_operator, fitness_evaluator,
                        selection_strategy)
예제 #12
0
 def test_apply_zero(self):
     alphabet = 'abcd'
     inputs, outputs = ('aaaa', 'aaaa')
     candidate = Candidate()
     candidate.data = inputs
     population = Population()
     population.append(candidate)
     probability = Probability(0)
     random = Random()
     crossover_operator = StringMutation(probability, random, alphabet)
     result = crossover_operator.apply(population)
     self.assertEqual(len(result), len(population))
     self.assertEqual(result[0].data, outputs)
예제 #13
0
    def test_create_invalid(self):
        self.engine = EvolutionEngine()
        with self.assertRaises(TypeError) as cm:
            self.engine.create(None, None, None, None)
        self.assertIn('CandidateFactory', str(cm.exception))

        random = Random()
        factory = CandidateFactory(random)
        with self.assertRaises(TypeError) as cm:
            self.engine.create(factory, None, None, None)
        self.assertIn('Operator', str(cm.exception))

        operator = Operator()
        with self.assertRaises(TypeError) as cm:
            self.engine.create(factory, operator, None, None)
        self.assertIn('FitnessEvaluator', str(cm.exception))

        fitness_evaluator = FitnessEvaluator()
        with self.assertRaises(TypeError) as cm:
            self.engine.create(factory, operator, fitness_evaluator, None)
        self.assertIn('SelectionStrategy', str(cm.exception))
예제 #14
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)
예제 #15
0
 def test_create_population_unique(self):
     random = Random()
     random.sample = MagicMock(return_value=[0, 1, 2, 3])
     candidate_factory = ListFactory(random, 3)
     result = candidate_factory.create_candidate()
     self.assertEqual(result.data, [0, 1, 2, 3])
예제 #16
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])
예제 #17
0
 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')
예제 #18
0
 def setUp(self):
     self.random = Random()
     self.obj = TournamentSelection(2, self.random)
예제 #19
0
 def setUp(self):
     self.random = Random()
     self.obj = StochasticUniversalSamplingSelection(self.random)
예제 #20
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)
예제 #21
0
 def test_create_population(self):
     random = Random()
     random.choice = MagicMock(side_effect=['a','a','c','d'])
     candidate_factory = StringFactory(random, 'abcd', 4)
     result = candidate_factory.create_candidate()
     self.assertEqual(result.data, 'aacd')
예제 #22
0
 def test_apply_single_point(self):
     random = Random()
     random.int = MagicMock(return_value=3)
     self._test_apply([['AAAAAA', 'BBBBBB'], ['AAABBB', 'BBBAAA']], random,
                      1)
예제 #23
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)
예제 #24
0
 def setUp(self):
     self.random = Random()
     self.obj = RankSelection(self.random)
예제 #25
0
 def setUp(self):
     self.random = Random()
     self.obj = RouletteWheelSelection(self.random)