コード例 #1
0
    def generate(seed_generation):
        """
        Given a seed generation will return a new generation of candidate
        solutions assuming the cantus_firmus and other settings in the closure.
        """
        length = len(seed_generation)
        # Keep the fittest 50%
        new_generation = seed_generation[:length / 2]

        # Breed the remaining 50% using roulette wheel selection
        offspring = []
        while len(offspring) < length / 2:
            mum = ga.roulette_wheel_selection(seed_generation)
            dad = ga.roulette_wheel_selection(seed_generation)
            children = mum.breed(dad)
            offspring.extend(children)

        # Mutate
        for genome in offspring:
            genome.mutate(mutation_range, mutation_rate, cantus_firmus)

        # Ensure the new generation is the right length
        new_generation.extend(offspring)
        new_generation = new_generation[:length]

        return new_generation
コード例 #2
0
ファイル: utils.py プロジェクト: GitBruno/foox
    def generate(seed_generation):
        """
        Given a seed generation will return a new generation of candidate
        solutions assuming the cantus_firmus and other settings in the closure.
        """
        length = len(seed_generation)
        # Keep the fittest 50%
        new_generation = seed_generation[:length/2]

        # Breed the remaining 50% using roulette wheel selection
        offspring = []
        while len(offspring) < length/2:
            mum = ga.roulette_wheel_selection(seed_generation)
            dad = ga.roulette_wheel_selection(seed_generation)
            children = mum.breed(dad)
            offspring.extend(children)

        # Mutate
        for genome in offspring:
            genome.mutate(mutation_range, mutation_rate, cantus_firmus)

        # Ensure the new generation is the right length
        new_generation.extend(offspring)
        new_generation = new_generation[:length]

        return new_generation
コード例 #3
0
    def test_returns_genome(self):
        """
        Ensures that the roulette wheel returns a selected genome.
        """
        class Genome(object):
            """
            A simple representation of a genome.
            """
            def __init__(self, fitness):
                self.fitness = fitness

        population = [Genome(random.uniform(0.1, 10.0)) for i in range(4)]
        result = roulette_wheel_selection(population)
        self.assertIsInstance(
            result, Genome,
            "Expected result of roulette_wheel_selection is not a Genome")
コード例 #4
0
    def test_returns_genome_with_unfit_population(self):
        """
        Ensures that the roulette wheel returns a randomly selected genome if
        none of them have a positive fitness score.
        """
        class Genome(object):
            """
            A simple representation of a genome.
            """
            def __init__(self, fitness):
                self.fitness = fitness

        population = [Genome(0.0) for i in range(4)]
        result = roulette_wheel_selection(population)
        self.assertIsInstance(
            result, Genome,
            "Expected result of roulette_wheel_selection is not a Genome")