Ejemplo n.º 1
0
 def test_yields_starting_population(self):
     """
     Ensures that the first item yielded *is* the starting population.
     """
     start_pop = [3, 2, 1]
     ga = genetic_algorithm(start_pop, None, None, None)
     self.assertEqual(ga.next(), start_pop,
                      "Starting population not yielded as first result.")
Ejemplo n.º 2
0
 def test_fitness_called_for_starting_population(self):
     """
     Ensures the fitness function is called on each initial genome before
     yielding the starting population.
     """
     start_pop = [1, 2, 3]
     mock_fitness = MagicMock(return_value=None)
     ga = genetic_algorithm(start_pop, mock_fitness, None, None)
     result = ga.next()
     self.assertEqual(len(start_pop), mock_fitness.call_count,
                      "Fitness function not called for each genome.")
Ejemplo n.º 3
0
    def test_generate(self):
        """
        Ensures that the generate iterable is used to create new generations.
        """
        start_pop = [1, 2, 3]

        def fitness(genome):
            """
            For illustrative purposes.
            """
            return genome

        def halt(population, generation_count):
            """
            Halts the algorithm after ten populations.
            """
            return generation_count == 10

        class Generate(object):
            """
            An iterable for producing new offspring.
            """
            def __init__(self, parent_population):
                """
                @param parent_population: the parent genomes.
                """
                self.counter = 0
                self.size = len(parent_population)
                self.parents = parent_population

            def __iter__(self):
                return self

            def __next__(self):
                """
                For testing purposes only.
                """
                if self.counter < self.size:
                    pos, self.counter = self.counter, self.counter + 1
                    return self.parents[pos] + 1
                else:
                    raise StopIteration()

        ga = genetic_algorithm(start_pop, fitness, Generate, halt)
        actual = [p for p in ga]
        expected = [[3 + i, 2 + i, 1 + i] for i in range(10)]
        self.assertEqual(actual, expected,
                         "Actual: %r Expected: %r" % (actual, expected))
Ejemplo n.º 4
0
    def test_starting_population_fitness_ordered(self):
        """
        Ensures that the starting population as returned by the GA is ordered
        according to the fitness function.
        """
        start_pop = [1, 2, 3]

        def fitness(genome):
            """
            For illustrative purposes.
            """
            return genome * genome

        ga = genetic_algorithm(start_pop, fitness, None, None)
        expected = [3, 2, 1]
        actual = next(ga)
        self.assertEqual(actual, expected,
                         "Actual: %r Expected: %r" % (actual, expected))
Ejemplo n.º 5
0
    def test_halt_function(self):
        """
        Ensures that the halt function is called for all subsequent yields
        after yielding the starting population.
        """
        start_pop = [1, 2, 3]

        def fitness(genome):
            """
            For illustrative purposes.
            """
            return genome

        def halt(population, generation_count):
            """
            Halts the algorithm after ten populations.
            """
            return generation_count == 10

        class Generate(object):
            """
            As simple as possible. See generate related test below.
            """
            def __init__(self, parents):
                pass

            def __iter__(self):
                return self

            def __next__(self):
                raise StopIteration()

        ga = genetic_algorithm(start_pop, fitness, Generate, halt)
        result = [p for p in ga]
        actual = len(result)
        expected = 10
        self.assertEqual(actual, expected,
                         "Actual: %r Expected: %r" % (actual, expected))
Ejemplo n.º 6
0
        mutation_range = third.DEFAULT_MUTATION_RANGE
        mutation_rate = third.DEFAULT_MUTATION_RATE
        start_population = third.create_population(population_size, cf)
        fitness_function = third.make_fitness_function(cf)
        generate_function = third.make_generate_function(mutation_range,
            mutation_rate, cf)
        halt_function = third.make_halt_function(cf)
    elif species == 4:
        population_size = fourth.DEFAULT_POPULATION_SIZE
        mutation_range = fourth.DEFAULT_MUTATION_RANGE
        mutation_rate = fourth.DEFAULT_MUTATION_RATE
        start_population = fourth.create_population(population_size, cf)
        fitness_function = fourth.make_fitness_function(cf)
        generate_function = fourth.make_generate_function(mutation_range,
            mutation_rate, cf)
        halt_function = fourth.make_halt_function(cf)

    ga = ga.genetic_algorithm(start_population, fitness_function,
        generate_function, halt_function)
    fitness = 0.0

    counter = 0
    for generation in ga:
        counter += 1
        fitness = generation[0].fitness
        print "--- Generation %d ---" % counter
        print generation[0]
        print fitness
    with open('%s.ly' % output, 'w') as output:
        output.write(lilypond.render(species, cf, generation[0].chromosome))