Beispiel #1
0
        public override double CompatibilityDistance(SpeciatedGenome <CPPNNEATGeneCollection, PType> genome)
        {
            var differences = new DifferenceAnalysis(this.GeneCollection, genome.GeneCollection);

            var totalExcess   = differences.FirstCollection.Excess.Count + differences.SecondCollection.Excess.Count;
            var totalDisjoint = differences.FirstCollection.Disjoint.Count + differences.SecondCollection.Disjoint.Count;

            var averageWeightDifference = differences.Matches
                                          .Select(match =>
                                                  (match.FirstCollection.Weight - match.SecondCollection.Weight)
                                                  .Magnitude)
                                          .Average();

            var n = (double)Math.Max(this.GeneCollection.LinkGenes.Count(),
                                     genome.GeneCollection.LinkGenes.Count());

            if (n <= SMALL_GENOME_THRESHOLD)
            {
                n = 1;
            }

            var parent = Parent as ICPPNNEATGA;
            var averageFunctionDifference = GetAverageFunctionDifference(this, (CPPNNEATGenome <PType>)genome);

            return(parent.ExcessGenesWeight * (totalExcess / n) +
                   parent.DisjointGenesWeight * (totalDisjoint / n) +
                   parent.MatchingGenesWeight * averageWeightDifference +
                   parent.FunctionDifferenceWeight * averageFunctionDifference);
        }
Beispiel #2
0
        public CPPNNEATGenome(ICPPNNEATGA parentGA, CPPNNEATGenome <PType> parent, CPPNNEATGenome <PType> partner)
            : this(parentGA)
        {
            var differences = new DifferenceAnalysis(parent.GeneCollection, partner.GeneCollection);

            var disjointAndExcessSource = differences.FirstCollection;

            if (partner.Score > parent.Score)
            {
                disjointAndExcessSource = differences.SecondCollection;
            }
            else if (partner.Score == parent.Score)
            {
                disjointAndExcessSource = (MathExtensions.RandomNumber() <= 0.5) ? differences.FirstCollection :
                                          differences.SecondCollection;
            }

            Func <CPPNNEATLinkGene, CPPNNEATLinkGene, Complex> weightSelector = null;

            if (MathExtensions.RandomNumber() <= parentGA.MateByAveragingRate)
            {
                weightSelector = (first, second) => (first.Weight + second.Weight) / 2;
            }
            else
            {
                weightSelector = (first, second) => (MathExtensions.RandomNumber() <= 0.5) ? first.Weight : second.Weight;
            }

            foreach (var match in differences.Matches)
            {
                var geneToCopy = match.FirstCollection;

                var newGene = geneToCopy.Copy();
                newGene.Enabled = true;
                newGene.Weight  = weightSelector(match.FirstCollection, match.SecondCollection);

                GeneCollection.TryAddLinkGene(newGene);

                if ((!match.FirstCollection.Enabled || !match.SecondCollection.Enabled) &&
                    MathExtensions.RandomNumber() <= parentGA.DisableGeneRate)
                {
                    GeneCollection.DisableLinkGene(newGene.InnovationNumber);
                }
            }

            foreach (var linkGene in disjointAndExcessSource
                     .Disjoint.Union(disjointAndExcessSource.Excess))
            {
                GeneCollection.TryAddLinkGene(linkGene.Copy());
            }
        }