public void Mutate(Genotype genotype, MutationResults results)
        {
            var prunedSynapses = genotype.SynapseGenes
            .Where(_ => Random.value < p)
            .Where(g => !g.isEnabled)
            .ToList();

              var synapseGenes = genotype.SynapseGenes.Except(prunedSynapses)
            .ToGeneList();
              genotype.SynapseGenes = synapseGenes;

              // An orphan is a neuron with no connecting synapses
              // For each neuron: check if any synapse connects to it
              // Exclude IO neurons
              var orphanedNeurons = genotype.HiddenNeurons.Where(g =>
            synapseGenes.None(s =>
              s.fromNeuronId == g.InnovationId ||
              s.toNeuronId == g.InnovationId)).ToList();

              var neuronGenes = genotype.NeuronGenes.Except(orphanedNeurons)
            .ToGeneList();
              genotype.NeuronGenes = neuronGenes;

              results.orphanedNeurons += orphanedNeurons.Count;
              results.prunedSynapses += prunedSynapses.Count;
        }
        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;
              }
        }
Ejemplo n.º 3
0
        public void TestAddSynapseMutator()
        {
            var mutator = new AddSynapseMutator(innovations);

            var mutatedGenotype = new Genotype(genotype);
            var mutationResults = new MutationResults();

            mutator.Mutate(mutatedGenotype, mutationResults);

            Assert.AreEqual(1, mutationResults.addedSynapses);

            // Compare gene counts
            Assert.AreEqual(mutatedGenotype.NeuronCount, genotype.NeuronCount);
            Assert.AreEqual(1, mutatedGenotype.SynapseCount - genotype.SynapseCount);

            // Compare innovation ids
            var aN = new [] { 0, 1, 2 };
            var bN = mutatedGenotype.NeuronGenes.Select(g => g.InnovationId);

            Assert.That(aN.SequenceEqual(bN));

            var aS = new [] { 3, 4, 5, 6 };
            var bS = mutatedGenotype.SynapseGenes.Select(g => g.InnovationId);

            Assert.That(aS.SequenceEqual(bS));
        }
Ejemplo n.º 4
0
        public void Mutate(Genotype genotype, MutationResults results)
        {
            var prunedSynapses = genotype.SynapseGenes
                                 .Where(_ => Random.value < p)
                                 .Where(g => !g.isEnabled)
                                 .ToList();

            var synapseGenes = genotype.SynapseGenes.Except(prunedSynapses)
                               .ToGeneList();

            genotype.SynapseGenes = synapseGenes;

            // An orphan is a neuron with no connecting synapses
            // For each neuron: check if any synapse connects to it
            // Exclude IO neurons
            var orphanedNeurons = genotype.HiddenNeurons.Where(g =>
                                                               synapseGenes.None(s =>
                                                                                 s.fromNeuronId == g.InnovationId ||
                                                                                 s.toNeuronId == g.InnovationId)).ToList();

            var neuronGenes = genotype.NeuronGenes.Except(orphanedNeurons)
                              .ToGeneList();

            genotype.NeuronGenes = neuronGenes;

            results.orphanedNeurons += orphanedNeurons.Count;
            results.prunedSynapses  += prunedSynapses.Count;
        }
 public void Mutate(Genotype genotype, MutationResults results)
 {
     var innovationIds = genotype.NeuronGenes.InnovationIds;
       for (int i = 0; i < innovationIds.Count; i++) {
     if (Random.value < p) {
       var innovationId = innovationIds[i];
       genotype.NeuronGenes[innovationId] = NeuronGene.Random(innovationId);
       results.replacedNeurons++;
     }
       }
 }
 public void Mutate(Genotype genotype, MutationResults results)
 {
     var innovationIds = genotype.SynapseGenes.InnovationIds;
       for (int i = 0; i < innovationIds.Count; i++) {
     if (Random.value < p) {
       var synapseGene = genotype.SynapseGenes[innovationIds[i]];
       synapseGene.isEnabled = !synapseGene.isEnabled;
       genotype.SynapseGenes[synapseGene.InnovationId] = synapseGene;
       results.toggledSynapses++;
     }
       }
 }
Ejemplo n.º 7
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)
 {
     var innovationIds = genotype.SynapseGenes.InnovationIds;
       for (int i = 0; i < innovationIds.Count; i++) {
     if (Random.value < p) {
       var synapseGene = genotype.SynapseGenes[innovationIds[i]];
       synapseGene.weight = Random.value;
       genotype.SynapseGenes[synapseGene.InnovationId] = synapseGene;
       results.replacedSynapses++;
     }
       }
 }
 public void Mutate(Genotype genotype, MutationResults results)
 {
     var innovationIds = genotype.SynapseGenes.InnovationIds;
       for (int i = 0; i < innovationIds.Count; i++) {
     if (Random.value < p) {
       var synapseGene = genotype.SynapseGenes[innovationIds[i]];
       synapseGene.weight = Mathf.Clamp01(RandomHelper.NextCauchy(synapseGene.weight, mutationScale));
       genotype.SynapseGenes[synapseGene.InnovationId] = synapseGene;
       results.perturbedSynapses++;
     }
       }
 }
