Beispiel #1
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)
Beispiel #2
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)
Beispiel #3
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)
 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])
 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)
 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)
Beispiel #7
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)
Beispiel #8
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)
Beispiel #9
0
 def test_trigger(self):
     candidate = Candidate()
     candidate.fitness = Fitness(1)
     population = Population([candidate])
     observer = ConsoleObserver()
     observer.trigger(Event(Event.INITIALIZE, {'population': population}))
     observer.trigger(
         Event(Event.EVALUATED_POPULATION, {
             'generation': 1,
             'population': population
         }))
     observer.trigger(
         Event(Event.TERMINATE, {
             'generation': 1,
             'population': population
         }))
Beispiel #10
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)
 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)
Beispiel #12
0
 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))
 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)
Beispiel #14
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)
Beispiel #15
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)
    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)
    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_apply(self, strings, random, points):
     candidate1 = Candidate()
     candidate1.data = strings[0][0]
     candidate2 = Candidate()
     candidate2.data = strings[0][1]
     crossover_points = points
     crossover_operator = ListCrossover(Probability(1), random, crossover_points)
     result = crossover_operator.mate(candidate1, candidate2)
     self.assertEqual(len(result), 2)
     self.assertEqual(result[0].data, strings[1][0])
     self.assertEqual(result[1].data, strings[1][1])
Beispiel #19
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])
Beispiel #20
0
 def create_candidate(self, fitness=None, is_natural=True):
     candidate = Candidate()
     candidate.fitness = Fitness(fitness, is_natural=is_natural)
     return candidate
Beispiel #21
0
 def create_population(self, size):
     population = Population()
     for _ in range(size):
         population.append(Candidate())
     return population
Beispiel #22
0
 def test_init(self):
     candidate = Candidate()
     self.assertTrue(str(candidate))