Exemple #1
0
    def initialze_network(self, **kwargs):
        number_hidden_nodes = kwargs['number_hidden_nodes']
        number_genes = kwargs['number_genes']
        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()

        self.output_node = genome.add_output_node()

        genes.append(
            Gene(0, self.output_node,
                 new_weight_range - 2 * new_weight_range * random.random()))
        genes.append(
            Gene(self.input_node1, self.output_node,
                 new_weight_range - 2 * new_weight_range * random.random()))
        genes.append(
            Gene(self.input_node2, self.output_node,
                 new_weight_range - 2 * new_weight_range * random.random()))

        genome.set_genes(genes)

        genome.allocate_genes(number_genes)

        return Network(genome)
Exemple #2
0
	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)
Exemple #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)
Exemple #4
0
	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)
Exemple #5
0
	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])