[Quantproject-developers] QuantProject/b1_ADT/Optimizing/Genetic GeneticOptimizer.cs, 1.20, 1.21
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2006-08-13 19:01:00
|
Update of /cvsroot/quantproject/QuantProject/b1_ADT/Optimizing/Genetic In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv31453/b1_ADT/Optimizing/Genetic Modified Files: GeneticOptimizer.cs Log Message: Code has been reorganized in a more readable way. Index: GeneticOptimizer.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b1_ADT/Optimizing/Genetic/GeneticOptimizer.cs,v retrieving revision 1.20 retrieving revision 1.21 diff -C2 -d -r1.20 -r1.21 *** GeneticOptimizer.cs 11 Aug 2006 18:43:47 -0000 1.20 --- GeneticOptimizer.cs 13 Aug 2006 19:00:55 -0000 1.21 *************** *** 258,261 **** --- 258,263 ---- { //this.run_calculateRandomFitness(); to be tested yet + if (this.genomeSize == 0 || this.genomeSize < 0) + throw new IndexOutOfRangeException("Genome size not set"); this.createFirstGeneration(showOutputToConsole); if(this.keepOnRunningUntilConvergenceIsReached) *************** *** 263,276 **** { for (int i = 0; !this.IsConvergenceReached(); i++) ! { ! this.generateNewPopulation(showOutputToConsole); ! } } else // the GO simply generates the given number of populations and then stops { for (int i = 0; i < this.generationNumber; i++) - { this.generateNewPopulation(showOutputToConsole); - } } } --- 265,274 ---- { for (int i = 0; !this.IsConvergenceReached(); i++) ! this.generateNewPopulation(showOutputToConsole); } else // the GO simply generates the given number of populations and then stops { for (int i = 0; i < this.generationNumber; i++) this.generateNewPopulation(showOutputToConsole); } } *************** *** 279,282 **** --- 277,281 ---- { this.createNextGeneration(); + this.generationCounter++; this.updateBestGenomeFoundInRunning((Genome)this.currentGeneration[populationSize-1]); this.updateWorstGenomeFoundInRunning((Genome)this.currentGeneration[0]); *************** *** 296,312 **** } ! private void createFirstGeneration(bool showOutputToConsole) { - if (this.genomeSize == 0 || this.genomeSize < 0) - throw new IndexOutOfRangeException("Genome size not set"); - this.createGenomes(); this.currentGeneration.Sort(this.genomeComparer); if(this.NewGeneration != null) ! this.NewGeneration( this , new NewGenerationEventArgs( ! this.currentGeneration , this.generationCounter , this.generationNumber ) ); this.calculateTotalSpecialFitnessForRouletteSelectionAndPlainTotalFitness(); this.updateCumulativeSpecialFitnessListForRouletteSelection(); this.setInitialBestAndWorstGenomes(); ! if (showOutputToConsole) this.showOutputToConsole(); --- 295,318 ---- } ! private void sortCurrentGenerationAndFireNewGenerationEvent() { this.currentGeneration.Sort(this.genomeComparer); if(this.NewGeneration != null) ! this.NewGeneration( this , new NewGenerationEventArgs( ! this.currentGeneration , this.generationCounter , this.generationNumber ) ); ! } ! ! private void setSpecialFitnessForRouletteSelection() ! { this.calculateTotalSpecialFitnessForRouletteSelectionAndPlainTotalFitness(); this.updateCumulativeSpecialFitnessListForRouletteSelection(); + } + + private void createFirstGeneration(bool showOutputToConsole) + { + this.createGenomes(); + this.sortCurrentGenerationAndFireNewGenerationEvent(); this.setInitialBestAndWorstGenomes(); ! this.setSpecialFitnessForRouletteSelection(); if (showOutputToConsole) this.showOutputToConsole(); *************** *** 421,439 **** } ! private void setCurrentEliteToTransmitToNextGeneration() { this.currentEliteToTransmitToNextGeneration.Clear(); for(int i = populationSize - 1; ! i >=(populationSize - this.elitismRate*this.populationSize - 1); ! i--) { ! if(this.currentGeneration[i] is Genome) ! this.currentEliteToTransmitToNextGeneration.Add((Genome)this.currentGeneration[i]); } ! } ! ! private void transmitEliteToNextGeneration() ! { for(int i = 0; i < this.currentEliteToTransmitToNextGeneration.Count; --- 427,442 ---- } ! private void createNextGeneration_transmitEliteToNextGeneration() { this.currentEliteToTransmitToNextGeneration.Clear(); for(int i = populationSize - 1; ! i >=(populationSize - this.elitismRate*this.populationSize - 1); ! i--) { ! if(this.currentGeneration[i] is Genome) ! this.currentEliteToTransmitToNextGeneration.Add((Genome)this.currentGeneration[i]); } ! for(int i = 0; i < this.currentEliteToTransmitToNextGeneration.Count; *************** *** 445,459 **** } ! private void createNextGeneration() { - this.nextGeneration.Clear(); - for (int i = 0 ; i < this.populationSize ; i+=2) { int indexForParent1 = this.rouletteSelection(); int indexForParent2 = this.rouletteSelection(); - Genome parent1, parent2; ! Genome[] childs; parent1 = ((Genome) this.currentGeneration[indexForParent1]); parent2 = ((Genome) this.currentGeneration[indexForParent2]); --- 448,459 ---- } ! private void createNextGeneration_addChildsWithRouletteSelection() { for (int i = 0 ; i < this.populationSize ; i+=2) { int indexForParent1 = this.rouletteSelection(); int indexForParent2 = this.rouletteSelection(); Genome parent1, parent2; ! Genome[] childs; parent1 = ((Genome) this.currentGeneration[indexForParent1]); parent2 = ((Genome) this.currentGeneration[indexForParent2]); *************** *** 461,504 **** { childs = this.genomeManager.GetChilds(parent1, parent2); ! } else {//if the crossover doesn't take place there are only ! //two childs, identical to parents childs = new Genome[2]; childs[0] = parent1.Clone(); childs[1] = parent2.Clone(); } ! foreach(Genome g in childs){ ! this.nextGeneration.Add(g); ! } } ! this.setCurrentEliteToTransmitToNextGeneration(); ! this.transmitEliteToNextGeneration(); ! ! this.mutateGenomes(this.nextGeneration); ! this.calculateFitnessAndMeaningForAllGenomes(this.nextGeneration); ! this.nextGeneration.Sort(this.genomeComparer); this.updateCurrentGeneration(); ! this.currentGeneration.Sort(this.genomeComparer); ! if(this.NewGeneration != null) ! this.NewGeneration( this , new NewGenerationEventArgs( ! this.currentGeneration , this.generationCounter , this.generationNumber ) ); ! this.calculateTotalSpecialFitnessForRouletteSelectionAndPlainTotalFitness(); ! this.updateCumulativeSpecialFitnessListForRouletteSelection(); ! ! this.generationCounter++; } ! //mutate all genomes of the given population, according to the mutation rate ! private void mutateGenomes(ArrayList populationOfGenomes) { ! foreach(Genome g in populationOfGenomes) this.genomeManager.Mutate(g,this.MutationRate); } ! //calculate Fitness and Meaning for each genome in populationOfGenomes ! private void calculateFitnessAndMeaningForAllGenomes(ArrayList populationOfGenomes) { ! foreach(Genome g in populationOfGenomes) { if(!g.HasBeenCloned || g.HasBeenChanged) --- 461,498 ---- { childs = this.genomeManager.GetChilds(parent1, parent2); ! } else {//if the crossover doesn't take place there are only ! //two childs, identical to parents childs = new Genome[2]; childs[0] = parent1.Clone(); childs[1] = parent2.Clone(); } ! foreach(Genome g in childs) ! this.nextGeneration.Add(g); } ! } ! ! private void createNextGeneration() ! { ! this.nextGeneration.Clear(); ! this.createNextGeneration_addChildsWithRouletteSelection(); ! this.createNextGeneration_transmitEliteToNextGeneration(); ! this.mutateGenomes(); ! this.calculateFitnessAndMeaningForAllGenomes(); this.updateCurrentGeneration(); ! this.sortCurrentGenerationAndFireNewGenerationEvent(); ! this.setSpecialFitnessForRouletteSelection(); } ! private void mutateGenomes() { ! foreach(Genome g in this.nextGeneration) this.genomeManager.Mutate(g,this.MutationRate); } ! private void calculateFitnessAndMeaningForAllGenomes() { ! foreach(Genome g in this.nextGeneration) { if(!g.HasBeenCloned || g.HasBeenChanged) *************** *** 512,518 **** } } ! private void updateCurrentGeneration() { this.currentGeneration.Clear(); int numOfNextGeneration = this.nextGeneration.Count; --- 506,513 ---- } } ! private void updateCurrentGeneration() { + this.nextGeneration.Sort(this.genomeComparer); this.currentGeneration.Clear(); int numOfNextGeneration = this.nextGeneration.Count; |