Esempio n. 1
0
 def test_get_best_not_natural(self):
     candidate1 = Candidate()
     candidate1.fitness = Fitness(-1, is_natural=False)
     candidate2 = Candidate()
     candidate2.fitness = Fitness(-2, is_natural=False)
     result = Population([candidate1, candidate2]).get_best()
     self.assertEqual(result, candidate2)
     result = Population([candidate2, candidate1]).get_best()
     self.assertEqual(result, candidate2)
Esempio n. 2
0
 def test_sort_by_fitness_not_natural(self):
     candidate1 = Candidate()
     candidate1.fitness = Fitness(-1, is_natural=False)
     candidate2 = Candidate()
     candidate2.fitness = Fitness(-2, is_natural=False)
     population = Population([candidate2, candidate1])
     population.sort_by_fitness()
     self.assertEqual(population[0], candidate1)
     self.assertEqual(population[1], candidate2)
Esempio n. 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)
Esempio n. 4
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)
Esempio n. 5
0
 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)
Esempio n. 6
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)
Esempio n. 7
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
         }))
Esempio n. 8
0
 def create_candidate(self, fitness=None, is_natural=True):
     candidate = Candidate()
     candidate.fitness = Fitness(fitness, is_natural=is_natural)
     return candidate
Esempio n. 9
0
 def test_init(self):
     self.assertEqual(str(Fitness(123)), '123')
     self.assertEqual(str(Fitness(-123, is_natural=False)), '-123')
     self.assertEqual(int(Fitness(123)), 123)
     self.assertEqual(float(Fitness(123.5)), 123.5)
Esempio n. 10
0
    def test_logic(self):
        self.assertTrue(Fitness(123))
        self.assertTrue(Fitness(-123))
        self.assertTrue(Fitness(0))
        self.assertFalse(Fitness(None))

        self.assertTrue(Fitness(123) == 123)
        self.assertFalse(Fitness(123) == 122)

        self.assertTrue(Fitness(123) != 122)
        self.assertFalse(Fitness(123) != 123)

        self.assertTrue(Fitness(123) < 124)
        self.assertFalse(Fitness(123) < 123)
        self.assertFalse(Fitness(123) < 122)

        self.assertTrue(Fitness(123) <= 124)
        self.assertTrue(Fitness(123) <= 123)
        self.assertFalse(Fitness(123) <= 122)

        self.assertTrue(Fitness(123) > 122)
        self.assertFalse(Fitness(123) > 123)
        self.assertFalse(Fitness(123) > 124)

        self.assertTrue(Fitness(123) >= 122)
        self.assertTrue(Fitness(123) >= 123)
        self.assertFalse(Fitness(123) >= 124)
Esempio n. 11
0
 def test_math(self):
     self.assertEqual(Fitness(123) + 1, 124)
     self.assertEqual(Fitness(123) - 1, 122)
     self.assertEqual(Fitness(123) * 2, 246)
     self.assertEqual(abs(Fitness(-123)), 123)