def test_easy_forward_propagation(self):
        genome = GenomeMock([(2, 4, 0, True), (1, 3, 0, True), (2, 3, 0, True),
                             (1, 4, 0, True)], 2, 2)
        nn = NeuralNetwork()
        nn.generate_network(genome)
        y = nn.forward([3, 22])
        self.assertEqual(len(y), 2)
        self.assertEqual(y, [0.5, 0.5])

        genome = GenomeMock([(2, 4, 1, True), (1, 3, 0, True), (2, 3, 0, True),
                             (1, 4, 0, True)], 2, 2)
        nn.generate_network(genome)
        y = nn.forward([3, 22])
        self.assertEqual(y[0], 0.5)
        self.assertAlmostEqual(y[1], 0.9926085)

        genome = GenomeMock([(2, 4, 1, True), (1, 3, 0, True), (2, 3, 0, True),
                             (1, 4, -2, True)], 2, 2)
        nn.generate_network(genome)
        y = nn.forward([3, 22])
        self.assertEqual(y[0], 0.5)
        self.assertAlmostEqual(y[1], 0.00739157)

        genome = GenomeMock([(2, 4, 1, True), (1, 3, 0, False),
                             (2, 3, 0, False), (1, 4, -2, True)], 2, 2)
        nn.generate_network(genome)
        y = nn.forward([3, 22])
        self.assertEqual(y[0], 0.5)
        self.assertAlmostEqual(y[1], 0.00739157)
 def test_input_neurons_have_input_signal(self):
     genome = GenomeMock([(2, 4, 0, True), (1, 3, 0, True), (2, 3, 0, True),
                          (1, 4, 0, True)], 2, 2)
     nn = NeuralNetwork()
     nn.generate_network(genome)
     nn.forward([3, 22])
     self.assertEqual(nn._input_neurons[1]._input_signals, [3])
     self.assertEqual(nn._input_neurons[2]._input_signals, [22])
 def test_input_length_exception(self):
     genome = GenomeMock([(2, 4, 0, True), (1, 3, 0, True), (2, 3, 0, True),
                          (1, 4, 0, True)], 2, 2)
     nn = NeuralNetwork()
     nn.generate_network(genome)
     with self.assertRaises(Exception) as e:
         nn.forward([1])
     self.assertEqual(str(e.exception), "Expected 2 inputs, got 1 instead")
    def test_hard_forward_propagation(self):
        genome = GenomeMock([(1, 5, 3, True), (2, 5, -2, True),
                             (1, 6, -1, True), (5, 6, -3.4, True),
                             (3, 6, 4, True), (6, 4, 5, True)], 3, 1)
        nn = NeuralNetwork()
        nn.generate_network(genome)
        y = nn.forward([0.2, 2, -0.02])
        self.assertAlmostEqual(y[0], 0.5144, places=4)

        genome = GenomeMock([(1, 5, 3, True),
                             (2, 5, -2, True), (1, 7, -1, True),
                             (5, 7, -3.4, True), (3, 7, 4, True),
                             (7, 6, 2, True), (6, 4, 0.3, True)], 3, 1)
        nn.generate_network(genome)
        y = nn.forward([0.2, 2, -0.02])
        self.assertAlmostEqual(y[0], 0.6778, places=4)
