def test_mutate_add_connection(self): g = Genome(3, 1) g.connect_nodes_by_id(1, 4, 1) g.connect_nodes_by_id(2, 4, 2) g.connect_nodes_by_id(3, 4, 3) g.create_node_between(2, 4, 5, 4) g.connect_nodes_by_id(1, 5, 6) self.assertEqual(True, g.select_node_by_id(1).is_connected_to_next_by_id(4)) self.assertEqual(True, g.select_node_by_id(4).is_connected_to_prev_by_id(1)) self.assertEqual(True, g.select_node_by_id(1).is_connected_to_next_by_id(5)) self.assertEqual(True, g.select_node_by_id(2).is_connected_to_next_by_id(5)) self.assertEqual(True, g.select_node_by_id(5).is_connected_to_next_by_id(4)) self.assertEqual(True, g.select_node_by_id(3).is_connected_to_next_by_id(4)) g.connect_nodes_by_id(3, 5, 7) self.assertEqual(True, g.select_node_by_id(3).is_connected_to_next_by_id(5))
def test_print(self): g = Genome(3, 1) g.connect_nodes_by_id(1, 4, 1) g.connect_nodes_by_id(2, 4, 2) g.connect_nodes_by_id(3, 4, 3) g.create_node_between(2, 4, 5, 4) g.connect_nodes_by_id(1, 5, 6) printer = Printer(g) printer.print()
def test_run(self): g = Genome(3, 1, initializer=lambda: random.random()) g.connect_nodes_by_id(1, 4, 1) g.connect_nodes_by_id(2, 4, 2) g.connect_nodes_by_id(3, 4, 3) g.create_node_between(2, 4, 5, 4) g.connect_nodes_by_id(1, 5, 6) g.connect_nodes_by_id(5, 5, 7) output = g.run([1, 1, 1]) self.assertNotEqual(0, output[0])
def test_mutate_add_node(self): g = Genome(3, 1) g.connect_nodes_by_id(1, 4, 1) g.connect_nodes_by_id(2, 4, 2) g.connect_nodes_by_id(3, 4, 3) g.create_node_between(2, 4, 5, 4) g.connect_nodes_by_id(1, 5, 6) self.assertEqual(True, g.select_node_by_id(1).is_connected_to_next_by_id(4)) self.assertEqual(True, g.select_node_by_id(4).is_connected_to_prev_by_id(1)) self.assertEqual(True, g.select_node_by_id(1).is_connected_to_next_by_id(5)) self.assertEqual(True, g.select_node_by_id(2).is_connected_to_next_by_id(5)) self.assertEqual(True, g.select_node_by_id(5).is_connected_to_next_by_id(4)) self.assertEqual(True, g.select_node_by_id(3).is_connected_to_next_by_id(4)) g.create_node_between(3, 4, 6, 7) self.assertIsNotNone(g.select_node_by_id(6)) self.assertRaises(Exception, lambda: g.select_node_by_id(7))
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))
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())