def test_add_node(self): Genome.reset() genome = Genome() genome.allocate_hidden_nodes(3) input_node_id_1 = genome.add_input_node() input_node_id_2 = genome.add_input_node() hidden_node_id_1 = genome.add_hidden_node() hidden_node_id_2 = genome.add_hidden_node() hidden_node_id_3 = genome.add_hidden_node() output_node_id_1 = genome.add_output_node() output_node_id_2 = genome.add_output_node() #Inserted check self.assertTrue(input_node_id_1 in range(len(genome.nodes))) self.assertTrue(input_node_id_2 in range(len(genome.nodes))) self.assertTrue(hidden_node_id_1 in range(len(genome.nodes))) self.assertTrue(hidden_node_id_2 in range(len(genome.nodes))) self.assertTrue(hidden_node_id_3 in range(len(genome.nodes))) self.assertTrue(output_node_id_1 in range(len(genome.nodes))) self.assertTrue(output_node_id_2 in range(len(genome.nodes))) #Added to output ndoes check self.assertFalse(input_node_id_1 in genome.output_nodes_ids) self.assertFalse(input_node_id_2 in genome.output_nodes_ids) self.assertFalse(hidden_node_id_1 in genome.output_nodes_ids) self.assertFalse(hidden_node_id_2 in genome.output_nodes_ids) self.assertFalse(hidden_node_id_3 in genome.output_nodes_ids) self.assertTrue(output_node_id_1 in genome.output_nodes_ids) self.assertTrue(output_node_id_2 in genome.output_nodes_ids) #Type set check self.assertEqual(genome.nodes[input_node_id_1].type, Type.INPUT) self.assertEqual(genome.nodes[input_node_id_2].type, Type.INPUT) self.assertEqual(genome.nodes[hidden_node_id_1].type, Type.HIDDEN) self.assertEqual(genome.nodes[hidden_node_id_2].type, Type.HIDDEN) self.assertEqual(genome.nodes[hidden_node_id_3].type, Type.HIDDEN) self.assertEqual(genome.nodes[output_node_id_1].type, Type.OUTPUT) self.assertEqual(genome.nodes[output_node_id_2].type, Type.OUTPUT) #ID set check self.assertEqual(genome.nodes[input_node_id_1].id, input_node_id_1) self.assertEqual(genome.nodes[input_node_id_2].id, input_node_id_2) self.assertEqual(genome.nodes[hidden_node_id_1].id, hidden_node_id_1) self.assertEqual(genome.nodes[hidden_node_id_2].id, hidden_node_id_2) self.assertEqual(genome.nodes[hidden_node_id_3].id, hidden_node_id_3) self.assertEqual(genome.nodes[output_node_id_1].id, output_node_id_1) self.assertEqual(genome.nodes[output_node_id_2].id, output_node_id_2)
def test_add_gene(self): Genome.reset() genome = Genome() genes = [] genome.allocate_hidden_nodes(1) input_node_id_1 = genome.add_input_node() input_node_id_2 = genome.add_input_node() hidden_node_id_1 = genome.add_hidden_node() output_node_id_1 = genome.add_output_node() output_node_id_2 = genome.add_output_node() genes.append(Gene(input_node_id_1, hidden_node_id_1)) genes.append(Gene(input_node_id_2, hidden_node_id_1)) genes.append(Gene(hidden_node_id_1, output_node_id_1)) genes.append(Gene(hidden_node_id_1, output_node_id_2)) genome.set_genes((genes)) gene = Gene(input_node_id_1, output_node_id_1) genome.add_gene(gene) self.assertEqual(genome.nodes[output_node_id_1].connected_nodes[1], genome.nodes[input_node_id_1])
def test_mate(self): Genome.reset() for pair in Genome.GENE_INNOVATION_PAIRS: print(pair[0]) print(pair[1]) genome_parent_1 = TestGenome() genome_parent_2 = TestGenome() genome_child = TestGenome() network_parent_1 = Network(genome_parent_1) network_parent_2 = Network(genome_parent_2) network_child = Network(genome_child) genome_parent_1.add_new_node(0) genome_parent_2.add_new_node(1) genome_child.add_new_node(2) Genome.mate(genome_parent_1, genome_parent_2, genome_child) network_child.set_up(genome_child) for i in range(-1, -4, -1): self.assertTrue(network_child.genome.genes[i].enabled) self.assertTrue(network_child.genome.genes[i].used) self.assertEqual(network_child.genome.genes[3].innovation, 1) self.assertEqual(network_child.genome.genes[4].innovation, 2) self.assertEqual(network_child.genome.genes[5].innovation, 3)
def test_execute(self): Genome.reset() network = XORNetwork() value_11 = 1 value_12 = 0 value_21 = 0 value_22 = 1 value_31 = 0 value_32 = 0 value_41 = 1 value_42 = 1 input_values_node_ids1 = [(value_11, network.input_node_id_1), (value_12, network.input_node_id_2)] input_values_node_ids2 = [(value_21, network.input_node_id_1), (value_22, network.input_node_id_2)] input_values_node_ids3 = [(value_31, network.input_node_id_1), (value_32, network.input_node_id_2)] input_values_node_ids4 = [(value_41, network.input_node_id_1), (value_42, network.input_node_id_2)] results1 = network.execute(input_values_node_ids1) results2 = network.execute(input_values_node_ids2) results3 = network.execute(input_values_node_ids3) results4 = network.execute(input_values_node_ids4) self.assertEqual(int(value_11 != value_12), round(results1[5])) self.assertEqual(int(value_21 != value_22), round(results2[5])) self.assertEqual(int(value_31 != value_32), round(results3[5])) self.assertEqual(int(value_41 != value_42), round(results4[5]))
def test_mate(self): Genome.reset() genome1 = Genome() genome2 = Genome() new_genome = Genome() genes1 = [] genes2 = [] genome1.allocate_hidden_nodes(1) genome2.allocate_hidden_nodes(1) new_genome.allocate_hidden_nodes(1) #genome 1 input_node_id_11 = genome1.add_input_node() input_node_id_21 = genome1.add_input_node() hidden_node_id_11 = genome1.add_hidden_node() output_node_id_11 = genome1.add_output_node() output_node_id_21 = genome1.add_output_node() genes1.append(Gene(input_node_id_11, hidden_node_id_11, weight = 10.0)) genes1.append(Gene(input_node_id_21, hidden_node_id_11)) genes1.append(Gene(hidden_node_id_11, output_node_id_11)) genes1.append(Gene(hidden_node_id_11, output_node_id_21)) genome1.set_genes((genes1)) #genome 2 input_node_id_12 = genome2.add_input_node() input_node_id_22 = genome2.add_input_node() hidden_node_id_12 = genome2.add_hidden_node() output_node_id_12 = genome2.add_output_node() output_node_id_22 = genome2.add_output_node() genes2.append(Gene(input_node_id_12, hidden_node_id_12, weight = 20.0)) genes2.append(Gene(input_node_id_22, hidden_node_id_12)) genes2.append(Gene(hidden_node_id_12, output_node_id_12)) genes2.append(Gene(hidden_node_id_12, output_node_id_22, enabled = False)) genome2.set_genes((genes2)) genome2.allocate_genes(1) genome2.add_new_connection(input_node_id_12, output_node_id_12) #new genome Genome.mate(genome1, genome2, new_genome) self.assertEqual(new_genome.genes[0].weight, 15.0) self.assertEqual(len(new_genome.genes), 5) self.assertFalse(new_genome.genes[3].enabled)
def test_distance(self): Genome.reset() genome1 = TestGenome3() genome2 = TestGenome2() species = Species(genome1) genome2.add_new_node(0) distance1 = species.compare(genome1) distance2 = species.compare(genome2) self.assertEqual(distance1, 0.0) self.assertEqual(distance2, 2.0/3.0)
def test_add_new_connection(self): Genome.reset() genome = Genome() genes = [] genome.allocate_hidden_nodes(1) input_node_id_1 = genome.add_input_node() input_node_id_2 = genome.add_input_node() hidden_node_id_1 = genome.add_hidden_node() output_node_id_1 = genome.add_output_node() output_node_id_2 = genome.add_output_node() genes.append(Gene(input_node_id_1, hidden_node_id_1)) genes.append(Gene(input_node_id_2, hidden_node_id_1)) genes.append(Gene(hidden_node_id_1, output_node_id_1)) genes.append(Gene(hidden_node_id_1, output_node_id_2)) genome.set_genes((genes)) genome.allocate_genes(1) connected = genome.add_new_connection(input_node_id_1, output_node_id_1) self.assertEqual(genome.nodes[output_node_id_1].connected_nodes[1], genome.nodes[input_node_id_1]) self.assertTrue(connected) connected1 = genome.add_new_connection(input_node_id_1, input_node_id_2) connected2 = genome.add_new_connection(output_node_id_1, output_node_id_2) connected3 = genome.add_new_connection(output_node_id_2, hidden_node_id_1) connected4 = genome.add_new_connection(output_node_id_2, input_node_id_1) self.assertFalse(connected1) self.assertFalse(connected2) self.assertFalse(connected3) self.assertFalse(connected4)
def test_add_new_node(self): Genome.reset() genome = Genome() genes = [] genome.allocate_hidden_nodes(2) input_node_id_1 = genome.add_input_node() input_node_id_2 = genome.add_input_node() hidden_node_id_1 = genome.add_hidden_node() output_node_id_1 = genome.add_output_node() output_node_id_2 = genome.add_output_node() genes.append(Gene(input_node_id_1, hidden_node_id_1, weight = 10.0)) genes.append(Gene(input_node_id_2, hidden_node_id_1)) genes.append(Gene(hidden_node_id_1, output_node_id_1)) genes.append(Gene(hidden_node_id_1, output_node_id_2)) genome.set_genes(genes) genome.allocate_genes(2) gene_id = 0 genome.add_new_node(gene_id) self.assertEqual(genome.nodes[hidden_node_id_1].connected_nodes[0], genome.nodes[input_node_id_1]) self.assertEqual(genome.nodes[hidden_node_id_1].connected_nodes[1], genome.nodes[input_node_id_2]) self.assertEqual(genome.nodes[hidden_node_id_1].connected_nodes[2], genome.nodes[2]) self.assertEqual(genome.nodes[2].connected_nodes[0], genome.nodes[input_node_id_1]) self.assertEqual(genome.genes[-1].weight, 10.0) self.assertEqual(genome.genes[-2].weight, 1.0)
def test_set_genes(self): genome = Genome() genes = [] Genome.reset() genome.allocate_hidden_nodes(1) input_node_id_1 = genome.add_input_node() input_node_id_2 = genome.add_input_node() hidden_node_id_1 = genome.add_hidden_node() output_node_id_1 = genome.add_output_node() output_node_id_2 = genome.add_output_node() genes.append(Gene(input_node_id_1, hidden_node_id_1)) genes.append(Gene(input_node_id_2, hidden_node_id_1)) genes.append(Gene(hidden_node_id_1, output_node_id_1)) genes.append(Gene(hidden_node_id_1, output_node_id_2)) genome.set_genes((genes)) self.assertEqual(genome.nodes[output_node_id_1].connected_nodes[0], genome.nodes[hidden_node_id_1]) self.assertEqual(genome.nodes[output_node_id_2].connected_nodes[0], genome.nodes[hidden_node_id_1]) self.assertEqual(genome.nodes[hidden_node_id_1].connected_nodes[0], genome.nodes[input_node_id_1]) self.assertEqual(genome.nodes[hidden_node_id_1].connected_nodes[1], genome.nodes[input_node_id_2]) self.assertEqual(genome.nodes[input_node_id_1].level, 0) self.assertEqual(genome.nodes[input_node_id_2].level, 0) self.assertEqual(genome.nodes[hidden_node_id_1].level, 1) self.assertEqual(genome.nodes[output_node_id_1].level, 2) self.assertEqual(genome.nodes[output_node_id_2].level, 2)
def test_find_max_innovation(self): Genome.reset() genome1 = Genome() genome2 = Genome() genes1 = [] genes2 = [] genome1.allocate_hidden_nodes(1) genome2.allocate_hidden_nodes(1) #genome 1 input_node_id_11 = genome1.add_input_node() input_node_id_21 = genome1.add_input_node() hidden_node_id_11 = genome1.add_hidden_node() output_node_id_11 = genome1.add_output_node() output_node_id_21 = genome1.add_output_node() genes1.append(Gene(input_node_id_11, hidden_node_id_11, weight = 10.0)) genes1.append(Gene(input_node_id_21, hidden_node_id_11)) genes1.append(Gene(hidden_node_id_11, output_node_id_11)) genes1.append(Gene(hidden_node_id_11, output_node_id_21)) genome1.set_genes(genes1) #genome 2 input_node_id_12 = genome2.add_input_node() input_node_id_22 = genome2.add_input_node() hidden_node_id_12 = genome2.add_hidden_node() output_node_id_12 = genome2.add_output_node() output_node_id_22 = genome2.add_output_node() genes2.append(Gene(input_node_id_12, hidden_node_id_12, weight = 10.0)) genes2.append(Gene(input_node_id_22, hidden_node_id_12)) genes2.append(Gene(hidden_node_id_12, output_node_id_12)) genes2.append(Gene(hidden_node_id_12, output_node_id_22)) genome2.set_genes(genes2) genome2.allocate_genes(1) genome2.add_new_connection(input_node_id_12, output_node_id_12) max_innovation = genome1.find_max_innovation(genome2) base_lists = genome1.set_up_base_lists(genome2) innovative_lists = genome1.set_up_innovative_lists(genome2, max_innovation) total_list = genome1.set_up_lists(genome2) self.assertEqual(max_innovation, 1) self.assertEqual(len(base_lists[0]), len(base_lists[1])) self.assertEqual(len(innovative_lists), max_innovation) self.assertEqual(innovative_lists[0][0], None) self.assertEqual(innovative_lists[0][1], genome2.genes[-1]) self.assertEqual(total_list[:-1], base_lists[:]) self.assertEqual(total_list[-1], innovative_lists[0])