Esempio n. 1
0
    def test_genome_crossover(self):
        a = NEAT.Genome(1, 3, 0, 1, False,
                        NEAT.ActivationFunction.UNSIGNED_SIGMOID,
                        NEAT.ActivationFunction.UNSIGNED_SIGMOID, 0,
                        self._params, 0)
        self._innov_db.Init_with_genome(a)
        a.Mutate(False, NEAT.SearchMode.COMPLEXIFYING, self._innov_db,
                 self._params, self._rng)
        b = NEAT.Genome(a)
        b.SetID(2)
        for i in range(10):
            b.Mutate(False, NEAT.SearchMode.COMPLEXIFYING, self._innov_db,
                     self._params, self._rng)
        # b.PrintAllTraits()

        c = a.Mate(b, True, True, self._rng, self._params)
        c.SetID(3)
        self.assertEqual(c.GetID(), 3)
        output_a = self.activate_network(a)
        output_b = self.activate_network(b)
        output_c = self.activate_network(c)
        self.assertIsNotNone(output_c)
        self.assertNotAlmostEqual(output_a, output_c)
        self.assertNotAlmostEqual(output_b, output_c)

        for i in range(10):
            c.Mutate(False, NEAT.SearchMode.COMPLEXIFYING, self._innov_db,
                     self._params, self._rng)
        output_c = self.activate_network(c)
        self.assertIsNotNone(output_c)
        self.assertNotAlmostEqual(output_a, output_c)
        self.assertNotAlmostEqual(output_b, output_c)
Esempio n. 2
0
    def test_serialize_deserialize(self):
        str_genome = self.g.Serialize()
        cloned = multineat.Genome()
        cloned.Deserialize(str_genome)

        self.assertEqual(self.g.GetID(), cloned.GetID())
        self.assertEqual(self.g.NumInputs(), cloned.NumInputs())
        self.assertEqual(self.g.NumOutputs(), cloned.NumOutputs())
        self.assertEqual(self.g.GetFitness(), cloned.GetFitness())
        self.assertEqual(self.g.GetAdjFitness(), cloned.GetAdjFitness())
        self.assertEqual(self.g.GetDepth(), cloned.GetDepth())
        self.assertEqual(self.g.IsEvaluated(), cloned.IsEvaluated())

        for i, n_gene in enumerate(self.g.NeuronGenes):
            self.assertEqual(n_gene.A, cloned.NeuronGenes[i].A)
            self.assertEqual(n_gene.B, cloned.NeuronGenes[i].B)
            self.assertEqual(n_gene.TimeConstant, cloned.NeuronGenes[i].TimeConstant)
            self.assertEqual(n_gene.Bias, cloned.NeuronGenes[i].Bias)
            self.assertEqual(n_gene.ActFunction, cloned.NeuronGenes[i].ActFunction)
            self.assertEqual(n_gene.Type, cloned.NeuronGenes[i].Type)

        for i, l_gene in enumerate(self.g.LinkGenes):
            self.assertEqual(l_gene.FromNeuronID, cloned.LinkGenes[i].FromNeuronID)
            self.assertEqual(l_gene.ToNeuronID, cloned.LinkGenes[i].ToNeuronID)
            self.assertEqual(l_gene.InnovationID, cloned.LinkGenes[i].InnovationID)
            self.assertEqual(l_gene.Weight, cloned.LinkGenes[i].Weight)
Esempio n. 3
0
    def test_genome_clone(self):
        a = NEAT.Genome(1, 3, 0, 1, False,
                        NEAT.ActivationFunction.UNSIGNED_SIGMOID,
                        NEAT.ActivationFunction.UNSIGNED_SIGMOID, 0,
                        self._params, 0)
        self._innov_db.Init_with_genome(a)
        a.Mutate(False, NEAT.SearchMode.COMPLEXIFYING, self._innov_db,
                 self._params, self._rng)
        b = NEAT.Genome(a)
        self.assertEqual(a.NumNeurons(), b.NumNeurons())
        self.assertEqual(a.NumLinks(), b.NumLinks())
        self.assertEqual(a.NumInputs(), b.NumInputs())
        self.assertEqual(a.NumOutputs(), b.NumOutputs())

        self.assertAlmostEqual(self.activate_network(a),
                               self.activate_network(b))
Esempio n. 4
0
    def test_genome(self):
        a = NEAT.Genome(1, 3, 0, 1, False,
                        NEAT.ActivationFunction.UNSIGNED_SIGMOID,
                        NEAT.ActivationFunction.UNSIGNED_SIGMOID, 0,
                        self._params, 0)
        # a.PrintAllTraits()
        self.assertEqual(a.GetID(), 1)
        self.assertIsNotNone(self.activate_network(a))

        a.SetID(2)
        self.assertEqual(a.GetID(), 2)
Esempio n. 5
0
 def setUp(self):
     self._params = self.parameters()
     self._net = multineat.NeuralNetwork()
     self._rng = multineat.RNG()
     self._rng.Seed(0)
     self._innov_db = multineat.InnovationDatabase()
     self.g = multineat.Genome(42, 3, 7, 1, False,
                               multineat.ActivationFunction.UNSIGNED_SIGMOID,
                               multineat.ActivationFunction.UNSIGNED_SIGMOID,
                               0, self._params, 0)
     for i in range(100):
         self.g.Mutate(False, multineat.SearchMode.COMPLEXIFYING, self._innov_db, self._params, self._rng)
Esempio n. 6
0
 def test_genome_mutate(self):
     a = NEAT.Genome(1, 3, 0, 1, False,
                     NEAT.ActivationFunction.UNSIGNED_SIGMOID,
                     NEAT.ActivationFunction.UNSIGNED_SIGMOID, 0,
                     self._params, 0)
     self._innov_db.Init_with_genome(a)
     output_1 = self.activate_network(a)
     for i in range(10):
         a.Mutate(False, NEAT.SearchMode.COMPLEXIFYING, self._innov_db,
                  self._params, self._rng)
     output_2 = self.activate_network(a)
     # a.PrintAllTraits()
     self.assertEqual(a.GetID(), 1)
     self.assertIsNotNone(output_1)
     self.assertNotAlmostEqual(output_1, output_2)
Esempio n. 7
0
        def getbest(i, seed=0):
            g = NEAT.Genome(0, 3, 0, 1, False, NEAT.ActivationFunction.UNSIGNED_SIGMOID,
                            NEAT.ActivationFunction.UNSIGNED_SIGMOID, 0, params, 0)
            pop = NEAT.Population(g, params, True, 1.0, seed)
            pop.RNG.Seed(seed)

            generations = 0
            for generation in range(1000):
                genome_list = NEAT.GetGenomeList(pop)
                fitness_list = EvaluateGenomeList_Serial(genome_list, evaluate, display=False)
                NEAT.ZipFitness(genome_list, fitness_list)
                pop.Epoch()
                generations = generation
                best = max(fitness_list)
                if best > 15.0:
                    break

            return generations