Exemple #1
0
 def test_select_fitness_zero(self):
     population = Population()
     population.append(self.create_candidate(fitness=0))
     population.append(self.create_candidate(fitness=0))
     population.append(self.create_candidate(fitness=0))
     population.append(self.create_candidate(fitness=0))
     results = self.obj.select(population, 4)
     self.assertEqual(len(results), 4)
Exemple #2
0
 def test_get_fitness(self):
     candidate = Candidate()
     population = Population()
     population.append(candidate)
     callback = MagicMock(return_value=2)
     fitness_evaluator = CallbackFitnessEvaluator(callback)
     result = fitness_evaluator.get_fitness(candidate, population)
     self.assertEqual(callback.call_count, 1)
     self.assertEqual(result, 2)
Exemple #3
0
 def _test_get_fitness(self, target, inputs, score):
     candidate = Candidate()
     candidate.data = inputs
     population = Population()
     population.append(candidate)
     fitness_evaluator = StringFitnessEvaluator(target)
     result = fitness_evaluator.get_fitness(candidate, population)
     self.assertIsInstance(result, Fitness)
     self.assertEqual(result, score)
 def test_next_evolution_step(self):
     population = Population()
     for i in range(5):
         population.append(Candidate())
     result = self.engine.next_evolution_step(population, 3)
     self.assertEqual(len(result), len(population))
     self.assertEqual(self.engine.selection_strategy.validate.call_count, 1)
     self.assertEqual(self.engine.selection_strategy.select.call_count, 1)
     self.assertEqual(self.engine.evolutionary_operator.apply.call_count, 1)
Exemple #5
0
 def test_select_result_size(self):
     population = Population()
     population.append(self.create_candidate(fitness=1))
     population.append(self.create_candidate(fitness=2))
     population.append(self.create_candidate(fitness=3))
     population.append(self.create_candidate(fitness=4))
     for selection_size in range(len(population)):
         results = self.obj.select(population, selection_size+1)
         self.assertEqual(selection_size+1, len(results))
 def test_apply_empty(self):
     candidate = Candidate()
     population = Population()
     population.append(candidate)
     population.shuffle = MagicMock()
     sample_operator = SampleOperator()
     pipeline_operator = PipelineOperator()
     pipeline_operator.append_operator(sample_operator)
     result = pipeline_operator.apply(population)
     self.assertEqual(len(result), len(population))
Exemple #7
0
 def test_select_proper_items(self):
     population = Population()
     population.append(self.create_candidate(fitness=1))
     population.append(self.create_candidate(fitness=2))
     population.append(self.create_candidate(fitness=3))
     population.append(self.create_candidate(fitness=4))
     selection_size = 2
     results = self.obj.select(population, selection_size)
     self.assertEqual(results[0].fitness, 4)
     self.assertEqual(results[1].fitness, 3)
Exemple #8
0
 def test_select_proper_items_natural_false(self):
     population = Population()
     population.append(self.create_candidate(fitness=-4))
     population.append(self.create_candidate(fitness=-3))
     population.append(self.create_candidate(fitness=-2))
     population.append(self.create_candidate(fitness=-1))
     selection_size = 2
     results = self.obj.select(population, selection_size)
     self.assertEqual(results[0].fitness, -1)
     self.assertEqual(results[1].fitness, -2)
Exemple #9
0
 def test_should_terminate(self):
     candidate = Candidate()
     candidate.fitness = Fitness(50)
     population = Population()
     population.append(candidate)
     termination_condition = TargetFitness(100)
     self.assertEqual(termination_condition.should_terminate(population),
                      False)
     candidate.fitness = Fitness(150)
     self.assertEqual(termination_condition.should_terminate(population),
                      True)
Exemple #10
0
 def test_select_proper_items(self):
     population = Population()
     population.append(self.create_candidate(fitness=1))
     population.append(self.create_candidate(fitness=2))
     population.append(self.create_candidate(fitness=3))
     population.append(self.create_candidate(fitness=4))
     selection_size = 3
     self.random.float = MagicMock(return_value=0.99)
     results = self.obj.select(population, selection_size)
     for candidate in results:
         self.assertEqual(candidate.fitness, 4)
