Esempio n. 1
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))
Esempio n. 2
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()
Esempio n. 3
0
    def test_get_output_nodes(self):
        g = Genome(3, 1)

        self.assertEqual([4], [g.get_innovation() for g in g.get_output_nodes()])
Esempio 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))