Example #1
0
        static GeneList <T> CrossoverGenes <T>(GeneList <T> a, GeneList <T> b, float fitnessA, float fitnessB) where T : IHistoricalGene
        {
            var newGenes = new GeneList <T>(System.Math.Max(a.Count, b.Count));

            foreach (var t in new GeneEnumerable <T>(a, b))
            {
                switch (t.First)
                {
                case HistoricalGeneTypes.Aligned: // Pick random aligned gene
                    newGenes.Add(Random.value >= 0.5f ? a[t.Second] : b[t.Third]);
                    break;

                case HistoricalGeneTypes.DisjointA:
                case HistoricalGeneTypes.ExcessA:
                    if (fitnessA >= fitnessB)
                    {
                        newGenes.Add(a[t.Second]);
                    }
                    break;

                case HistoricalGeneTypes.DisjointB:
                case HistoricalGeneTypes.ExcessB:
                    if (fitnessB >= fitnessA)
                    {
                        newGenes.Add(b[t.Third]);
                    }
                    break;
                }
            }

            return(newGenes);
        }
Example #2
0
 public Genotype(Genotype other)
 {
     this.neuronGenes = other.NeuronGenes
                        .Select(g => new NeuronGene(g))
                        .ToGeneList();
     this.synapseGenes = other.SynapseGenes
                         .Select(g => new SynapseGene(g))
                         .ToGeneList();
 }
Example #3
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;
        }
Example #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;
            }
        }
Example #5
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;
            }
        }
Example #6
0
 public GeneEnumerable(GeneList <T> genesA, GeneList <T> genesB)
 {
     this.genesA = genesA;
     this.genesB = genesB;
 }
Example #7
0
 public Genotype(GeneList <NeuronGene> neuronGenes, GeneList <SynapseGene> synapseGenes)
 {
     this.neuronGenes  = neuronGenes;
     this.synapseGenes = synapseGenes;
 }
Example #8
0
 public GeneList(GeneList <TGene> other)
 {
     genes = new SortedList <int, TGene>(other.ToDictionary(g => g.InnovationId));
 }