[Quantproject-developers] QuantProject/b1_ADT/Optimizing/Genetic GeneticOptimizer.cs, 1.17, 1.18
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2006-08-10 16:08:33
|
Update of /cvsroot/quantproject/QuantProject/b1_ADT/Optimizing/Genetic In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv17362/b1_ADT/Optimizing/Genetic Modified Files: GeneticOptimizer.cs Log Message: Fixed VERY important bug in rouletteSelection method: GO now works properly also with negative fitnesses. Index: GeneticOptimizer.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b1_ADT/Optimizing/Genetic/GeneticOptimizer.cs,v retrieving revision 1.17 retrieving revision 1.18 diff -C2 -d -r1.17 -r1.18 *** GeneticOptimizer.cs 7 Aug 2006 21:03:24 -0000 1.17 --- GeneticOptimizer.cs 10 Aug 2006 16:08:27 -0000 1.18 *************** *** 51,55 **** private int generationNumber; private int genomeSize; ! private double totalFitness; private Genome bestGenome; private Genome worstGenome; --- 51,55 ---- private int generationNumber; private int genomeSize; ! private double totalSpecialFitnessForRouletteSelection; private Genome bestGenome; private Genome worstGenome; *************** *** 60,64 **** private ArrayList currentEliteToTransmitToNextGeneration; private ArrayList nextGeneration; ! private ArrayList cumulativeFitnessList; private int generationCounter; --- 60,64 ---- private ArrayList currentEliteToTransmitToNextGeneration; private ArrayList nextGeneration; ! private ArrayList cumulativeSpecialFitnessListForRouletteSelection; private int generationCounter; *************** *** 210,214 **** this.genomeSize = this.genomeManager.GenomeSize; this.genomeComparer = new GenomeComparer(); ! this.cumulativeFitnessList = new ArrayList(this.PopulationSize); this.currentGeneration = new ArrayList(this.PopulationSize); int eliteNumber = (int)(this.ElitismRate*(double)this.PopulationSize); --- 210,214 ---- this.genomeSize = this.genomeManager.GenomeSize; this.genomeComparer = new GenomeComparer(); ! this.cumulativeSpecialFitnessListForRouletteSelection = new ArrayList(this.PopulationSize); this.currentGeneration = new ArrayList(this.PopulationSize); int eliteNumber = (int)(this.ElitismRate*(double)this.PopulationSize); *************** *** 227,231 **** { this.createFirstGeneration(false); ! this.averageRandomFitness = this.totalFitness/this.currentGeneration.Count; this.standardDeviationOfRandomFitness = this.calculateRandomFitness_getStdDevOfRandomFitness(); } --- 227,231 ---- { this.createFirstGeneration(false); ! this.averageRandomFitness = this.totalSpecialFitnessForRouletteSelection/this.currentGeneration.Count; this.standardDeviationOfRandomFitness = this.calculateRandomFitness_getStdDevOfRandomFitness(); } *************** *** 287,295 **** { bool returnValue = false; ! double averageFitnessOfCurrentGeneration = this.totalFitness / this.populationSize; ! double bestFitnessReachedUntilNow = this.BestGenome.Fitness; ! if (averageFitnessOfCurrentGeneration >= ! this.minConvergenceRate * bestFitnessReachedUntilNow ) returnValue = true; --- 287,296 ---- { bool returnValue = false; ! double averageSpecialFitness = this.totalSpecialFitnessForRouletteSelection / this.populationSize; ! double bestSpecialFitnessReachedUntilNow = this.BestGenome.Fitness + ! Math.Abs(((Genome)this.currentGeneration[0]).Fitness); ! if (averageSpecialFitness >= ! this.minConvergenceRate * bestSpecialFitnessReachedUntilNow ) returnValue = true; *************** *** 307,312 **** this.NewGeneration( this , new NewGenerationEventArgs( this.currentGeneration , this.generationCounter , this.generationNumber ) ); ! this.calculateTotalFitness(); ! this.updateCumulativeFitnessList(); this.setInitialBestAndWorstGenomes(); --- 308,313 ---- this.NewGeneration( this , new NewGenerationEventArgs( this.currentGeneration , this.generationCounter , this.generationNumber ) ); ! this.calculateTotalSpecialFitnessForRouletteSelection(); ! this.updateCumulativeSpecialFitnessListForRouletteSelection(); this.setInitialBestAndWorstGenomes(); *************** *** 317,326 **** private void showOutputToConsole() { ! System.Console.WriteLine("\n\n\n\n*_*_*_*_*_*_*_*_*_*_*\n\nGeneration " + this.generationCounter +"\n"); for(int i = 0; i < this.populationSize; i++) { ! System.Console.WriteLine((string)((Genome)this.currentGeneration[i]).Meaning + ! "--> " + ((Genome)this.currentGeneration[i]).Fitness); } //Console.WriteLine("Press enter key to continue ..."); //Console.ReadLine(); --- 318,345 ---- private void showOutputToConsole() { ! string genes = ""; ! //System.Console.WriteLine("\n*_*_*_*_*_*_*_*_*_*_*\n\nGeneration " + this.generationCounter +"\n"); ! // string pathFile = System.Configuration.ConfigurationSettings.AppSettings["GenericArchive"] + ! // "\\GenomesThroughoutGenerations.txt"; ! // System.IO.StreamWriter w = System.IO.File.AppendText(pathFile); for(int i = 0; i < this.populationSize; i++) { ! int genomeNumber = i + 1; ! foreach(int gene in ((Genome)this.currentGeneration[i]).Genes() ) ! genes = genes + " " + gene.ToString(); ! System.Console.WriteLine("\r\n" + this.GenerationCounter + " " + genomeNumber + " " + genes + " " + ! ((Genome)this.currentGeneration[i]).Fitness + " " + ! ((Genome)this.currentGeneration[i]).HasBeenChanged.ToString() + " " + ! ((Genome)this.currentGeneration[i]).HasBeenCloned.ToString() + " " + ! ((Genome)this.currentGeneration[i]).Generation); ! // w.Write("\r\n" + this.GenerationCounter + " " + genomeNumber + " " + genes + " " + ! // ((Genome)this.currentGeneration[i]).Fitness + " " + ! // ((Genome)this.currentGeneration[i]).HasBeenChanged.ToString() + " " + ! // ((Genome)this.currentGeneration[i]).HasBeenCloned.ToString() + " " + ! // ((Genome)this.currentGeneration[i]).Generation); ! genes = ""; } + // w.Flush(); + // w.Close(); //Console.WriteLine("Press enter key to continue ..."); //Console.ReadLine(); *************** *** 334,338 **** private int rouletteSelection() { ! double randomFitness = this.totalFitness *(double)this.random.Next(1,1001)/1000; int idx = -1; int first = 0; --- 353,357 ---- private int rouletteSelection() { ! double randomFitness = this.totalSpecialFitnessForRouletteSelection *(double)this.random.Next(1,1001)/1000; int idx = -1; int first = 0; *************** *** 343,351 **** while (idx == -1 && first <= last) { ! if (randomFitness < (double)this.cumulativeFitnessList[mid]) { last = mid; } ! else if (randomFitness >= (double)this.cumulativeFitnessList[mid]) { first = mid; --- 362,370 ---- while (idx == -1 && first <= last) { ! if (randomFitness < (double)this.cumulativeSpecialFitnessListForRouletteSelection[mid]) { last = mid; } ! else if (randomFitness >= (double)this.cumulativeSpecialFitnessListForRouletteSelection[mid]) { first = mid; *************** *** 353,357 **** mid = (first + last)/2; if ((last - first) == 1) ! idx = last; } return idx; --- 372,376 ---- mid = (first + last)/2; if ((last - first) == 1) ! idx = last;//it's time to exit from the while loop } return idx; *************** *** 360,385 **** /// <summary> ! /// Calculate total fitness for current generation /// </summary> ! private void calculateTotalFitness() { ! this.totalFitness = 0.0; ! foreach(Genome g in this.currentGeneration) ! this.totalFitness += g.Fitness; } ! ! /// <summary> ! /// Rank current generation and sort in order of fitness. ! /// </summary> ! private void updateCumulativeFitnessList() { ! double cumulativeFitness = 0.0; ! this.cumulativeFitnessList.Clear(); for (int i = 0; i < this.populationSize; i++) { ! cumulativeFitness += ((Genome)this.currentGeneration[i]).Fitness; ! this.cumulativeFitnessList.Add(cumulativeFitness); } } --- 379,405 ---- /// <summary> ! /// Calculate total special fitness for current generation /// </summary> ! private void calculateTotalSpecialFitnessForRouletteSelection() { ! this.totalSpecialFitnessForRouletteSelection = 0.0; ! double worstSpecialFitnessForRouletteSelection = ! Math.Abs(((Genome)this.currentGeneration[0]).Fitness); foreach(Genome g in this.currentGeneration) ! this.totalSpecialFitnessForRouletteSelection += g.Fitness + ! worstSpecialFitnessForRouletteSelection; } ! ! private void updateCumulativeSpecialFitnessListForRouletteSelection() { ! double cumulativeSpecialFitness = 0.0; ! this.cumulativeSpecialFitnessListForRouletteSelection.Clear(); for (int i = 0; i < this.populationSize; i++) { ! cumulativeSpecialFitness += ! ((Genome)this.currentGeneration[i]).Fitness + ! Math.Abs(((Genome)this.currentGeneration[0]).Fitness); ! this.cumulativeSpecialFitnessListForRouletteSelection.Add(cumulativeSpecialFitness); } } *************** *** 459,464 **** this.NewGeneration( this , new NewGenerationEventArgs( this.currentGeneration , this.generationCounter , this.generationNumber ) ); ! this.calculateTotalFitness(); ! this.updateCumulativeFitnessList(); this.generationCounter++; --- 479,484 ---- this.NewGeneration( this , new NewGenerationEventArgs( this.currentGeneration , this.generationCounter , this.generationNumber ) ); ! this.calculateTotalSpecialFitnessForRouletteSelection(); ! this.updateCumulativeSpecialFitnessListForRouletteSelection(); this.generationCounter++; *************** *** 492,496 **** this.currentGeneration.Clear(); int numOfNextGeneration = this.nextGeneration.Count; ! // Note that next generation is greater than current: // due to the population size, genomes with lowest fitness are abandoned for (int i = 1 ; i <= this.populationSize; i++) --- 512,516 ---- this.currentGeneration.Clear(); int numOfNextGeneration = this.nextGeneration.Count; ! // Note that next generation is greater than current: // due to the population size, genomes with lowest fitness are abandoned for (int i = 1 ; i <= this.populationSize; i++) |