示例#1
0
        public override string ToString()
        {
            string result = "|";

            Genes.ForEach(x => result += x.ToString() + "|");
            return(result);
        }
示例#2
0
        public void MutateGeneWeights(GeneMutationType mutationType, double power, double rate)
        {
            int    geneCount  = 0;
            double genomeTail = Genes.Count * 0.8;

            bool severe = new Random().NextDouble() > GeneParameters.MutateGeneWeightSevereProbability;

            double adjustmentProbability  = 0;
            double replacementProbability = 0;

            Genes.ForEach(g =>
            {
                if (severe)
                {
                    adjustmentProbability  = GeneParameters.MutateGeneWeightSevereAdjustmentProbability;
                    replacementProbability = GeneParameters.MutateGeneWeightSevereReplacementProbability;
                }
                else if (Genes.Count >= GeneParameters.GeneCountThreshold && geneCount > genomeTail)
                {
                    adjustmentProbability  = GeneParameters.MutateGeneWeightAdjustmentProbability;
                    replacementProbability = GeneParameters.MutateGeneWeightReplacementProbability;
                }
                else
                {
                    if (new Random().NextDouble() > 0.5)
                    {
                        adjustmentProbability  = 1 - rate;
                        replacementProbability = 1 - rate - 0.1;
                    }
                    else
                    {
                        adjustmentProbability  = 1 - rate;
                        replacementProbability = 1 - rate;
                    }
                }

                if (mutationType == GeneMutationType.AdjustWeight)
                {
                    double choice = new Random().NextDouble();

                    if (choice > adjustmentProbability)
                    {
                        g.MutateWeight(mutationType, power);
                    }
                    else if (choice > replacementProbability)
                    {
                        g.MutateWeight(mutationType, power);
                    }
                }
                else if (mutationType == GeneMutationType.ReplaceWeight)
                {
                    g.MutateWeight(mutationType, power);
                }

                if (g.Weight > GeneParameters.GeneWeightMaximum)
                {
                    g.Weight = GeneParameters.GeneWeightMaximum;
                }
                else if (g.Weight < GeneParameters.GeneWeightMinimum)
                {
                    g.Weight = GeneParameters.GeneWeightMinimum;
                }

                geneCount++;
            });
        }