def test_halt_expected(self): """ Ensure the function returns true if we're in a halting state. """ halt = make_halt_function([6, 5]) g1 = Genome([6, 5]) g1.fitness = MAX_REWARD population = [g1, ] result = halt(population, 1) self.assertTrue(result)
def test_halt_expected(self): """ Ensure the function returns true if we're in a halting state. """ halt = make_halt_function([6, 5]) g1 = Genome([6, 5]) g1.fitness = MAX_REWARD population = [ g1, ] result = halt(population, 1) self.assertTrue(result)
def test_halt_not(self): """ Ensures if the fittest genome has fitness < MAX_REWARD then halt doesn't succeed. """ halt = make_halt_function([3, 2, 1]) g1 = Genome([1, 2, 3]) g1.fitness = MAX_REWARD - 0.1 g2 = Genome([1, 2, 3]) g2.fitness = 3 g3 = Genome([1, 2, 3]) g3.fitness = 2 # Any fittest solution with fitness < MAX_REWARD means no halt. population = [g1, g2, g3] result = halt(population, 1) self.assertFalse(result)
def test_halt_checks_suspension_count(self): """ If the solution contains suspensions the halt function should ensure that the MAX_REWARD is incremented by the number of suspensions (rewarded because they're part of a valid step wise motion). """ halt = make_halt_function([9, 8, 7, 6, 5]) g1 = Genome([11, 10, 9, 8, 7]) # only one our of two "correct" dissonances g1.fitness = MAX_REWARD + REWARD_SUSPENSION population = [g1, ] result = halt(population, 1) self.assertFalse(result) # Try again # two out of two "correct" dissonances g1.fitness = MAX_REWARD + (REWARD_SUSPENSION * 2) population = [g1, ] result = halt(population, 1) self.assertTrue(result)
def test_halt_checks_suspension_count(self): """ If the solution contains suspensions the halt function should ensure that the MAX_REWARD is incremented by the number of suspensions (rewarded because they're part of a valid step wise motion). """ halt = make_halt_function([9, 8, 7, 6, 5]) g1 = Genome([11, 10, 9, 8, 7]) # only one our of two "correct" dissonances g1.fitness = MAX_REWARD + REWARD_SUSPENSION population = [ g1, ] result = halt(population, 1) self.assertFalse(result) # Try again # two out of two "correct" dissonances g1.fitness = MAX_REWARD + (REWARD_SUSPENSION * 2) population = [ g1, ] result = halt(population, 1) self.assertTrue(result)
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))