Exemplo n.º 1
0
    def test_align_genome(self):
        g1 = Genome(3, 1)
        g1.create_node(Node.TYPE_HIDDEN, 5)
        g1.connect_nodes_by_id(1, 4, 1)
        g1.connect_nodes_by_id(2, 4, 2)
        g1.connect_nodes_by_id(3, 4, 3)
        g1.connect_nodes_by_id(2, 5, 4)
        g1.connect_nodes_by_id(5, 4, 5)
        g1.connect_nodes_by_id(1, 5, 8)

        g2 = Genome(3, 1)
        g2.create_node(Node.TYPE_HIDDEN, 5)
        g2.create_node(Node.TYPE_HIDDEN, 6)

        g2.connect_nodes_by_id(1, 4, 1)
        g2.connect_nodes_by_id(2, 4, 2)
        g2.connect_nodes_by_id(3, 4, 3)
        g2.connect_nodes_by_id(2, 5, 4)
        g2.connect_nodes_by_id(5, 4, 5)
        g2.connect_nodes_by_id(5, 6, 6)
        g2.connect_nodes_by_id(6, 4, 7)
        g2.connect_nodes_by_id(3, 5, 9)
        g2.connect_nodes_by_id(1, 6, 10)

        # p = Population(2, 3, 1)
        # p.population['s0'] = [g1, g2]

        a1, a2 = Population.align_genome(g1, g2)

        self.assertEqual(1, a1[0].get_innovation())
        self.assertEqual(2, a1[1].get_innovation())
        self.assertEqual(3, a1[2].get_innovation())
        self.assertEqual(4, a1[3].get_innovation())
        self.assertEqual(5, a1[4].get_innovation())
        self.assertEqual(None, a1[5])
        self.assertEqual(None, a1[6])
        self.assertEqual(8, a1[7].get_innovation())
        self.assertEqual(None, a1[8])
        self.assertEqual(None, a1[9])

        self.assertEqual(1, a2[0].get_innovation())
        self.assertEqual(2, a2[1].get_innovation())
        self.assertEqual(3, a2[2].get_innovation())
        self.assertEqual(4, a2[3].get_innovation())
        self.assertEqual(5, a2[4].get_innovation())
        self.assertEqual(6, a2[5].get_innovation())
        self.assertEqual(7, a2[6].get_innovation())
        self.assertEqual(None, a2[7])
        self.assertEqual(9, a2[8].get_innovation())
        self.assertEqual(10, a2[9].get_innovation())
Exemplo n.º 2
0
    def test_crossover(self):
        g1 = Genome(3, 1)
        g1.create_node(Node.TYPE_HIDDEN, 5)
        g1.connect_nodes_by_id(1, 4, 1)
        g1.connect_nodes_by_id(2, 4, 2)
        g1.connect_nodes_by_id(3, 4, 3)
        g1.connect_nodes_by_id(2, 5, 4)
        g1.connect_nodes_by_id(5, 4, 5)
        g1.connect_nodes_by_id(1, 5, 8)

        g2 = Genome(3, 1)
        g2.create_node(Node.TYPE_HIDDEN, 5)
        g2.create_node(Node.TYPE_HIDDEN, 6)

        g2.connect_nodes_by_id(1, 4, 1)
        g2.connect_nodes_by_id(2, 4, 2)
        g2.connect_nodes_by_id(3, 4, 3)
        g2.connect_nodes_by_id(2, 5, 4)
        g2.connect_nodes_by_id(5, 4, 5)
        g2.connect_nodes_by_id(5, 6, 6)
        g2.connect_nodes_by_id(6, 4, 7)
        g2.connect_nodes_by_id(3, 5, 9)
        g2.connect_nodes_by_id(1, 6, 10)

        p = Population(2, 3, 1)
        p.population[0] = g1
        p.population[1] = g2

        a1, a2 = Population.align_genome(g1, g2)

        g3 = Population.crossover(a1, a2, g1.get_input_nodes(), g1.get_output_nodes())

        self.assertEqual(10, len(g3.connections))
        self.assertEqual(6, len(g3.nodes))

        self.assertEqual(1, g3.select_connection_by_innovation(1).get_innovation())
        self.assertEqual(2, g3.select_connection_by_innovation(2).get_innovation())
        self.assertEqual(3, g3.select_connection_by_innovation(3).get_innovation())
        self.assertEqual(4, g3.select_connection_by_innovation(4).get_innovation())
        self.assertEqual(5, g3.select_connection_by_innovation(5).get_innovation())
        self.assertEqual(6, g3.select_connection_by_innovation(6).get_innovation())
        self.assertEqual(7, g3.select_connection_by_innovation(7).get_innovation())
        self.assertEqual(8, g3.select_connection_by_innovation(8).get_innovation())
        self.assertEqual(9, g3.select_connection_by_innovation(9).get_innovation())
        self.assertEqual(10, g3.select_connection_by_innovation(10).get_innovation())

        self.assertEqual((2, 3), Population.calculate_excess_disjoint(g1, g2))
        self.assertEqual(5, species_distance(g1, g2))
Exemplo n.º 3
0
    def test_another_crossover(self):
        g1 = Genome(3, 1)
        g2 = Genome(3, 1)

        node_i = 4
        conn_i = 0

        for generation in range(10):
            conn_i = g1.mutate_connections(conn_i)
            node_i, conn_i = g1.mutate_nodes(node_i, conn_i)
            conn_i = g2.mutate_connections(conn_i)
            node_i, conn_i = g2.mutate_nodes(node_i, conn_i)

            a1, a2 = Population.align_genome(g1, g2)
            g1 = Population.crossover(a1, a2, g1.get_input_nodes(), g1.get_output_nodes())
            g2 = Population.crossover(a1, a2, g1.get_input_nodes(), g1.get_output_nodes())

        Printer(g1).print()
Exemplo n.º 4
0
 def test_empty_crossover(self):
     g1 = Genome(3, 1)
     g2 = Genome(3, 1)
     a1, a2 = Population.align_genome(g1, g2)
     c = Population.crossover(a1, a2, g1.get_input_nodes(), g1.get_output_nodes())
     self.assertGreaterEqual(4, len(c.nodes))