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, 6, 6, 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, 6, 6, 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_dissonance_count(self): """ If the solution contains dissonances the halt function should ensure that the MAX_REWARD is incremented by the number of dissonances (rewarded because they're part of a valid step wise motion). """ halt = make_halt_function([6, 5]) g1 = Genome([8, 9, 9, 11, 12]) # only one our of two "correct" dissonances g1.fitness = MAX_REWARD + REWARD_STEPWISE_MOTION population = [g1, ] result = halt(population, 1) self.assertFalse(result) # Try again # two out of two "correct" dissonances g1.fitness = MAX_REWARD + (REWARD_STEPWISE_MOTION * 2) population = [g1, ] result = halt(population, 1) self.assertTrue(result)
def test_halt_checks_dissonance_count(self): """ If the solution contains dissonances the halt function should ensure that the MAX_REWARD is incremented by the number of dissonances (rewarded because they're part of a valid step wise motion). """ halt = make_halt_function([6, 5]) g1 = Genome([8, 9, 9, 11, 12]) # only one our of two "correct" dissonances g1.fitness = MAX_REWARD + REWARD_STEPWISE_MOTION population = [ g1, ] result = halt(population, 1) self.assertFalse(result) # Try again # two out of two "correct" dissonances g1.fitness = MAX_REWARD + (REWARD_STEPWISE_MOTION * 2) population = [ g1, ] result = halt(population, 1) self.assertTrue(result)
mutation_range = second.DEFAULT_MUTATION_RANGE mutation_rate = second.DEFAULT_MUTATION_RATE start_population = second.create_population(population_size, cf) fitness_function = second.make_fitness_function(cf) generate_function = first.make_generate_function(mutation_range, 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