public static Genome MutateGenome(Genome genome, double mutationRate)
 {
     for (int i = 0; i < genome.Phrase.Length; i++)
     {
         genome.Phrase[i] = Random.NextDouble() < mutationRate
             ? RandomGenomeGenerator.RandomChars(1).First()
             : genome.Phrase[i];
     }
     return genome;
 }
        public static int GetFitness(Genome genome, string answer)
        {
            var fitness = answer.Where((currentChar, i) => currentChar == genome[i]).Count();
			genome.Fitness = fitness;
			return fitness;
        }
 public static Genome CrossoverGenomes(Genome genome1, Genome genome2)
 {
     return new Genome(genome1.Phrase.Zip(genome2.Phrase, (g1, g2) => Random.Next(0,2) == 0 ? g1 : g2).ToArray());
 }
示例#4
0
 public Genome <GType, PType> Crossover(Genome <GType, PType> partner)
 {
     return(InnerCrossover(partner));
 }
示例#5
0
 protected abstract Genome <GType, PType> InnerCrossover(Genome <GType, PType> partner);
示例#6
0
        public void NextGeneration(double rangeRatio, out double newRangeRatio)
        {
            #region Count the number of generations where the result is stuck at some local optimum.
            if (Generation == 1)
            {
                stuckFitnessValue          = maxFitness;
                stuckGenerationNumberLimit = initialStuckGenerationNumberLimit;
                rangeRatioIncrement        = initialRangeRatioIncrement;
            }
            else
            {
                if (Math.Round(stuckFitnessValue, 6) != Math.Round(maxFitness, 6))//Reduced the precision of fiteness value comparison. He, 16/07/2010.
                {
                    stuckFitnessValue          = maxFitness;
                    stuckGenerationNumber      = 0;
                    stuckGenerationNumberLimit = initialStuckGenerationNumberLimit;
                    rangeRatioIncrement        = initialRangeRatioIncrement;
                }
                else
                {
                    stuckGenerationNumber++;
                }
            }
            #endregion

            #region Calculate the current range ratio of the parameters.
            double deltaRangeRatio = Convert.ToDouble((double)(1) / (double)numberOfRangeSteps);
            deltaRangeRatio = deltaRangeRatio * rangeShrinkingFactor;
            rangeRatio      = Math.Max((rangeRatio - deltaRangeRatio), rangeRatioLimit);//Don't let the range less than 10% of the initial range.

            if (stuckGenerationNumber == stuckGenerationNumberLimit)
            {
                rangeRatio                 = rangeRatio + rangeRatioIncrement;
                stuckGenerationNumber      = 0;
                stuckGenerationNumberLimit = stuckGenerationNumberLimit + stuckGenerationNumberLimitIncrement;
                rangeRatioIncrement        = rangeRatioIncrement + deltaRangeRatioIncrement;
            }

            newRangeRatio = rangeRatio;

            Console.WriteLine("------------------------");
            Console.WriteLine(stuckGenerationNumber);
            Console.WriteLine(stuckGenerationNumberLimit);
            Console.WriteLine(rangeRatio);
            Console.WriteLine("------------------------");
            #endregion

            // increment the generation;
            Generation++;

            // check who can die
            for (int i = 0; i < Genomes.Count; i++)
            {
                if (((Genome)Genomes[i]).CanDie(kDeathFitness))
                {
                    Genomes.RemoveAt(i);
                    i--;
                }
            }

            // determine who can reproduce
            GenomeReproducers.Clear();
            GenomeResults.Clear();

            for (int i = 0; i < Genomes.Count; i++)
            {
                if (((Genome)Genomes[i]).CanReproduce(kReproductionFitness))
                {
                    GenomeReproducers.Add(Genomes[i]);
                }
            }

            ArrayList tempGenomes = (ArrayList)GenomeReproducers.Clone();
            tempGenomes.Sort(myCompareMethod);
            tempGenome0 = (Genome)tempGenomes[0];
            parameterValueArray0.Clear();
            parameterValueArray0 = getParameterValues(tempGenome0); //Save the genome that has the highest fitness value in this generation,which is the first one after sorting.

            int crossoverIncrementRatio = Math.Min((stuckGenerationNumberLimit / initialStuckGenerationNumberLimit), (populationIncrementRatioLimit + 1));

            for (int i = 0; i < crossoverIncrementRatio; i++)
            {
                DoCrossover(GenomeReproducers);
            }

            Genomes = (ArrayList)GenomeResults.Clone();

            Console.WriteLine("================================================================");
            Console.WriteLine("The number of genomes after crossover is " + Genomes.Count);
            Console.WriteLine("================================================================");

            // mutate a part of genes, which is determined by the frequency, in the new population, except the last two genes which are the best two from laste generation.
            for (int i = 0; i < (Genomes.Count); i++)
            {
                Mutate(
                    (Genome)Genomes[i],
                    kParameterMin,
                    kParameterMax,
                    kParameterStep,
                    kParameterStepLimit,
                    Generation,
                    optimalParameterValueArray,
                    rangeRatio);
            }

            //Generate local genomes, i.e. local exploitation of best solution.
            if (maxFitness > fitnessShresholdForLoacalSearch)  //Only start local search when the maxFitnees is greater than the threshold value.
            {
                LocalPopulationGeneration();
            }

            Genomes.Add(tempGenome0);
            setParmeterValues((Genome)Genomes[Genomes.Count - 1], parameterValueArray0);
            //Add the best genome from previous generations...

            //calculate fitness of all the genes
            for (int i = 0; i < (Genomes.Count); i++)
            {
                double CurrentFitness = ((Genome)Genomes[i]).CalculateFitness(calibrationRoundID, dataDirectory, workDirectory, projectDirectory, genericAlgorithmsDirectory, observationArray, stdArray, kParameterSiteIndex, amplificationFactor, CV, observationDateArray, observationSeperator);
            }

            Genomes.Sort(myCompareMethod);

            // kill all the genes above the population limit
            if (Genomes.Count > kPopulationLimit)
            {
                Genomes.RemoveRange(kPopulationLimit, Genomes.Count - kPopulationLimit);
            }

            CurrentPopulation = Genomes.Count;
            //Console.WriteLine(CurrentPopulation);
        }
示例#7
0
 private void setParmeterValues(Genome aGene, ArrayList parameterVaueList)
 {
     aGene.setParameterValues(parameterVaueList);
 }
 public static Genome CrossoverGenomes(Genome genome1, Genome genome2)
 {
     return(new Genome(genome1.Phrase.Zip(genome2.Phrase, (g1, g2) => Random.Next(0, 2) == 0 ? g1 : g2).ToArray()));
 }