Exemple #5
0
    def test_evolve_xor(self):
        print("testing advanced xor")
        Group._GROUP_ID = 0
        Generation._GENERATION_ID = 0

        specie = Group()

        c1 = ConnectionGene(1, 3, enabled=True)
        c2 = ConnectionGene(2, 3, enabled=True)
        c3 = ConnectionGene(2, 3, enabled=True)
        c4 = ConnectionGene(2, 3, enabled=True)
        c5 = ConnectionGene(2, 3, enabled=True)
        c6 = ConnectionGene(2, 3, enabled=True)
        c7 = ConnectionGene(2, 3, enabled=True)
        c8 = ConnectionGene(2, 3, enabled=True)
        c9 = ConnectionGene(2, 3, enabled=True)
        c10 = ConnectionGene(2, 3, enabled=True)
        c11 = ConnectionGene(2, 3, enabled=True)
        c12 = ConnectionGene(2, 3, enabled=True)
        c13 = ConnectionGene(2, 3, enabled=True)
        c14 = ConnectionGene(2, 3, enabled=True)
        c15 = ConnectionGene(2, 3, enabled=True)
        c16 = ConnectionGene(2, 3, enabled=True)
        c17 = ConnectionGene(2, 3, enabled=True)
        c18 = ConnectionGene(2, 3, enabled=True)
        c19 = ConnectionGene(2, 3, enabled=True)
        c20 = ConnectionGene(2, 3, enabled=True)
        c21 = ConnectionGene(2, 3, enabled=True)
        c22 = ConnectionGene(2, 3, enabled=True)
        c23 = ConnectionGene(2, 3, enabled=True)
        c24 = ConnectionGene(2, 3, enabled=True)
        c25 = ConnectionGene(2, 3, enabled=True)
        c26 = ConnectionGene(2, 3, enabled=True)
        c27 = ConnectionGene(2, 3, enabled=True)
        c28 = ConnectionGene(2, 3, enabled=True)
        c29 = ConnectionGene(2, 3, enabled=True)
        c30 = ConnectionGene(2, 3, enabled=True)
        c31 = ConnectionGene(2, 3, enabled=True)
        c31 = ConnectionGene(2, 3, enabled=True)
        c33 = ConnectionGene(2, 3, enabled=True)

        for i in range(10):
            specie.add_genome(
                Genome([
                    [1, 9, random.normalvariate(mu=0.0, sigma=1.0), True],
                    [1, 10,
                     random.normalvariate(mu=0.0, sigma=1.0), True],
                    [1, 11,
                     random.normalvariate(mu=0.0, sigma=1.0), True],
                    [1, 12,
                     random.normalvariate(mu=0.0, sigma=1.0), True],
                    [2, 9, random.normalvariate(mu=0.0, sigma=1.0), True],
                    [2, 10,
                     random.normalvariate(mu=0.0, sigma=1.0), True],
                    [2, 11,
                     random.normalvariate(mu=0.0, sigma=1.0), True],
                    [2, 12,
                     random.normalvariate(mu=0.0, sigma=1.0), True],
                    [3, 9, random.normalvariate(mu=0.0, sigma=1.0), True],
                    [3, 10,
                     random.normalvariate(mu=0.0, sigma=1.0), True],
                    [3, 11,
                     random.normalvariate(mu=0.0, sigma=1.0), True],
                    [3, 12,
                     random.normalvariate(mu=0.0, sigma=1.0), True],
                    [4, 9, random.normalvariate(mu=0.0, sigma=1.0), True],
                    [4, 10,
                     random.normalvariate(mu=0.0, sigma=1.0), True],
                    [4, 11,
                     random.normalvariate(mu=0.0, sigma=1.0), True],
                    [4, 12,
                     random.normalvariate(mu=0.0, sigma=1.0), True],
                    [5, 9, random.normalvariate(mu=0.0, sigma=1.0), True],
                    [5, 10,
                     random.normalvariate(mu=0.0, sigma=1.0), True],
                    [5, 11,
                     random.normalvariate(mu=0.0, sigma=1.0), True],
                    [5, 12,
                     random.normalvariate(mu=0.0, sigma=1.0), True],
                    [6, 9, random.normalvariate(mu=0.0, sigma=1.0), True],
                    [6, 10,
                     random.normalvariate(mu=0.0, sigma=1.0), True],
                    [6, 11,
                     random.normalvariate(mu=0.0, sigma=1.0), True],
                    [6, 12,
                     random.normalvariate(mu=0.0, sigma=1.0), True],
                    [7, 9, random.normalvariate(mu=0.0, sigma=1.0), True],
                    [7, 10,
                     random.normalvariate(mu=0.0, sigma=1.0), True],
                    [7, 11,
                     random.normalvariate(mu=0.0, sigma=1.0), True],
                    [7, 12,
                     random.normalvariate(mu=0.0, sigma=1.0), True],
                    [8, 9, random.normalvariate(mu=0.0, sigma=1.0), True],
                    [8, 10,
                     random.normalvariate(mu=0.0, sigma=1.0), True],
                    [8, 11,
                     random.normalvariate(mu=0.0, sigma=1.0), True],
                    [8, 12,
                     random.normalvariate(mu=0.0, sigma=1.0), True]
                ], 8, 4))

        mutation_coefficients = {
            'add_connection': 0.5,
            'split_connection': 0.2,
            'change_weight': 0.8,
            'new_connection_abs_max_weight': 1.0,
            'max_weight_mutation': 0.25
        }
        compatibility_coefficients = {
            'excess_factor': 1.0,
            'disjoint_factor': 1.0,
            'weight_difference_factor': 0.5
        }
        log = Logger()
        gen = Generation([specie],
                         mutation_coefficients=mutation_coefficients,
                         compatibility_coefficients=compatibility_coefficients,
                         logger=log)

        i = 1
        while i < 8:
            print(i)
            gen = gen.create_new_generation()
            i += 1
        best_nn = NeuralNetwork(Generation.best_genome)
        print(best_nn.forward([0, 1, 1, 0, 0, 1, 1, 0]))
        print(str(best_nn._genome.fitness) + "/" + str(256 * 4))
