[Quantproject-developers] QuantProject/b1_ADT/Optimizing/Genetic GeneticOptimizer.cs,1.6,1.7
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2005-07-25 22:27:58
|
Update of /cvsroot/quantproject/QuantProject/b1_ADT/Optimizing/Genetic In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20238/b1_ADT/Optimizing/Genetic Modified Files: GeneticOptimizer.cs Log Message: - Added if condition for calling NewGeneration event (just to avoid run time errors if no handler has been added to the event) - added new constructor: now it is possible to set directly the randomGenerator Index: GeneticOptimizer.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b1_ADT/Optimizing/Genetic/GeneticOptimizer.cs,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** GeneticOptimizer.cs 23 Jul 2005 18:02:23 -0000 1.6 --- GeneticOptimizer.cs 25 Jul 2005 22:27:49 -0000 1.7 *************** *** 41,45 **** { #region fields ! private static Random random = new Random((int)DateTime.Now.Ticks); private double mutationRate; --- 41,45 ---- { #region fields ! private Random random; private double mutationRate; *************** *** 143,147 **** this.populationSize = populationSize; this.generationNumber = generationNumber; ! this.commonInitialization(); } --- 143,147 ---- this.populationSize = populationSize; this.generationNumber = generationNumber; ! this.random = new Random((int)DateTime.Now.Ticks); this.commonInitialization(); } *************** *** 158,162 **** this.generationNumber = generationNumber; this.genomeManager = genomeManager; ! this.commonInitialization(); } --- 158,176 ---- this.generationNumber = generationNumber; this.genomeManager = genomeManager; ! this.random = new Random((int)DateTime.Now.Ticks); ! this.commonInitialization(); ! } ! ! public GeneticOptimizer(double crossoverRate, double mutationRate, double elitismRate, ! int populationSize, int generationNumber, ! IGenomeManager genomeManager, int seedForRandomGenerator) ! { ! this.crossoverRate = crossoverRate; ! this.mutationRate = mutationRate; ! this.elitismRate = elitismRate; ! this.populationSize = populationSize; ! this.generationNumber = generationNumber; ! this.genomeManager = genomeManager; ! this.random = new Random(seedForRandomGenerator); this.commonInitialization(); } *************** *** 234,239 **** this.createGenomes(); this.currentGeneration.Sort(this.genomeComparer); ! this.NewGeneration( this , new NewGenerationEventArgs( ! this.currentGeneration , this.generationCounter , this.generationNumber ) ); this.calculateTotalFitness(); this.updateCumulativeFitnessList(); --- 248,254 ---- this.createGenomes(); this.currentGeneration.Sort(this.genomeComparer); ! if(this.NewGeneration != null) ! this.NewGeneration( this , new NewGenerationEventArgs( ! this.currentGeneration , this.generationCounter , this.generationNumber ) ); this.calculateTotalFitness(); this.updateCumulativeFitnessList(); *************** *** 263,267 **** private int rouletteSelection() { ! double randomFitness = this.totalFitness *(double)GeneticOptimizer.random.Next(1,1001)/1000; int idx = -1; int first = 0; --- 278,282 ---- private int rouletteSelection() { ! double randomFitness = this.totalFitness *(double)this.random.Next(1,1001)/1000; int idx = -1; int first = 0; *************** *** 294,303 **** { this.totalFitness = 0.0; - for (int i = 0; i < this.populationSize; i++) - { - Genome g = ((Genome) this.currentGeneration[i]); - this.totalFitness += g.Fitness; - } } --- 309,316 ---- { this.totalFitness = 0.0; + foreach(Genome g in this.currentGeneration) + this.totalFitness += g.Fitness; + } *************** *** 364,368 **** parent1 = ((Genome) this.currentGeneration[indexForParent1]); parent2 = ((Genome) this.currentGeneration[indexForParent2]); ! if ((double)GeneticOptimizer.random.Next(1,1001)/1000 < this.crossoverRate) { childs = this.genomeManager.GetChilds(parent1, parent2); --- 377,381 ---- parent1 = ((Genome) this.currentGeneration[indexForParent1]); parent2 = ((Genome) this.currentGeneration[indexForParent2]); ! if ((double)this.random.Next(1,1001)/1000 < this.crossoverRate) { childs = this.genomeManager.GetChilds(parent1, parent2); *************** *** 387,392 **** this.updateCurrentGeneration(); this.currentGeneration.Sort(this.genomeComparer); ! this.NewGeneration( this , new NewGenerationEventArgs( ! this.currentGeneration , this.generationCounter , this.generationNumber ) ); this.calculateTotalFitness(); this.updateCumulativeFitnessList(); --- 400,406 ---- this.updateCurrentGeneration(); this.currentGeneration.Sort(this.genomeComparer); ! if(this.NewGeneration != null) ! this.NewGeneration( this , new NewGenerationEventArgs( ! this.currentGeneration , this.generationCounter , this.generationNumber ) ); this.calculateTotalFitness(); this.updateCumulativeFitnessList(); |