def test_fitness_function_returns_float(self): """ Makes sure the generated fitness function returns a fitness score as a float. """ fitness_function = make_fitness_function(CANTUS_FIRMUS) genome = Genome([1, 2, 3]) result = fitness_function(genome) self.assertTrue(float, type(result))
def test_fitness_function_uses_cached_genome_fitness(self): """ Ensures the fitness function bails if there is already a score set for the genome. """ fitness_function = make_fitness_function(CANTUS_FIRMUS) genome = Genome([1, 2, 3]) genome.fitness = 12345 result = fitness_function(genome) self.assertEqual(12345, result)
def test_fitness_function_sets_fitness_on_genome(self): """ Ensures the fitness score is set in the genome's fitness attribute and is the same as the returned fitness score. """ fitness_function = make_fitness_function(CANTUS_FIRMUS) genome = Genome([1, 2, 3]) self.assertEqual(None, genome.fitness) result = fitness_function(genome) self.assertNotEqual(None, genome.fitness) self.assertEqual(result, genome.fitness)
def test_make_fitness_function_returns_callable(self): """ Ensures the make_fitness_function returns a callable. """ result = make_fitness_function(CANTUS_FIRMUS) self.assertTrue(callable(result))
halt_function = first.halt elif species == 2: population_size = second.DEFAULT_POPULATION_SIZE 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)