Exemple #6
0
    def test_simple_xor(self):
        print("testing simple xor")
        Group._GROUP_ID = 0
        Generation._GENERATION_ID = 0
        specie = Group()
        for i in range(16):
            for j in range(17, 21):
                ConnectionGene(i + 1, j, enabled=True)

        connection_list = []
        z = 0
        for i in range(16):
            for j in range(17, 21):
                connection_list.append([
                    i + 1, j,
                    random.normalvariate(mu=0.0, sigma=1.0), True, z
                ])
                z += 1
        print(connection_list)
        for i in range(10):
            specie.add_genome(
                Genome(
                    [[1, 3,
                      random.normalvariate(mu=0.0, sigma=1.0), True, 0],
                     [2, 3,
                      random.normalvariate(mu=0.0, sigma=1.0), True, 1]], 2,
                    1))

        mutation_coefficients = {
            'add_connection': 0.5,
            'split_connection': 0.2,
            'change_weight': 0.8,
            'new_connection_abs_max_weight': 1.0,
            'max_weight_mutation': 0.5
        }
        compatibility_coefficients = {
            'excess_factor': 1.0,
            'disjoint_factor': 1.0,
            'weight_difference_factor': 2.0
        }
        log = Logger()
        gen = Generation([specie],
                         mutation_coefficients=mutation_coefficients,
                         compatibility_coefficients=compatibility_coefficients,
                         logger=log)

        i = 1
        while i < 150:
            print(i)
            gen = gen.create_new_generation()
            i += 1
        best_nn = NeuralNetwork(Generation.best_genome)
        a = (best_nn.forward([0.0, 0.0]))
        b = (best_nn.forward([0.0, 1.0]))
        c = (best_nn.forward([1.0, 0.0]))
        d = (best_nn.forward([1.0, 1.0]))
        print(a)
        print(b)
        print(c)
        print(d)

        print(4.0 - (a[0] - 0)**2 - (b[0] - 1)**2 - (c[0] - 1)**2 -
              (d[0] - 0)**2)
        print(best_nn._genome.fitness)

        groups_count = []
        for generation_log in gen.logger.log.values():
            groups_count.append(len(generation_log.groups_log))

        plt.plot(list(gen.logger.log.keys()), groups_count)
        plt.xlabel("Generation")
        plt.ylabel("Number of groups")
        plt.title("Groups amount change over evolution")
        plt.savefig("plot of gen count")
        plt.clf()

        last_gen_groups_fitness = []
        for fit in gen.logger.log[gen.id -
                                  1].groups_fitness_scores_log.values():
            last_gen_groups_fitness.append(fit[0][2])
        plt.plot(list(gen.logger.log[gen.id - 1].groups_log.keys()),
                 last_gen_groups_fitness, 'ro')
        plt.xlabel("Group")
        plt.ylabel("Group adjusted fitness")
        plt.title("Adjusted fitness of groups in last generation")
        plt.savefig("plot of last gen fitness")
        plt.clf()

        plt.plot(list(Generation.best_fitnesses.keys()),
                 list(Generation.best_fitnesses.values()))
        plt.xlabel("Generation")
        plt.ylabel("Fitness score")
        plt.title("Fitness score progression")
        plt.savefig("plot of fitness")