def test_add_node(self): g = Genome(1, 1) # Test adding nodes msg = 'Node added incorrectly!' g.add_connection(0, 1) g.add_node(0) self.assertEqual(len(g.get_nodes()), 3, msg) self.assertEqual(len(g.get_connections()), 3, msg) self.assertFalse(g.get_connections()[0].expressed, msg) self.assertTrue(g.get_connections()[1].expressed, msg) self.assertTrue(g.get_connections()[2].expressed, msg) self.assertEqual(g.get_connections()[0].weight, g.get_connections()[1].weight, msg) self.assertEqual(g.get_connections()[2].weight, 1.0, msg) self.assertEqual(g.get_nodes()[2].activation, activations.modified_sigmoid, msg) g.add_node(1, activation=activations.absolute) self.assertEqual(len(g.get_nodes()), 4, msg) self.assertEqual(len(g.get_connections()), 5, msg) self.assertFalse(g.get_connections()[1].expressed, msg) self.assertTrue(g.get_connections()[3].expressed, msg) self.assertTrue(g.get_connections()[4].expressed, msg) self.assertEqual(g.get_connections()[1].weight, g.get_connections()[3].weight, msg) self.assertEqual(g.get_connections()[4].weight, 1.0, msg) self.assertEqual(g.get_nodes()[3].activation, activations.absolute, msg)
def test_set_connections(self): g = Genome(2, 2) # Test to make sure connections are set correctly msg = 'Connections set incorrectly!' connections = [ Connection(0, 0, 2), Connection(1, 0, 3), Connection(2, 1, 2), Connection(3, 1, 3) ] g.set_connections(connections) self.assertEqual(g.get_connections(), connections, msg) # Make sure weights were compiled correctly msg = 'Weights compiled incorrectly!' self.assertEqual(g.weights.shape[0], len(g.get_nodes()) - g.shape[0], msg=msg) self.assertEqual(g.weights.shape[1], len(g.get_nodes()), msg=msg) for i in range(len(g.weights)): for j in range(len(g.weights)): nid = g._Genome__order[i] conn = g.get_connection_from_nodes(j, nid) if conn is not None: self.assertEqual(conn.weight, g.weights[i, j], msg) else: self.assertEqual(g.weights[i, j], 0.0, msg)
def test_mutate_add_node(self): g = Genome(1, 1) # Test to make sure you can't add a node without an existing connection msg = 'Node added without existing connection!' g.mutate_add_node() self.assertEqual(len(g.get_nodes()), 2, msg) # Test adding a node msg = 'Node added incorrectly!' g.mutate_add_connection() g.mutate_add_node() self.assertEqual(len(g.get_nodes()), 3, msg) self.assertEqual(g.get_nodes()[2].id, 2, msg) self.assertEqual(g.get_nodes()[2].type, 'hidden', msg) self.assertEqual(len(g.get_connections()), 3, msg) self.assertEqual(g.get_connections()[0].weight, g.get_connections()[1].weight, msg) self.assertEqual(g.get_connections()[2].weight, 1.0, msg) # Test to make sure you can't add a node without any expressed connections msg = "Node added to disabled connection!" for c in g.get_connections(): c.disable() g.mutate_add_node() self.assertEqual(len(g.get_nodes()), 3, msg)
def test_copy(self): g = Genome(2, 2) # Test to make sure the copy is the same as the original msg = 'Copy is different from the original!' g.add_connection(0, 2) g.add_connection(0, 3) g.add_node(0) gc = g.copy() self.assertEqual(len(g.get_nodes()), len(gc.get_nodes()), msg) self.assertEqual(len(g.get_connections()), len(gc.get_connections()), msg) for i in range(len(g.get_connections())): self.assertEqual(g.get_connections()[i].weight, gc.get_connections()[i].weight, msg) # Test to make sure the copy doesn't change when the original does msg = 'Copy changes when original changes!' g.add_node(1) g.get_connections()[0].weight = 50 self.assertNotEqual(len(g.get_nodes()), len(gc.get_nodes()), msg) self.assertNotEqual(len(g.get_connections()), len(gc.get_connections()), msg) self.assertNotEqual(g.get_connections()[0].weight, gc.get_connections()[0].weight, msg)
def test_get_node_max_distance(self): g = Genome(2, 2) # Test to make sure empty genomes return the correct values for output nodes msg = 'Disconnected output node returned invalid value!' self.assertEqual(g.get_node_max_distance(g.get_node(2).id), -1, msg) self.assertEqual(g.get_node_max_distance(g.get_node(3).id), -1, msg) # Add nodes and connections g.add_connection(0, 2, weight=-0.7) g.add_connection(0, 3, weight=-0.1) g.add_connection(1, 2, weight=0.5) g.add_connection(1, 3, weight=0.9) g.add_node(0) g.add_node(2) g.add_connection(4, 5, 0.5) msg = 'Incorrect node max distance!' # Test the values of each node distance to make sure they are correct correct_distances = [0, 0, 3, 1, 1, 2] for node, distance in zip(g.get_nodes(), correct_distances): self.assertEqual(g.get_node_max_distance(node.id), distance, msg) # Add a node and test again g.add_node(8) correct_distances = [0, 0, 4, 1, 1, 3, 2] for node, distance in zip(g.get_nodes(), correct_distances): self.assertEqual(g.get_node_max_distance(node.id), distance, msg) # Add connection and test again g.add_connection(6, 3) correct_distances = [0, 0, 4, 3, 1, 3, 2] for node, distance in zip(g.get_nodes(), correct_distances): self.assertEqual(g.get_node_max_distance(node.id), distance, msg) # Test genome with connection loop msg = 'Genome failed to handle connection loop!' g2 = Genome(1, 1) g2.add_connection(0, 1) g2.add_node(0) g2.add_node(0) g2.add_node(1) g2.add_connection(3, 2) g2.add_connection(4, 3) correct_distances = [0, 4, 3, 3, 3] for node, distance in zip(g2.get_nodes(), correct_distances): self.assertEqual(g2.get_node_max_distance(node.id), distance, msg)
def test_constructor(self): input_size = 2 output_size = 2 g = Genome(input_size, output_size) # Test if attributes are correct msg = 'Failed to assign genome attributes correctly!' self.assertEqual(len(g.get_nodes()), input_size + output_size, msg) self.assertEqual(len(g.get_connections()), 0) self.assertEqual(g.fitness, 0)
def test_set_activation(self): g = Genome(3, 3) node_ids = range(len(g.get_nodes())) act = [activations.relu, activations.gaussian, activations.square] # Test to make sure the activation is set correctly msg = 'Activation set incorrectly!' for nid in node_ids: for a in act: g.set_activation(nid, a) self.assertEqual(g.get_node(nid).activation, a)
def test_mutate_add_connection(self): # Test adding a connection msg = 'Connection added incorrectly!' g = Genome(2, 2) for i in range(30): g = Genome(2, 2) g.mutate_add_connection() self.assertEqual(len(g.get_connections()), 1, msg) self.assertEqual(g.get_connections()[0].innovation_number, 0, msg) self.assertTrue( g.get_connections()[0].in_node in [n.id for n in g.get_nodes()], msg) self.assertTrue( g.get_connections()[0].out_node in [n.id for n in g.get_nodes()], msg) self.assertTrue(-1.0 <= g.get_connections()[0].weight <= 1.0, msg) self.assertTrue(g.get_connections()[0].expressed, msg) self.assertNotEqual(g.get_connections()[0].in_node, g.get_connections()[0].out_node) in_type = g.get_node(g.get_connections()[0].in_node).type out_type = g.get_node(g.get_connections()[0].out_node).type self.assertFalse(in_type == out_type != 'hidden') self.assertFalse((in_type == 'output' and out_type == 'input')) self.assertFalse((in_type == 'hidden' and out_type == 'input')) # Test to make sure connections are always added (unless at max) msg = 'Connection not added!' for i in range(2, 4): g.mutate_add_connection() self.assertEqual(len(g.get_connections()), i, msg) # Test to make sure it doesn't go above the maximum connections msg = 'Connections exceeded maximum amount!' g.mutate_add_connection() self.assertEqual(len(g.get_connections()), 4, msg) # Shouldn't go past 4
def test_set_nodes(self): g = Genome(1, 1) # Test to make sure nodes are set correctly msg = 'Nodes set incorrectly!' nodes = [ Node(0, 'input'), Node(1, 'input'), Node(2, 'output'), Node(3, 'output'), Node(4, 'hidden') ] g.set_nodes(nodes) self.assertEqual(g.get_nodes(), nodes, msg) # Make sure weights were compiled correctly msg = 'Weights compiled incorrectly!' self.assertEqual(g.weights.shape[0], len(nodes) - g.shape[0], msg=msg) self.assertEqual(g.weights.shape[1], len(nodes), msg=msg)
def test_forward(self): error_margin = 0.000000000001 g = Genome(2, 2) x = np.array([0.5, 0.5]) msg = 'Invalid genome output!' # No connections y = g(x) self.assertEqual(y[0], 0.0, msg=msg) self.assertEqual(y[1], 0.0, msg=msg) # No hidden nodes, modified sigmoid activation g.add_connection(0, 2, weight=-0.7) g.add_connection(0, 3, weight=-0.1) g.add_connection(1, 2, weight=0.5) g.add_connection(1, 3, weight=0.9) y = g(x) self.assertAlmostEqual(y[0], 0.3798935676569099, msg=msg, delta=error_margin) self.assertAlmostEqual(y[1], 0.8765329524347759, msg=msg, delta=error_margin) # Different activation for n in g.get_nodes(): if n.type != 'input': g.set_activation(n.id, activations.absolute) y = g(x) self.assertAlmostEqual(y[0], 0.1, msg=msg, delta=error_margin) self.assertAlmostEqual(y[1], 0.4, msg=msg, delta=error_margin) # With hidden nodes and different activations (sigmoid for the new ones) g.add_node(0, activation=activations.sigmoid) g.add_node(2, activation=activations.sigmoid) g.add_connection(4, 5, 0.5) y = g(x) self.assertAlmostEqual(y[0], 0.08953579350695234, msg=msg, delta=error_margin) self.assertAlmostEqual(y[1], 0.4, msg=msg, delta=error_margin) # Test with many hidden nodes in a line g2 = Genome(2, 2) g2.add_connection(0, 2, -0.3) g2.add_node(0) g2.add_node(2) g2.add_node(4) g2.add_node(6) g2.add_connection(1, 7, 0.7) g2.add_connection(4, 3, -0.2) y = g2(x) self.assertAlmostEqual(y[0], 0.18866305913528142, msg=msg, delta=error_margin) self.assertAlmostEqual(y[1], 0.2743863603871294, msg=msg, delta=error_margin) # Test with recursive loop g3 = Genome(1, 1) x = np.array([0.5]) g3.add_connection(0, 1, 0.5) g3.add_node(0) g3.add_node(0) g3.add_node(1) g3.add_connection(3, 2, 0.5) g3.add_connection(4, 3, 0.5) y = g3(x) self.assertAlmostEqual(y[0], 0.9907948306148218, msg=msg, delta=error_margin)
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)