public void TestNeuronGene_ToJSON()
 {
     var neuronGene = new NeuronGene(3, NeuronType.HiddenNeuron, 0.1f, 0.2f, 0.3f, 0.4f, 0.0f, 1.0f);
       var json = JSON.Serialize(neuronGene.ToJSON());
       var expected = "{\"innovation\":3,\"type\":4,\"mean\":0.5,\"sigma\":0,\"a\":0.1,\"b\":0.2,\"c\":0.3,\"d\":0.4}";
       Assert.AreEqual(expected, json);
 }
 public NeuronGene(NeuronGene other)
 {
     this.type = other.type;
       this.innovationId = other.innovationId;
       this.a = other.a;
       this.b = other.b;
       this.c = other.c;
       this.d = other.d;
       this.mean = other.mean;
       this.sigma = other.sigma;
 }
        public void TestNeuronGene()
        {
            var neuronGene = new NeuronGene(3, NeuronType.HiddenNeuron, 0.1f, 0.2f, 0.3f, 0.4f, 0.0f, 1.0f);
              Assert.AreEqual(neuronGene.InnovationId, 3);
              Assert.AreEqual(neuronGene.a, 0.1f);
              Assert.AreEqual(neuronGene.b, 0.2f);
              Assert.AreEqual(neuronGene.c, 0.3f);
              Assert.AreEqual(neuronGene.d, 0.4f);

              var neuronGeneRand = NeuronGene.FromPrototype(neuronGene);
              Assert.AreEqual(neuronGeneRand.InnovationId, 3);
              Assert.AreNotEqual(neuronGeneRand.a, neuronGene.a);
              Assert.AreNotEqual(neuronGeneRand.b, neuronGene.b);
              Assert.AreNotEqual(neuronGeneRand.c, neuronGene.c);
              Assert.AreNotEqual(neuronGeneRand.d, neuronGene.d);
        }
예제 #4
0
        public void Mutate(Genotype genotype, MutationResults results)
        {
            var neuronIndexA = Random.Range(0, genotype.NeuronCount);
            var neuronGeneA  = genotype.NeuronGenes.ElementAt(neuronIndexA);

            var candidates = new List <NeuronGene>(genotype.NeuronGenes);

            candidates.Shuffle();

            NeuronGene neuronGeneB = default(NeuronGene);
            bool       foundNeuron = false;

            for (var i = 0; i < candidates.Count; i++)
            {
                neuronGeneB = candidates[i];

                var exists = genotype.SynapseGenes.Any(s =>
                                                       neuronGeneA.InnovationId == s.fromNeuronId &&
                                                       neuronGeneB.InnovationId == s.toNeuronId);

                if (!exists)
                {
                    foundNeuron = true;
                    break;
                }
            }

            if (foundNeuron)
            {
                var synapseInnovationId = innovations.GetSynapseInnovationId(neuronGeneA.InnovationId, neuronGeneB.InnovationId);
                var synapseGene         = new SynapseGene(synapseInnovationId, neuronGeneA.InnovationId, neuronGeneB.InnovationId, true);

                var synapseGenes = new GeneList <SynapseGene>(genotype.SynapseGenes);
                synapseGenes.Add(synapseGene);
                genotype.SynapseGenes = synapseGenes;

                results.addedSynapses += 1;
            }
        }
예제 #5
0
 public static NeuronGene FromPrototype(NeuronGene other)
 {
     return(NeuronGene.Random(other.InnovationId));
 }
