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 initialze_network(self, **kwargs): number_hidden_nodes = kwargs['number_hidden_nodes'] number_genes = kwargs['number_genes'] number_output_nodes = kwargs['number_output_nodes'] new_weight_range = kwargs['new_weight_range'] genome = Genome() genes = [] genome.allocate_hidden_nodes(number_hidden_nodes) self.input_node1 = genome.add_input_node() self.input_node2 = genome.add_input_node() if not len(self.output_nodes): for _ in range(number_output_nodes): self.output_nodes.append(genome.add_output_node()) genes.append( Gene( 0, self.output_nodes[-1], new_weight_range - 2 * new_weight_range * random.random())) genes.append( Gene( self.input_node1, self.output_nodes[-1], new_weight_range - 2 * new_weight_range * random.random())) genes.append( Gene( self.input_node2, self.output_nodes[-1], new_weight_range - 2 * new_weight_range * random.random())) else: for i in range(number_output_nodes): genome.add_output_node() genes.append( Gene( 0, self.output_nodes[i], new_weight_range - 2 * new_weight_range * random.random())) genes.append( Gene( self.input_node1, self.output_nodes[i], new_weight_range - 2 * new_weight_range * random.random())) genes.append( Gene( self.input_node2, self.output_nodes[i], new_weight_range - 2 * new_weight_range * random.random())) genome.set_genes(genes) genome.allocate_genes(number_genes) return Network(genome)
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_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 __init__(self): super().__init__() genome = Genome() genes = [] self.input_node_id_1 = genome.add_input_node() self.input_node_id_2 = genome.add_input_node() self.hidden_node_id_1 = genome.add_hidden_node() self.output_node_id_1 = genome.add_output_node() self.output_node_id_2 = genome.add_output_node() genes.append(Gene(self.input_node_id_1, self.hidden_node_id_1)) genes.append(Gene(self.input_node_id_2, self.hidden_node_id_1)) genes.append(Gene(self.hidden_node_id_1, self.output_node_id_1)) genes.append(Gene(self.hidden_node_id_1, self.output_node_id_2)) genome.set_genes(genes) self.set_up(genome)
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])
def __init__(self): super().__init__() genome = Genome() genes = [] genome.allocate_hidden_nodes(2) self.bias_node_id = 0 self.input_node_id_1 = genome.add_input_node() self.input_node_id_2 = genome.add_input_node() self.hidden_node_id_1 = genome.add_hidden_node() self.hidden_node_id_2 = genome.add_hidden_node() self.output_node_id = genome.add_output_node() genes.append( Gene(in_node_id=self.bias_node_id, out_node_id=self.hidden_node_id_1, weight=-2.32161229)) genes.append( Gene(in_node_id=self.bias_node_id, out_node_id=self.hidden_node_id_2, weight=-5.2368337)) genes.append( Gene(in_node_id=self.bias_node_id, out_node_id=self.output_node_id, weight=-3.13762134)) genes.append( Gene(in_node_id=self.input_node_id_1, out_node_id=self.hidden_node_id_1, weight=5.70223616)) genes.append( Gene(in_node_id=self.input_node_id_1, out_node_id=self.hidden_node_id_2, weight=3.42762429)) genes.append( Gene(in_node_id=self.input_node_id_2, out_node_id=self.hidden_node_id_1, weight=5.73141813)) genes.append( Gene(in_node_id=self.input_node_id_2, out_node_id=self.hidden_node_id_2, weight=3.4327536)) genes.append( Gene(in_node_id=self.hidden_node_id_1, out_node_id=self.output_node_id, weight=7.05553511)) genes.append( Gene(in_node_id=self.hidden_node_id_2, out_node_id=self.output_node_id, weight=-7.68450564)) genome.set_genes(genes) self.set_up(genome)
def __init__(self): """ """ super().__init__() self.created_output_node_ids = [] self.created_input_node_ids = [] #Inputs self.INPUT_OTHER_CREATURE_ANGLE = 0 self.INPUT_OTHER_CREATURE_DISTANCE = 0 self.INPUT_OTHER_CREATURE_SIZE = 0 self.INPUT_OTHER_CREATURE_ENERGY = 0 self.INPUT_OTHER_CREATURE_FOUND_STATUS = 0 self.INPUT_FOOD_ANGLE = 0 self.INPUT_FOOD_DISTANCE = 0 self.INPUT_FOOD_FOUND_STATUS = 0 self.INPUT_SELF_ENERNGY = 9 self.INPUT_SELF_SIZE = 10 self.INPUT_SELF_SPEED = 11 #Movement related output self.OUTPUT_MOVE_STATUS = 12 #Move status output self.OUTPUT_MOVE_ANGLE = 13 #Move angle output self.OUTPUT_MOVE_SPEED = 14 #Move speed output #Eating related output self.OUTPUT_EAT_STATUS = 15 #Eat status output #Reproduction related output self.OUTPUT_REPRODUCE_STATUS = 16 #Reproduce status output genome = Genome() #------------------------------------------------------------------ #INPUT NODES #------------------------------------------------------------------ #Other creature related input self.INPUT_OTHER_CREATURE_ANGLE = genome.add_input_node() #Angle to next creature self.INPUT_OTHER_CREATURE_DISTANCE = genome.add_input_node() #Distance to next creature self.INPUT_OTHER_CREATURE_SIZE = genome.add_input_node() #Size of next creature self.INPUT_OTHER_CREATURE_ENERGY = genome.add_input_node() #Enerngy of next creature self.INPUT_OTHER_CREATURE_FOUND_STATUS = genome.add_input_node() #Next creature found status self.created_input_node_ids.append(self.INPUT_OTHER_CREATURE_ANGLE) self.created_input_node_ids.append(self.INPUT_OTHER_CREATURE_DISTANCE) self.created_input_node_ids.append(self.INPUT_OTHER_CREATURE_SIZE) self.created_input_node_ids.append(self.INPUT_OTHER_CREATURE_ENERGY) self.created_input_node_ids.append(self.INPUT_OTHER_CREATURE_FOUND_STATUS) #Food related input self.INPUT_FOOD_ANGLE = genome.add_input_node() #Angle to next creature self.INPUT_FOOD_DISTANCE = genome.add_input_node() #Distance to next creature self.INPUT_FOOD_FOUND_STATUS = genome.add_input_node() #Food found status self.created_input_node_ids.append(self.INPUT_FOOD_ANGLE) self.created_input_node_ids.append(self.INPUT_FOOD_DISTANCE) self.created_input_node_ids.append(self.INPUT_FOOD_FOUND_STATUS) #Self related input self.INPUT_SELF_ENERNGY = genome.add_input_node() #Energy self.INPUT_SELF_SIZE = genome.add_input_node() #Size self.INPUT_SELF_SPEED = genome.add_input_node() #Speed self.created_input_node_ids.append(self.INPUT_SELF_ENERNGY) self.created_input_node_ids.append(self.INPUT_SELF_SIZE) self.created_input_node_ids.append(self.INPUT_SELF_SPEED) #------------------------------------------------------------------ #OUTPUT NODES #------------------------------------------------------------------ #Movement related input self.OUTPUT_MOVE_STATUS = genome.add_output_node() #Move status output self.OUTPUT_MOVE_ANGLE = genome.add_output_node() #Move angle output self.OUTPUT_MOVE_SPEED = genome.add_output_node() #Move speed output self.created_output_node_ids.append(self.OUTPUT_MOVE_STATUS) self.created_output_node_ids.append(self.OUTPUT_MOVE_ANGLE) self.created_output_node_ids.append(self.OUTPUT_MOVE_SPEED) #Eating related input self.OUTPUT_EAT_STATUS = genome.add_output_node() #Eat status output self.created_output_node_ids.append(self.OUTPUT_EAT_STATUS) #Reproduction related input self.OUTPUT_REPRODUCE_STATUS = genome.add_output_node() #Reproduce status output self.created_output_node_ids.append(self.OUTPUT_REPRODUCE_STATUS) self.__set_up_genes(genome)