Ejemplo n.º 10
0
        public void Mutate(Genotype genotype, MutationResults results)
        {
            var innovationIds = genotype.NeuronGenes.InnovationIds;

            for (int i = 0; i < innovationIds.Count; i++)
            {
                if (Random.value < p)
                {
                    var innovationId = innovationIds[i];
                    genotype.NeuronGenes[innovationId] = NeuronGene.Random(innovationId);
                    results.replacedNeurons++;
                }
            }
        }
        public void TestReplaceSynapseMutator()
        {
            var mutator = new ReplaceSynapseMutator(1.0f);

              var mutatedGenotype = new Genotype(genotype);
              var mutationResults = new MutationResults();
              mutator.Mutate(mutatedGenotype, mutationResults);

              Assert.AreEqual(mutationResults.replacedSynapses, 3);

              // Compare gene counts
              var comparer = new GenotypeShapeEqualityComparer();
              Assert.That(comparer.Equals(mutatedGenotype, genotype));
        }
        public void TestPerturbSynapseMutator()
        {
            var mutator = new PerturbSynapseMutator(1.0f, 0.25f);

              var mutatedGenotype = new Genotype(genotype);
              var mutationResults = new MutationResults();
              mutator.Mutate(mutatedGenotype, mutationResults);

              Assert.AreEqual(3, mutationResults.perturbedSynapses);

              // Compare gene counts
              var comparer = new GenotypeShapeEqualityComparer();
              Assert.That(comparer.Equals(mutatedGenotype, genotype));
        }
Ejemplo n.º 13
0
        public void Mutate(Genotype genotype, MutationResults results)
        {
            var innovationIds = genotype.SynapseGenes.InnovationIds;

            for (int i = 0; i < innovationIds.Count; i++)
            {
                if (Random.value < p)
                {
                    var synapseGene = genotype.SynapseGenes[innovationIds[i]];
                    synapseGene.weight = Random.value;
                    genotype.SynapseGenes[synapseGene.InnovationId] = synapseGene;
                    results.replacedSynapses++;
                }
            }
        }
Ejemplo n.º 14
0
        public void Mutate(Genotype genotype, MutationResults results)
        {
            var innovationIds = genotype.SynapseGenes.InnovationIds;

            for (int i = 0; i < innovationIds.Count; i++)
            {
                if (Random.value < p)
                {
                    var synapseGene = genotype.SynapseGenes[innovationIds[i]];
                    synapseGene.weight = Mathf.Clamp01(RandomHelper.NextCauchy(synapseGene.weight, mutationScale));
                    genotype.SynapseGenes[synapseGene.InnovationId] = synapseGene;
                    results.perturbedSynapses++;
                }
            }
        }
Ejemplo n.º 15
0
        public void Mutate(Genotype genotype, MutationResults results)
        {
            var innovationIds = genotype.SynapseGenes.InnovationIds;

            for (int i = 0; i < innovationIds.Count; i++)
            {
                if (Random.value < p)
                {
                    var synapseGene = genotype.SynapseGenes[innovationIds[i]];
                    synapseGene.isEnabled = !synapseGene.isEnabled;
                    genotype.SynapseGenes[synapseGene.InnovationId] = synapseGene;
                    results.toggledSynapses++;
                }
            }
        }
        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 MutationResults Mutate(Genotype[] genotypes)
 {
     var orderedMutators = mutators.OrderBy(m => m.Key);
       var results = new MutationResults();
       foreach (var genotype in genotypes) {
     foreach (var p_mutator in orderedMutators) {
       var p = p_mutator.Key;
       var mutator = p_mutator.Value;
       if (Random.value < p) {
     mutator.Mutate(genotype, results);
     break;
       }
     }
       }
       return results;
 }
Ejemplo n.º 18
0
        public void TestPerturbNeuronMutator()
        {
            var mutator = new PerturbNeuronMutator(1.0f, 0.25f);

            var mutatedGenotype = new Genotype(genotype);
            var mutationResults = new MutationResults();

            mutator.Mutate(mutatedGenotype, mutationResults);

            Assert.AreEqual(3, mutationResults.perturbedNeurons);

            // Compare gene counts
            var comparer = new GenotypeShapeEqualityComparer();

            Assert.That(comparer.Equals(mutatedGenotype, genotype));
        }
