Пример #1
0
 def test_returns_valid_genomes(self):
     """
     Checks the genomes returned by the create_population function are
     of the correct type.
     """
     result = create_population(1, CANTUS_FIRMUS)
     self.assertEqual(Genome, type(result[0]))
Пример #2
0
 def test_returns_valid_genomes(self):
     """
     Checks the genomes returned by the create_population function are
     of the correct type.
     """
     result = create_population(1, CANTUS_FIRMUS)
     self.assertEqual(Genome, type(result[0]))
Пример #3
0
 def test_solutions_have_correct_number_of_notes(self):
     """
     Ensures that all solutions have the expected number of notes.
     """
     result = create_population(20, CANTUS_FIRMUS)
     expected_length = len(CANTUS_FIRMUS)
     for genome in result:
         self.assertEqual(expected_length, len(genome.chromosome))
Пример #4
0
 def test_solutions_have_correct_number_of_notes(self):
     """
     Ensures that all solutions have the expected number of notes.
     """
     result = create_population(20, CANTUS_FIRMUS)
     expected_length = len(CANTUS_FIRMUS)
     for genome in result:
         self.assertEqual(expected_length, len(genome.chromosome))
Пример #5
0
 def test_uses_only_valid_intervals(self):
     """
     Tests that only valid consonant intervals are used.
     """
     valid_intervals = [2, 4, 5, 7, 9, 11]
     result = create_population(20, CANTUS_FIRMUS)
     for genome in result:
         for i in range(len(genome.chromosome)):
             contrapunctus_note = genome.chromosome[i]
             cantus_firmus_note = CANTUS_FIRMUS[i]
             interval = contrapunctus_note - cantus_firmus_note
             self.assertIn(interval, valid_intervals)
Пример #6
0
 def test_uses_only_valid_intervals(self):
     """
     Tests that only valid consonant intervals are used.
     """
     valid_intervals = [2, 4, 5, 7, 9, 11]
     result = create_population(20, CANTUS_FIRMUS)
     for genome in result:
         for i in range(len(genome.chromosome)):
             contrapunctus_note = genome.chromosome[i]
             cantus_firmus_note = CANTUS_FIRMUS[i]
             interval = contrapunctus_note - cantus_firmus_note
             self.assertIn(interval, valid_intervals)
Пример #7
0
 def test_returns_correct_number_of_genomes(self):
     """
     Ensures the correct number of genomes are returned by the function.
     """
     result = create_population(100, CANTUS_FIRMUS)
     self.assertEqual(100, len(result))
Пример #8
0
            mutation_rate, cf)
        halt_function = second.make_halt_function(cf)
    elif species == 3:
        population_size = third.DEFAULT_POPULATION_SIZE
        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]
Пример #9
0
 def test_returns_correct_number_of_genomes(self):
     """
     Ensures the correct number of genomes are returned by the function.
     """
     result = create_population(100, CANTUS_FIRMUS)
     self.assertEqual(100, len(result))