Example #1
0
 def test_new_population(self):
     ''' Make sure population can clone a new population. '''
     population = GAPopulation(indv_template=self.indv_template, size=10)
     population.init()
     new_population = population.new()
     self.assertEqual(new_population.size, 10)
     self.assertListEqual(new_population.individuals, [])
Example #2
0
    def test_all_fits(self):
        population = GAPopulation(indv_template=self.indv_template, size=10)
        population.init()
        all_fits = population.all_fits(fitness=self.fitness)

        self.assertEqual(len(all_fits), 10)

        for fit in all_fits:
            self.assertTrue(type(fit) is float)
    def test_selection(self):
        indv = GAIndividual(ranges=[(0, 30)])
        p = GAPopulation(indv)
        p.init()

        selection = TournamentSelection()
        father, mother = selection.select(p, fitness=self.fitness)

        self.assertTrue(isinstance(father, GAIndividual))
        self.assertTrue(isinstance(mother, GAIndividual))
Example #4
0
    def test_selection(self):
        indv = GAIndividual(ranges=[(0, 30)])
        p = GAPopulation(indv)
        p.init()

        selection = LinearRankingSelection()
        father, mother = selection.select(p, fitness=self.fitness)

        self.assertTrue(isinstance(father, GAIndividual))
        self.assertTrue(isinstance(mother, GAIndividual))
        self.assertNotEqual(father.chromsome, mother.chromsome)
Example #5
0
    def test_initialization(self):
        ''' Make sure a population can be initialized correctly. '''
        population = GAPopulation(indv_template=self.indv_template, size=10)

        self.assertListEqual(population.individuals, [])

        population.init()
        self.assertEqual(len(population.individuals), 10)

        # Check individual.
        self.assertTrue(isinstance(population[0], GAIndividual))