//The method the sort the genomes. #endregion public void PopulationGeneration() { for (int i = 0; i < kInitialPopulation; i++) { ListGenome aGenome = new ListGenome(kMin, kMax, kParameterMin, kParameterMax, kParameterStep); int kLength = kParameterMin.Length; int crossoverRandomSeed = ListGenome.TheSeed.Next(kLength); kCrossover = crossoverRandomSeed; //kCrossover or crossover point was a random number among the length of the genome. aGenome.SetCrossoverPoint(kCrossover); aGenome.CalculateFitness(calibrationRoundID, dataDirectory, workDirectory, projectDirectory, genericAlgorithmsDirectory, observationArray, stdArray, kParameterSiteIndex, amplificationFactor, CV, observationDateArray, observationSeperator); aGenome.OutputString(observationArray); Genomes.Add(aGenome); } Genomes.Sort(myCompareMethod); int kPopulationLimitOfInitialPopulation = 20 * kPopulationLimit;//The population limit of first generation is 10 times of the limits of other generations. // kill all the genes above the population limit if (Genomes.Count > kPopulationLimitOfInitialPopulation) { Genomes.RemoveRange(kPopulationLimitOfInitialPopulation, Genomes.Count - kPopulationLimitOfInitialPopulation); } CurrentPopulation = Genomes.Count; }
//Declare ab arrry list to save the model predictions. public override int CompareTo(object a) { ListGenome Gene1 = this; ListGenome Gene2 = (ListGenome)a; return(Math.Sign(Gene2.CurrentFitness - Gene1.CurrentFitness)); }
public override void CopyGeneInfo(Genome dest) { ListGenome theGene = (ListGenome)dest; theGene.Length = Length; theGene.TheMin = TheMin; theGene.TheMax = TheMax; }
public void LocalPopulationGeneration( ) { int parameterNumber = optimalParameterValueArray.Count; double bestFitness = 0; int numberOfGenomes = 0; double[] kLocalParameterValue = new double[parameterNumber]; double[] kLocalParameterMin = new double[parameterNumber]; double[] kLocalParameterMax = new double[parameterNumber]; double[] kLocalParameterStep = new double[parameterNumber]; double[] kLocalParameterStepLimit = new double[parameterNumber]; for (int i = 0; i < optimalParameterValueArray.Count; i++) { double tempLocalParameterValue = Convert.ToDouble(optimalParameterValueArray[i]); kLocalParameterValue[i] = tempLocalParameterValue; double tempInitialParameterMin = kParameterMin[i]; double tempInitialParameterMax = kParameterMax[i]; kLocalParameterMin[i] = Math.Max((tempLocalParameterValue * (1 - kLocalParameterRatio)), tempInitialParameterMin); kLocalParameterMax[i] = Math.Min((tempLocalParameterValue * (1 + kLocalParameterRatio)), tempInitialParameterMax); //Restric the local seaching range of the parameters with climbing hill methods. Modified by He, 14/10/2010. kLocalParameterStep[i] = kParameterStep[i] * (kLocalParameterRatio / kLocalParameterRatioPartition); //Define the local searching spaces. } for (int j = 0; j < kLocalPopulation; j++) { ListGenome aGenome = new ListGenome(kMin, kMax, kLocalParameterValue, kLocalParameterMin, kLocalParameterMax, kLocalParameterStep, kParameterChangeProbability); aGenome.CalculateFitness(calibrationRoundID, dataDirectory, workDirectory, projectDirectory, genericAlgorithmsDirectory, observationArray, stdArray, kParameterSiteIndex, amplificationFactor, CV, observationDateArray, observationSeperator); aGenome.OutputString(observationArray); if (j == 0) { Genomes.Add(aGenome); } //Add the first genome. numberOfGenomes = Genomes.Count; double tempFitness = aGenome.CurrentFitness; if (tempFitness > bestFitness) { Genomes.RemoveAt((numberOfGenomes - 1));//Remove the last element of Genomes. Genomes.Add(aGenome); bestFitness = tempFitness; } } }
public override Genome Crossover(Genome g) { ListGenome aGene1 = new ListGenome(); ListGenome aGene2 = new ListGenome(); g.CopyGeneInfo(aGene1); g.CopyGeneInfo(aGene2); ListGenome CrossingGene = (ListGenome)g; for (int i = 0; i < CrossoverPoint; i++) { aGene1.TheArray.Add(CrossingGene.TheArray[i]); aGene2.TheArray.Add(TheArray[i]); } for (int j = CrossoverPoint; j < Length; j++) { aGene1.TheArray.Add(TheArray[j]); aGene2.TheArray.Add(CrossingGene.TheArray[j]); } // 50/50 chance of returning gene1 or gene2 ListGenome aGene = null; if (TheSeed.Next(2) == 1) { aGene = aGene1; } else { aGene = aGene2; } return(aGene); }
public void DoCrossover(ArrayList genes) { ArrayList GeneMoms = new ArrayList(); GeneMoms.Clear(); ArrayList GeneDads = new ArrayList(); GeneDads.Clear(); //Console.WriteLine(genes.Count); for (int i = 0; i < genes.Count; i++) { // randomly pick the moms and dad's if (ListGenome.TheSeed.Next(100) % 2 > 0) { GeneMoms.Add(genes[i]); } else { GeneDads.Add(genes[i]); } } // now make them equal if (GeneMoms.Count > GeneDads.Count) { while (GeneMoms.Count > GeneDads.Count) { GeneDads.Add(GeneMoms[GeneMoms.Count - 1]); GeneMoms.RemoveAt(GeneMoms.Count - 1); } if (GeneDads.Count > GeneMoms.Count) { GeneDads.RemoveAt(GeneDads.Count - 1); // make sure they are equal } } else { while (GeneDads.Count > GeneMoms.Count) { GeneMoms.Add(GeneDads[GeneDads.Count - 1]); GeneDads.RemoveAt(GeneDads.Count - 1); } if (GeneMoms.Count > GeneDads.Count) { GeneMoms.RemoveAt(GeneMoms.Count - 1); // make sure they are equal } } // now cross them over and add them according to fitness for (int i = 0; i < GeneDads.Count; i++) { // pick best 2 from parent and children ListGenome babyGene1 = (ListGenome)((ListGenome)GeneDads[i]).Crossover((ListGenome)GeneMoms[i]); ListGenome babyGene2 = (ListGenome)((ListGenome)GeneMoms[i]).Crossover((ListGenome)GeneDads[i]); GenomeFamily.Clear(); GenomeFamily.Add(GeneDads[i]); GenomeFamily.Add(GeneMoms[i]); GenomeFamily.Add(babyGene1); GenomeFamily.Add(babyGene2); CalculateFitnessForAll(GenomeFamily); GenomeFamily.Sort(myCompareMethod); if (Best2 == true) { // if Best2 is true, add top fitness genes GenomeResults.Add(GenomeFamily[0]); GenomeResults.Add(GenomeFamily[1]); } else { GenomeResults.Add(babyGene1); GenomeResults.Add(babyGene2); } } }