Exemple #11
0
 def test_select_proper_items_natural_false(self):
     population = Population()
     population.append(self.create_candidate(fitness=-4, is_natural=False))
     population.append(self.create_candidate(fitness=-3, is_natural=False))
     population.append(self.create_candidate(fitness=-2, is_natural=False))
     population.append(self.create_candidate(fitness=-1, is_natural=False))
     selection_size = 2
     self.random.float = MagicMock(side_effect=[0.5, 0.9])
     results = self.obj.select(population, selection_size)
     self.assertEqual(results[0].fitness, -2)
     self.assertEqual(results[1].fitness, -1)
Exemple #12
0
 def test_select_proper_items_natural_false(self):
     population = Population()
     population.append(self.create_candidate(fitness=-4, is_natural=False))
     population.append(self.create_candidate(fitness=-3, is_natural=False))
     population.append(self.create_candidate(fitness=-2, is_natural=False))
     population.append(self.create_candidate(fitness=-1, is_natural=False))
     selection_size = 3
     self.random.float = MagicMock(return_value=0.99)
     results = self.obj.select(population, selection_size)
     for candidate in results:
         self.assertEqual(candidate.fitness, -1)
Exemple #13
0
 def test_select_proper_items(self):
     population = Population()
     population.append(self.create_candidate(fitness=1))
     population.append(self.create_candidate(fitness=2))
     population.append(self.create_candidate(fitness=3))
     population.append(self.create_candidate(fitness=4))
     selection_size = 2
     self.random.float = MagicMock(side_effect=[0.5, 0.9])
     results = self.obj.select(population, selection_size)
     self.assertEqual(results[0].fitness, 3)
     self.assertEqual(results[1].fitness, 4)
Exemple #14
0
 def test_should_terminate_not_natural(self):
     candidate = Candidate()
     candidate.fitness = Fitness(-50, is_natural=False)
     population = Population()
     population.append(candidate)
     termination_condition = TargetFitness(-100)
     self.assertEqual(termination_condition.should_terminate(population),
                      False)
     candidate.fitness = Fitness(-150, is_natural=False)
     self.assertEqual(termination_condition.should_terminate(population),
                      True)
 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)
Exemple #16
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)
 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)
Exemple #18
0
 def test_select_proper_items_natural_false(self):
     population = Population()
     population.append(self.create_candidate(fitness=-4))
     population.append(self.create_candidate(fitness=-3))
     population.append(self.create_candidate(fitness=-2))
     population.append(self.create_candidate(fitness=-1))
     selection_size = 2
     self.random.sample = MagicMock(
         side_effect=[[population[3], population[2]],
                      [population[2], population[1]]])
     results = self.obj.select(population, selection_size)
     self.assertEqual(results[0].fitness, -1)
     self.assertEqual(results[1].fitness, -2)
Exemple #19
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_logical_or(self):
        population = Population()
        population.append(Candidate())

        tc1 = TerminationCondition()
        tc1.should_terminate = MagicMock(return_value=True)
        tc2 = TerminationCondition()
        tc2.should_terminate = MagicMock(return_value=False)
        termination_condition = Multicondition(logic=Multicondition.LOGIC_OR)
        termination_condition.add(tc1)
        termination_condition.add(tc2)
        self.assertTrue(termination_condition.should_terminate(population))

        tc1.should_terminate = MagicMock(return_value=False)
        self.assertFalse(termination_condition.should_terminate(population))
 def test_should_terminate(self):
     candidate = Candidate()
     candidate.fitness = Fitness(1)
     population = Population()
     population.append(candidate)
     termination_condition = Stagnation(3)
     self.assertEqual(termination_condition.should_terminate(population),
                      False)
     self.assertEqual(termination_condition.should_terminate(population),
                      False)
     self.assertEqual(termination_condition.should_terminate(population),
                      True)
     candidate.fitness = Fitness(2)
     self.assertEqual(termination_condition.should_terminate(population),
                      False)
     self.assertEqual(termination_condition.should_terminate(population),
                      False)
     self.assertEqual(termination_condition.should_terminate(population),
                      True)
Exemple #22
0
 def test_select_result_type(self):
     population = Population()
     population.append(self.create_candidate(fitness=1))
     results = self.obj.select(population, 1)
     self.assertIsInstance(results, Population)
Exemple #23
0
 def create_population(self, size):
     population = Population()
     for _ in range(size):
         population.append(Candidate())
     return population