示例#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)
示例#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)
示例#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, 5])
     g1.fitness = MAX_REWARD
     population = [g1, ]
     result = halt(population, 1)
     self.assertTrue(result)
示例#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, 5])
     g1.fitness = MAX_REWARD
     population = [
         g1,
     ]
     result = halt(population, 1)
     self.assertTrue(result)
示例#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)
示例#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)
示例#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))
示例#8
0
 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)
示例#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)
示例#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)
示例#11
0
 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)
示例#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)
示例#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]))
示例#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]))