Beispiel #1
0
        public void TestSynapseGene_ToJSON()
        {
            var synapseGene = new SynapseGene(3, 0, 1, true, 0.5f);
            var json        = JSON.Serialize(synapseGene.ToJSON());

            Assert.AreEqual("{\"innovation\":3,\"weight\":0.5,\"from\":0,\"to\":1,\"enabled\":true}", json);
        }
        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;
              }
        }
 public static SynapseGene FromPrototype(SynapseGene other)
 {
     return SynapseGene.Random(other.innovationId,
     other.fromNeuronId,
     other.toNeuronId,
     other.isEnabled);
 }
 public void TestSynapseGene_FromJSON()
 {
     var json = JSON.Deserialize("{\"innovation\":3,\"weight\":0.5,\"from\":0,\"to\":1,\"enabled\":true}");
       var synapseGene = SynapseGene.FromJSON(json);
       var expected = new SynapseGene(3, 0, 1, true, 0.5f);
       Assert.AreEqual(expected, synapseGene);
 }
Beispiel #5
0
 public static SynapseGene FromPrototype(SynapseGene other)
 {
     return(SynapseGene.Random(other.innovationId,
                               other.fromNeuronId,
                               other.toNeuronId,
                               other.isEnabled));
 }
Beispiel #6
0
 public SynapseGene(SynapseGene other)
 {
     this.innovationId = other.innovationId;
     this.fromNeuronId = other.fromNeuronId;
     this.toNeuronId   = other.toNeuronId;
     this.isEnabled    = other.isEnabled;
     this.weight       = other.weight;
 }
Beispiel #7
0
        public void TestSynapseGene_FromJSON()
        {
            var json        = JSON.Deserialize("{\"innovation\":3,\"weight\":0.5,\"from\":0,\"to\":1,\"enabled\":true}");
            var synapseGene = SynapseGene.FromJSON(json);
            var expected    = new SynapseGene(3, 0, 1, true, 0.5f);

            Assert.AreEqual(expected, synapseGene);
        }
 public SynapseGene(SynapseGene other)
 {
     this.innovationId = other.innovationId;
       this.fromNeuronId = other.fromNeuronId;
       this.toNeuronId = other.toNeuronId;
       this.isEnabled = other.isEnabled;
       this.weight = other.weight;
 }
Beispiel #9
0
        public static Genotype FromPrototype(Genotype protoGenotype)
        {
            var neuronGenes = protoGenotype.NeuronGenes
                              .Select(g => NeuronGene.FromPrototype(g))
                              .ToGeneList();
            var synapseGenes = protoGenotype.SynapseGenes
                               .Select(g => SynapseGene.FromPrototype(g))
                               .ToGeneList();

            return(new Genotype(neuronGenes, synapseGenes));
        }
Beispiel #10
0
        public static Genotype FromJSON(object obj)
        {
            var data        = (Dictionary <string, object>)obj;
            var neuronGenes = ((List <object>)data["neurons"])
                              .Select(g => NeuronGene.FromJSON(g))
                              .ToGeneList();
            var synapseGenes = ((List <object>)data["synapses"])
                               .Select(g => SynapseGene.FromJSON(g))
                               .ToGeneList();

            return(new Genotype(neuronGenes, synapseGenes));
        }
