def test_adjust_population_fitness(self): msg = 'Adjusted fitness incorrectly!' e = Ecosystem(threshold=0.5, dc=1.0, ec=1.0, wc=0.4) # Create genomes g = Genome(2, 2) g2 = Genome(2, 2) g3 = Genome(2, 2) # Add connections and/or nodes g.add_connection(0, 2, weight=1.0) g.add_connection(0, 3, weight=1.0) g.add_connection(1, 2, weight=1.0) g2.add_connection(0, 2, weight=0.9) g2.add_connection(0, 3, weight=1.0) g2.add_connection(1, 2, weight=1.0) g2.add_connection(1, 3, weight=1.0) g3.add_connection(0, 2, weight=0.1) g3.add_connection(0, 3, weight=1.0) g3.add_connection(1, 2, weight=1.0) g3.add_connection(1, 3, weight=1.0) g3.add_node(0) g3.add_node(1) g3.add_node(2) g3.add_connection(0, 6, weight=1.0) g3.add_connection(1, 5, weight=1.0) g4 = copy.copy(g3) g4.add_node(3) # Set genome fitness g.fitness = 10 g2.fitness = 12 g3.fitness = 22 g4.fitness = 24 # Test to make sure it doesn't change if there are no different genomes e.add_genome(g) e.adjust_population_fitness() self.assertEqual(g.fitness, 10, msg) e.add_genome(g2) e.adjust_population_fitness() self.assertEqual(g.fitness, 10, msg) self.assertEqual(g2.fitness, 12, msg) # Test to make sure the fitness does change when there are different genomes e.add_genome(g3) e.add_genome(g4) e.adjust_population_fitness() self.assertEqual(g.fitness, 5, msg) self.assertEqual(g2.fitness, 6, msg) self.assertEqual(g3.fitness, 11, msg) self.assertEqual(g4.fitness, 12, msg)
def test_cross(self): e = Ecosystem() # Create genomes g = Genome(2, 2, ecosystem=e) g2 = Genome(2, 2, ecosystem=e) # Cross the genomes child = e.cross(g, g2) # Test child connections msg = 'Child connection doesn\'t exist within either parent!' for c in child.get_connections(): self.assertTrue( g.get_connection(c.innovation_number) is not None or g2.get_connection(c.innovation_number) is not None, msg) # Test to make sure the child has the same amount of connections as the fitter parent msg = 'Child missing fitter parent connection(s)!' self.assertEqual(len(child.get_connections()), len(g.get_connections()), msg) # Test child nodes msg = 'Child node doesn\'t exist within either parent!' for n in child.get_nodes(): self.assertTrue( g.get_node(n.id) is not None or g2.get_node(n.id) is not None, msg) # Test to make sure the child has the same amount of nodes as the fitter parent msg = 'Child is missing fitter parent node(s)!' self.assertEqual(len(child.get_nodes()), len(g.get_nodes()), msg) # Test preference for fit parents msg = 'Child connection preferred less fit parent!' for c in child.get_connections(): in_both = g.get_connection( c.innovation_number) is not None and g2.get_connection( c.innovation_number) is not None in_fit_parent = g.get_connection( c.innovation_number) is not None and g2.get_connection( c.innovation_number) is None self.assertTrue(in_both or in_fit_parent, msg) # Add connections and nodes g.add_connection(0, 2) g.add_connection(0, 3) g.add_connection(1, 2) g.add_connection(1, 3) g.add_node(0) g.get_connections()[5].weight = 0.4 g2.add_connection(0, 2) g2.add_connection(0, 3) g2.add_connection(1, 2) g2.add_connection(1, 3) g2.add_node(1) g.add_node(2) # Assign fitness to genomes g.fitness = 10 g2.fitness = 5 # Cross the genomes child = e.cross(g, g2) # Test child connections msg = 'Child connection doesn\'t exist within either parent!' for c in child.get_connections(): self.assertTrue( g.get_connection(c.innovation_number) is not None or g2.get_connection(c.innovation_number) is not None, msg) # Test to make sure the child has the same amount of connections as the fitter parent msg = 'Child missing fitter parent connection(s)!' self.assertEqual(len(child.get_connections()), len(g.get_connections()), msg) # Test child nodes msg = 'Child node doesn\'t exist within either parent!' for n in child.get_nodes(): self.assertTrue( g.get_node(n.id) is not None or g2.get_node(n.id) is not None, msg) # Test to make sure the child has the same amount of nodes as the fitter parent msg = 'Child is missing fitter parent node(s)!' self.assertEqual(len(child.get_nodes()), len(g.get_nodes()), msg) # Test preference for fit parents msg = 'Child connection preferred less fit parent!' for c in child.get_connections(): in_both = g.get_connection( c.innovation_number) is not None and g2.get_connection( c.innovation_number) is not None in_fit_parent = g.get_connection( c.innovation_number) is not None and g2.get_connection( c.innovation_number) is None self.assertTrue(in_both or in_fit_parent, msg) # Swap the fitness and test again g.fitness = 5 g2.fitness = 10 # Cross the genomes child = e.cross(g, g2) # Test child connections msg = 'Child connection doesn\'t exist within either parent!' for c in child.get_connections(): self.assertTrue( g.get_connection(c.innovation_number) is not None or g2.get_connection(c.innovation_number) is not None, msg) # Test to make sure the child has the same amount of connections as the fitter parent msg = 'Child missing fitter parent connection(s)!' self.assertEqual(len(child.get_connections()), len(g2.get_connections()), msg) # Test child nodes msg = 'Child node doesn\'t exist within either parent!' for n in child.get_nodes(): self.assertTrue( g.get_node(n.id) is not None or g2.get_node(n.id) is not None, msg) # Test to make sure the child has the same amount of nodes as the fitter parent msg = 'Child is missing fitter parent node(s)!' self.assertEqual(len(child.get_nodes()), len(g2.get_nodes()), msg) # Test preference for fit parents msg = 'Child connection preferred less fit parent!' for c in child.get_connections(): in_both = g.get_connection( c.innovation_number) is not None and g2.get_connection( c.innovation_number) is not None in_fit_parent = g.get_connection( c.innovation_number) is None and g2.get_connection( c.innovation_number) is not None self.assertTrue(in_both or in_fit_parent, msg)