Ejemplo n.º 19
0
        public void TestReplaceSynapseMutator()
        {
            var mutator = new ReplaceSynapseMutator(1.0f);

            var mutatedGenotype = new Genotype(genotype);
            var mutationResults = new MutationResults();

            mutator.Mutate(mutatedGenotype, mutationResults);

            Assert.AreEqual(mutationResults.replacedSynapses, 3);

            // Compare gene counts
            var comparer = new GenotypeShapeEqualityComparer();

            Assert.That(comparer.Equals(mutatedGenotype, genotype));
        }
 public void Mutate(Genotype genotype, MutationResults results)
 {
     var innovationIds = genotype.NeuronGenes.InnovationIds;
       for (int i = 0; i < innovationIds.Count; i++) {
     if (Random.value < p) {
       var neuronGene = genotype.NeuronGenes[innovationIds[i]];
       neuronGene.a = Mathf.Clamp01(RandomHelper.NextCauchy(neuronGene.a, mutationScale));
       neuronGene.b = Mathf.Clamp01(RandomHelper.NextCauchy(neuronGene.b, mutationScale));
       neuronGene.c = Mathf.Clamp01(RandomHelper.NextCauchy(neuronGene.c, mutationScale));
       neuronGene.d = Mathf.Clamp01(RandomHelper.NextCauchy(neuronGene.d, mutationScale));
       neuronGene.mean = Mathf.Clamp01(RandomHelper.NextCauchy(neuronGene.mean, mutationScale));
       neuronGene.sigma = Mathf.Clamp01(RandomHelper.NextCauchy(neuronGene.sigma, mutationScale));
       genotype.NeuronGenes[neuronGene.InnovationId] = neuronGene;
       results.perturbedNeurons++;
     }
       }
 }
Ejemplo n.º 21
0
        public MutationResults Mutate(Genotype[] genotypes)
        {
            var orderedMutators = mutators.OrderBy(m => m.Key);
            var results         = new MutationResults();

            foreach (var genotype in genotypes)
            {
                foreach (var p_mutator in orderedMutators)
                {
                    var p       = p_mutator.Key;
                    var mutator = p_mutator.Value;
                    if (Random.value < p)
                    {
                        mutator.Mutate(genotype, results);
                        break;
                    }
                }
            }
            return(results);
        }
Ejemplo n.º 22
0
        public void Mutate(Genotype genotype, MutationResults results)
        {
            var innovationIds = genotype.NeuronGenes.InnovationIds;

            for (int i = 0; i < innovationIds.Count; i++)
            {
                if (Random.value < p)
                {
                    var neuronGene = genotype.NeuronGenes[innovationIds[i]];
                    neuronGene.a     = Mathf.Clamp01(RandomHelper.NextCauchy(neuronGene.a, mutationScale));
                    neuronGene.b     = Mathf.Clamp01(RandomHelper.NextCauchy(neuronGene.b, mutationScale));
                    neuronGene.c     = Mathf.Clamp01(RandomHelper.NextCauchy(neuronGene.c, mutationScale));
                    neuronGene.d     = Mathf.Clamp01(RandomHelper.NextCauchy(neuronGene.d, mutationScale));
                    neuronGene.mean  = Mathf.Clamp01(RandomHelper.NextCauchy(neuronGene.mean, mutationScale));
                    neuronGene.sigma = Mathf.Clamp01(RandomHelper.NextCauchy(neuronGene.sigma, mutationScale));
                    genotype.NeuronGenes[neuronGene.InnovationId] = neuronGene;
                    results.perturbedNeurons++;
                }
            }
        }
        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;
              }
        }
Ejemplo n.º 24
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;
            }
        }
        public void TestAddSynapseMutator()
        {
            var mutator = new AddSynapseMutator(innovations);

              var mutatedGenotype = new Genotype(genotype);
              var mutationResults = new MutationResults();
              mutator.Mutate(mutatedGenotype, mutationResults);

              Assert.AreEqual(1, mutationResults.addedSynapses);

              // Compare gene counts
              Assert.AreEqual(mutatedGenotype.NeuronCount, genotype.NeuronCount);
              Assert.AreEqual(1, mutatedGenotype.SynapseCount - genotype.SynapseCount);

              // Compare innovation ids
              var aN = new []{0, 1, 2};
              var bN = mutatedGenotype.NeuronGenes.Select(g => g.InnovationId);
              Assert.That(aN.SequenceEqual(bN));

              var aS = new []{3, 4, 5, 6};
              var bS = mutatedGenotype.SynapseGenes.Select(g => g.InnovationId);
              Assert.That(aS.SequenceEqual(bS));
        }
Ejemplo n.º 26
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;
            }
        }