Пример #1
0
	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)
Пример #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)
Пример #3
0
	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])
Пример #4
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)
Пример #5
0
    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)
Пример #6
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)
Пример #7
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)
Пример #8
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])
Пример #9
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)