예제 #6
0
        public void TestInnovationCollection()
        {
            var innovations = new InnovationCollection();

            // Make sure innovation ids are ascending
            var a = innovations.GetInitialNeuronInnovationId(0);
            var b = innovations.GetInitialNeuronInnovationId(1);
            var c = innovations.GetInitialNeuronInnovationId(2);

            Assert.That(a == 0);
            Assert.That(b == 1);
            Assert.That(c == 2);

            var sA = innovations.GetSynapseInnovationId(a, b);
            var sB = innovations.GetSynapseInnovationId(b, c);
            var sC = innovations.GetSynapseInnovationId(c, a);

            Assert.That(sA == 3);
            Assert.That(sB == 4);
            Assert.That(sC == 5);

            var d = innovations.GetNeuronInnovationId(a, b, sA);
            var e = innovations.GetNeuronInnovationId(b, c, sB);
            var f = innovations.GetNeuronInnovationId(c, d, sC);

            Assert.That(d == 6);
            Assert.That(e == 8);
            Assert.That(f == 10);

            // Make sure duplicate innovations receive their original innovation id
            var a0 = innovations.GetInitialNeuronInnovationId(0);
            var b0 = innovations.GetInitialNeuronInnovationId(1);
            var c0 = innovations.GetInitialNeuronInnovationId(2);

            Assert.That(a0 == 0);
            Assert.That(b0 == 1);
            Assert.That(c0 == 2);

            var sA0 = innovations.GetSynapseInnovationId(a0, b0);
            var sB0 = innovations.GetSynapseInnovationId(b0, c0);
            var sC0 = innovations.GetSynapseInnovationId(c0, a0);

            Assert.That(sA0 == 3);
            Assert.That(sB0 == 4);
            Assert.That(sC0 == 5);

            var d0 = innovations.GetNeuronInnovationId(a0, b0, sA0);
            var e0 = innovations.GetNeuronInnovationId(b0, c0, sB0);
            var f0 = innovations.GetNeuronInnovationId(c0, d0, sC0);

            Assert.That(d0 == 6);
            Assert.That(e0 == 8);
            Assert.That(f0 == 10);

            // Test prune
            var genotypes = Enumerable.Range(0, 3).Select(_0 => {
                var neuronGeneA  = new NeuronGene(a, NeuronType.HiddenNeuron);
                var neuronGeneB  = new NeuronGene(b, NeuronType.HiddenNeuron);
                var synapseGeneA = new SynapseGene(sA, a, b, true);
                var synapseGeneB = new SynapseGene(sB, b, c, true);
                var neuronGenes  = new [] { neuronGeneA, neuronGeneB }.ToGeneList();
                var synapseGenes = new [] { synapseGeneA, synapseGeneB }.ToGeneList();
                return(new Genotype(neuronGenes, synapseGenes));
            }).ToList();

            var prunedCount = innovations.Prune(genotypes);

            Assert.That(innovations.Count == 4);
            Assert.That(prunedCount == 5);
        }
 public static NeuronGene FromPrototype(NeuronGene other)
 {
     return NeuronGene.Random(other.InnovationId);
 }
        public void TestInnovationCollection()
        {
            var innovations = new InnovationCollection();

              // Make sure innovation ids are ascending
              var a = innovations.GetInitialNeuronInnovationId(0);
              var b = innovations.GetInitialNeuronInnovationId(1);
              var c = innovations.GetInitialNeuronInnovationId(2);
              Assert.That(a == 0);
              Assert.That(b == 1);
              Assert.That(c == 2);

              var sA = innovations.GetSynapseInnovationId(a, b);
              var sB = innovations.GetSynapseInnovationId(b, c);
              var sC = innovations.GetSynapseInnovationId(c, a);
              Assert.That(sA == 3);
              Assert.That(sB == 4);
              Assert.That(sC == 5);

              var d = innovations.GetNeuronInnovationId(a, b, sA);
              var e = innovations.GetNeuronInnovationId(b, c, sB);
              var f = innovations.GetNeuronInnovationId(c, d, sC);
              Assert.That(d == 6);
              Assert.That(e == 8);
              Assert.That(f == 10);

              // Make sure duplicate innovations receive their original innovation id
              var a0 = innovations.GetInitialNeuronInnovationId(0);
              var b0 = innovations.GetInitialNeuronInnovationId(1);
              var c0 = innovations.GetInitialNeuronInnovationId(2);
              Assert.That(a0 == 0);
              Assert.That(b0 == 1);
              Assert.That(c0 == 2);

              var sA0 = innovations.GetSynapseInnovationId(a0, b0);
              var sB0 = innovations.GetSynapseInnovationId(b0, c0);
              var sC0 = innovations.GetSynapseInnovationId(c0, a0);
              Assert.That(sA0 == 3);
              Assert.That(sB0 == 4);
              Assert.That(sC0 == 5);

              var d0 = innovations.GetNeuronInnovationId(a0, b0, sA0);
              var e0 = innovations.GetNeuronInnovationId(b0, c0, sB0);
              var f0 = innovations.GetNeuronInnovationId(c0, d0, sC0);
              Assert.That(d0 == 6);
              Assert.That(e0 == 8);
              Assert.That(f0 == 10);

              // Test prune
              var genotypes = Enumerable.Range(0, 3).Select(_0 => {
            var neuronGeneA = new NeuronGene(a, NeuronType.HiddenNeuron);
            var neuronGeneB = new NeuronGene(b, NeuronType.HiddenNeuron);
            var synapseGeneA = new SynapseGene(sA, a, b, true);
            var synapseGeneB = new SynapseGene(sB, b, c, true);
            var neuronGenes = new []{neuronGeneA, neuronGeneB}.ToGeneList();
            var synapseGenes = new []{synapseGeneA, synapseGeneB}.ToGeneList();
            return new Genotype(neuronGenes, synapseGenes);
              }).ToList();

              var prunedCount = innovations.Prune(genotypes);
              Assert.That(innovations.Count == 4);
              Assert.That(prunedCount == 5);
        }