예제 #1
0
    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)
예제 #2
0
    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