Esempio n. 1
0
 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)
Esempio n. 2
0
 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)
Esempio n. 3
0
 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)
Esempio n. 4
0
 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)
Esempio n. 5
0
 def test_mutate_bounded_by_arg_values(self):
     """
     A rather contrived test but it proves that both the mutation_range and
     mutation_rate are used correctly given the context given by a cantus
     firmus.
     """
     cantus_firmus = [1, 1, 1, 1, 1]
     # mutate every time.
     mutation_rate = 1
     # will always mutate to thirds above the cf note.
     mutation_range = 2
     genome = Genome([5, 6, 7, 8, 9])
     genome.mutate(mutation_range, mutation_rate, cantus_firmus)
     self.assertEqual([3, 3, 3, 3, 3], genome.chromosome)
Esempio n. 6
0
 def test_mutate_bounded_by_arg_values(self):
     """
     A rather contrived test but it proves that both the mutation_range and
     mutation_rate are used correctly given the context given by a cantus
     firmus.
     """
     cantus_firmus = [1, 1, 1, 1, 1]
     # mutate every time.
     mutation_rate = 1
     # will always mutate to thirds above the cf note.
     mutation_range = 2
     genome = Genome([5, 6, 7, 8, 9])
     genome.mutate(mutation_range, mutation_rate, cantus_firmus)
     self.assertEqual([3, 3, 3, 3, 3], genome.chromosome)
Esempio n. 7
0
 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))
Esempio n. 8
0
 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)
Esempio n. 9
0
 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)
Esempio n. 10
0
 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)
Esempio n. 11
0
 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)
Esempio n. 12
0
 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)
Esempio n. 13
0
 def test_mutate_is_implemented(self):
     """
     Ensures that we have a mutate method implemented.
     """
     genome = Genome([1, 2, 3])
     self.assertNotEqual(NotImplemented, genome.mutate(2, 0.2, [1, 2, 3]))
Esempio n. 14
0
 def test_mutate_is_implemented(self):
     """
     Ensures that we have a mutate method implemented.
     """
     genome = Genome([1, 2, 3])
     self.assertNotEqual(NotImplemented, genome.mutate(2, 0.2, [1, 2, 3]))