Пример #1
0
    def test_best_challenge_changes_best_individual(self):
        """ Covers best challenge cases """
        self.config.mutation_probability = 0.0
        self.config.fitness_function_type = FitnessType.BASIC

        p = Population(self.samples, self.grammar, self.stats)
        i1 = Individual(self.samples,
                        self.grammar,
                        self.stats,
                        dna='00000000000000000000000000000000')
        i2 = Individual(self.samples,
                        self.grammar,
                        self.stats,
                        dna='01110101100101100110010110010101')

        # When there's no best individual yet, population's best individual is updated
        p.best_individual = None
        p.generation = [i2]
        p._best_challenge()

        super().assertEqual(p.best_individual, p.generation[0])

        # When a better individual is better fitted in a new generation, population's best individual is updated
        p.best_individual = i1
        p.generation = [i2]
        p._best_challenge()

        super().assertEqual(p.best_individual, p.generation[0])

        # When a worse individual is the most fitted in a new generation, population's best individual remains the same
        p.best_individual = i2
        p.generation = [i1]
        p._best_challenge()

        super().assertEqual(i2, p.best_individual)
Пример #2
0
 def test_mu_lambda_no_elite(self):
     """ Tests that replacement 'mu lambda without elitism' works as expected """
     self.config.replacement_type = ReplacementType.MU_LAMBDA_WITHOUT_ELITISM
     p = Population(self.samples, self.grammar, self.stats)
     mating_pool = p.selection(p.generation)
     p.offspring = p.recombination(mating_pool, p.generation)
     p.generation, p.offspring = p.replacement(p.generation, p.offspring)
     super().assertListEqual(p.offspring, [])
Пример #3
0
 def test_mu_plus_lambda(self):
     """ Tests that replacement 'mu plus lambda' works as expected """
     self.config.replacement_type = ReplacementType.MU_PLUS_LAMBDA
     p = Population(self.samples, self.grammar, self.stats)
     mating_pool = p.selection(p.generation)
     p.offspring = p.recombination(mating_pool, p.generation)
     p.generation, p.offspring = p.replacement(p.generation, p.offspring)
     super().assertListEqual(p.offspring, [])