Beispiel #11
0
        public void Mutate(Genotype genotype, MutationResults results)
        {
            // We require a synapse to split
            if (genotype.SynapseCount == 0)
            {
                return;
            }

            // Pick a synapse at random
            var synapseIndex = Random.Range(0, genotype.SynapseCount);
            var synapseGene  = genotype.SynapseGenes.ElementAt(synapseIndex);

            // Generate an innovation id for the operation
            var innovationId = innovations.GetNeuronInnovationId(
                synapseGene.fromNeuronId,
                synapseGene.toNeuronId,
                synapseGene.InnovationId
                );

            // If the genotype already contains the innovation id, bail
            if (genotype.Contains(innovationId))
            {
                return;
            }

            var neuronGene = NeuronGene.Random(innovationId);

            var neuronGenes = new GeneList <NeuronGene>(genotype.NeuronGenes);

            neuronGenes.Add(neuronGene);
            genotype.NeuronGenes = neuronGenes;

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

            var synapseGene1 = new SynapseGene(innovationId + 0,
                                               synapseGene.fromNeuronId,
                                               neuronGene.InnovationId,
                                               true, 0.5f);

            synapseGenes.Add(synapseGene1);

            var synapseGene2 = new SynapseGene(innovationId + 1,
                                               neuronGene.InnovationId,
                                               synapseGene.toNeuronId,
                                               true, 0.5f);

            synapseGenes.Add(synapseGene2);

            genotype.SynapseGenes = synapseGenes;

            results.addedNeurons  += 1;
            results.addedSynapses += 2;
        }
        public void Mutate(Genotype genotype, MutationResults results)
        {
            // We require a synapse to split
              if (genotype.SynapseCount == 0) {
            return;
              }

              // Pick a synapse at random
              var synapseIndex = Random.Range(0, genotype.SynapseCount);
              var synapseGene = genotype.SynapseGenes.ElementAt(synapseIndex);

              // Generate an innovation id for the operation
              var innovationId = innovations.GetNeuronInnovationId(
            synapseGene.fromNeuronId,
            synapseGene.toNeuronId,
            synapseGene.InnovationId
              );

              // If the genotype already contains the innovation id, bail
              if (genotype.Contains(innovationId)) {
            return;
              }

              var neuronGene = NeuronGene.Random(innovationId);

              var neuronGenes = new GeneList<NeuronGene>(genotype.NeuronGenes);
              neuronGenes.Add(neuronGene);
              genotype.NeuronGenes = neuronGenes;

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

              var synapseGene1 = new SynapseGene(innovationId + 0,
            synapseGene.fromNeuronId,
            neuronGene.InnovationId,
            true, 0.5f);
              synapseGenes.Add(synapseGene1);

              var synapseGene2 = new SynapseGene(innovationId + 1,
            neuronGene.InnovationId,
            synapseGene.toNeuronId,
            true, 0.5f);
              synapseGenes.Add(synapseGene2);

              genotype.SynapseGenes = synapseGenes;

              results.addedNeurons += 1;
              results.addedSynapses += 2;
        }
        public void TestSynapseGene()
        {
            var synapseGene = new SynapseGene(3, 0, 1, true, 0.5f);
              Assert.AreEqual(synapseGene.InnovationId, 3);
              Assert.AreEqual(synapseGene.fromNeuronId, 0);
              Assert.AreEqual(synapseGene.toNeuronId, 1);
              Assert.AreEqual(synapseGene.isEnabled, true);
              Assert.AreEqual(synapseGene.weight, 0.5f);

              var synapseGeneRand = SynapseGene.FromPrototype(synapseGene);
              Assert.AreEqual(synapseGeneRand.InnovationId, synapseGene.InnovationId);
              Assert.AreEqual(synapseGeneRand.fromNeuronId, synapseGene.fromNeuronId);
              Assert.AreEqual(synapseGeneRand.toNeuronId, synapseGene.toNeuronId);
              Assert.AreEqual(synapseGeneRand.isEnabled, synapseGene.isEnabled);
              Assert.AreNotEqual(synapseGeneRand.weight, synapseGene.weight);
        }
Beispiel #14
0
        public void TestSynapseGene()
        {
            var synapseGene = new SynapseGene(3, 0, 1, true, 0.5f);

            Assert.AreEqual(synapseGene.InnovationId, 3);
            Assert.AreEqual(synapseGene.fromNeuronId, 0);
            Assert.AreEqual(synapseGene.toNeuronId, 1);
            Assert.AreEqual(synapseGene.isEnabled, true);
            Assert.AreEqual(synapseGene.weight, 0.5f);

            var synapseGeneRand = SynapseGene.FromPrototype(synapseGene);

            Assert.AreEqual(synapseGeneRand.InnovationId, synapseGene.InnovationId);
            Assert.AreEqual(synapseGeneRand.fromNeuronId, synapseGene.fromNeuronId);
            Assert.AreEqual(synapseGeneRand.toNeuronId, synapseGene.toNeuronId);
            Assert.AreEqual(synapseGeneRand.isEnabled, synapseGene.isEnabled);
            Assert.AreNotEqual(synapseGeneRand.weight, synapseGene.weight);
        }
Beispiel #15
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;
            }
        }
Beispiel #16
0
        public void Mutate(Genotype genotype, MutationResults results)
        {
            var neuronIndexA = Random.Range(0, genotype.InputNeurons.Count);
            var neuronGeneA  = genotype.InputNeurons[neuronIndexA];

            var validNeurons = genotype.OutputNeurons
                               .Where(_ => Random.value < p)
                               .Where(neuronGeneB => genotype.SynapseGenes.None(s =>
                                                                                neuronGeneA.InnovationId == s.fromNeuronId &&
                                                                                neuronGeneB.InnovationId == s.toNeuronId));

            foreach (var neuronGeneB in validNeurons)
            {
                var synapseInnovationId = innovations.GetSynapseInnovationId(neuronGeneA.InnovationId, neuronGeneB.InnovationId);
                var synapseGene         = SynapseGene.Random(synapseInnovationId, neuronGeneA.InnovationId, neuronGeneB.InnovationId, true);

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

                results.addedSynapses += 1;
            }
        }
 public void TestSynapseGene_ToJSON()
 {
     var synapseGene = new SynapseGene(3, 0, 1, true, 0.5f);
       var json = JSON.Serialize(synapseGene.ToJSON());
       Assert.AreEqual("{\"innovation\":3,\"weight\":0.5,\"from\":0,\"to\":1,\"enabled\":true}", json);
 }
        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);
        }
Beispiel #19
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);
        }