[Quantproject-developers] QuantProject/b1_ADT/Optimizing/Genetic GeneticOptimizer.cs, 1.16, 1.17 Ge
Brought to you by:
glauco_1
Update of /cvsroot/quantproject/QuantProject/b1_ADT/Optimizing/Genetic In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv22618/b1_ADT/Optimizing/Genetic Modified Files: GeneticOptimizer.cs Genome.cs GenomeManagement.cs GenomeManagerTest.cs IGenomeManager.cs Log Message: Changed IGenomeManager interface. Now, properties MinValueForGenes and MaxValueForGenes have been replaced by GetMinValueForGenes(int genePosition) and GetMaxValueForGenes(int genePosition) methods. Property CurrentGeneticOptimizer has been deleted from the interface. Modified the constructor of Genome class: now a GeneticOptimizer object is necessary for a creating a new instance of Genome. Some useless code has been deleted through out the classes used for genetic optimization. Index: GenomeManagerTest.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b1_ADT/Optimizing/Genetic/GenomeManagerTest.cs,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** GenomeManagerTest.cs 8 Nov 2005 18:32:25 -0000 1.5 --- GenomeManagerTest.cs 7 Aug 2006 21:03:24 -0000 1.6 *************** *** 37,42 **** private int minValueForGenes; private int maxValueForGenes; ! private GeneticOptimizer currentGeneticOptimizer; ! public int GenomeSize { --- 37,41 ---- private int minValueForGenes; private int maxValueForGenes; ! public int GenomeSize { *************** *** 44,62 **** } ! public int MinValueForGenes ! { ! get{return this.minValueForGenes;} ! } ! ! public int MaxValueForGenes ! { ! get{return this.maxValueForGenes;} ! } ! ! public GeneticOptimizer CurrentGeneticOptimizer ! { ! get{return this.currentGeneticOptimizer;} ! set{this.currentGeneticOptimizer = value;} ! } public GenomeManagerTest(int genomeSize, int minValueForGenes, int maxValueForGenes) --- 43,47 ---- } ! public GenomeManagerTest(int genomeSize, int minValueForGenes, int maxValueForGenes) *************** *** 68,75 **** } public int GetNewGeneValue(Genome genome, int genePosition) { ! return GenomeManagement.RandomGenerator.Next(genome.MinValueForGenes, ! genome.MaxValueForGenes + 1); } --- 53,70 ---- } + public int GetMinValueForGenes(int genePosition) + { + return this.minValueForGenes; + } + + public int GetMaxValueForGenes(int genePosition) + { + return this.maxValueForGenes; + } + public int GetNewGeneValue(Genome genome, int genePosition) { ! return GenomeManagement.RandomGenerator.Next(genome.GetMinValueForGenes(genePosition), ! genome.GetMaxValueForGenes(genePosition) + 1); } Index: Genome.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b1_ADT/Optimizing/Genetic/Genome.cs,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** Genome.cs 2 Aug 2006 21:55:42 -0000 1.12 --- Genome.cs 7 Aug 2006 21:03:24 -0000 1.13 *************** *** 35,40 **** { private int[] genes; - private int minValueForGenes; - private int maxValueForGenes; private int size; private double fitness; --- 35,38 ---- *************** *** 44,47 **** --- 42,46 ---- private IGenomeManager genomeManager; + private GeneticOptimizer geneticOptimizer; private int generation; *************** *** 84,95 **** } ! public int MinValueForGenes { ! get{return this.minValueForGenes;} } ! ! public int MaxValueForGenes { ! get{return this.maxValueForGenes;} } --- 83,94 ---- } ! public int GetMinValueForGenes(int genePosition) { ! return this.genomeManager.GetMinValueForGenes(genePosition); } ! ! public int GetMaxValueForGenes(int genePosition) { ! return this.genomeManager.GetMaxValueForGenes(genePosition); } *************** *** 120,129 **** /// It creates a new genome object initialized by a IGenomeManager /// </summary> ! public Genome(IGenomeManager genomeManager) { this.genomeManager = genomeManager; this.size = this.genomeManager.GenomeSize; - this.minValueForGenes = this.genomeManager.MinValueForGenes; - this.maxValueForGenes = this.genomeManager.MaxValueForGenes; this.genes = new int[ this.size ]; } --- 119,128 ---- /// It creates a new genome object initialized by a IGenomeManager /// </summary> ! public Genome(IGenomeManager genomeManager, ! GeneticOptimizer geneticOptimizer) { this.genomeManager = genomeManager; + this.geneticOptimizer = geneticOptimizer; this.size = this.genomeManager.GenomeSize; this.genes = new int[ this.size ]; } *************** *** 140,144 **** //whenever at least one gene has been written, //the current generation number is stored ! this.generation = this.genomeManager.CurrentGeneticOptimizer.GenerationCounter; } --- 139,143 ---- //whenever at least one gene has been written, //the current generation number is stored ! this.generation = this.geneticOptimizer.GenerationCounter; } *************** *** 154,158 **** public Genome Clone() { ! Genome returnValue = new Genome(this.genomeManager); returnValue.CopyValuesInGenes(this.genes); returnValue.Fitness = this.Fitness; --- 153,157 ---- public Genome Clone() { ! Genome returnValue = new Genome(this.genomeManager, this.geneticOptimizer); returnValue.CopyValuesInGenes(this.genes); returnValue.Fitness = this.Fitness; *************** *** 175,190 **** //whenever at least one gene has been written, //the current generation number is stored ! this.generation = this.genomeManager.CurrentGeneticOptimizer.GenerationCounter; } public void SetGeneValue(int geneValue, int genePosition) { ! if(genePosition >= this.size || genePosition<0) ! throw new IndexOutOfRangeException("Gene position not valid for the genome! "); this.genes[genePosition] = geneValue; //whenever at least one gene has been written, //the current generation number is stored ! this.generation = this.genomeManager.CurrentGeneticOptimizer.GenerationCounter; this.hasBeenChanged = true; } --- 174,191 ---- //whenever at least one gene has been written, //the current generation number is stored ! this.generation = this.geneticOptimizer.GenerationCounter; } public void SetGeneValue(int geneValue, int genePosition) { ! if(geneValue < this.GetMinValueForGenes(genePosition) || ! geneValue > this.GetMaxValueForGenes(genePosition) ) ! throw new IndexOutOfRangeException("Gene value not valid for the gene at" + ! " the given position!"); this.genes[genePosition] = geneValue; //whenever at least one gene has been written, //the current generation number is stored ! this.generation = this.geneticOptimizer.GenerationCounter; this.hasBeenChanged = true; } *************** *** 192,198 **** public int GetGeneValue(int genePosition) { - if(genePosition >= this.size || genePosition<0) - throw new IndexOutOfRangeException("Gene position not valid for the genome! "); - return this.genes[genePosition]; } --- 193,196 ---- Index: IGenomeManager.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b1_ADT/Optimizing/Genetic/IGenomeManager.cs,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** IGenomeManager.cs 8 Nov 2005 18:32:25 -0000 1.3 --- IGenomeManager.cs 7 Aug 2006 21:03:24 -0000 1.4 *************** *** 30,50 **** /// an instance of the GeneticOptimizer class. /// ! /// The interface determines the genome format and provides: /// - an objective function used to calculate genome's fitness; ! /// - a decode function used to calculate genome's meaning as an object; /// - a GetChilds method used by the genetic optimizer to generate new ! /// population; /// - a Mutate method used by the genetic optimizer to mutate a /// given genome /// </summary> public interface IGenomeManager { int GenomeSize{get;} ! int MinValueForGenes{get;} ! int MaxValueForGenes{get;} ! GeneticOptimizer CurrentGeneticOptimizer{get;set;} ! ! int GetNewGeneValue(Genome genome, int genePosition); // Used in generation of genes ! // by the Genome parameter double GetFitnessValue(Genome genome); object Decode(Genome genome); --- 30,49 ---- /// an instance of the GeneticOptimizer class. /// ! /// The interface determines the genome format (size and min and max vaules for genes) ! /// and provides: /// - an objective function used to calculate genome's fitness; ! /// - a decode function used to determine genome's meaning as an object; /// - a GetChilds method used by the genetic optimizer to generate new ! /// offspring; /// - a Mutate method used by the genetic optimizer to mutate a /// given genome + /// - a method for production of new genes value (GetNewGeneValue) /// </summary> public interface IGenomeManager { int GenomeSize{get;} ! int GetMaxValueForGenes(int genePosition); ! int GetMinValueForGenes(int genePosition); ! int GetNewGeneValue(Genome genome, int genePosition); double GetFitnessValue(Genome genome); object Decode(Genome genome); Index: GenomeManagement.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b1_ADT/Optimizing/Genetic/GenomeManagement.cs,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** GenomeManagement.cs 2 Aug 2006 17:19:30 -0000 1.9 --- GenomeManagement.cs 7 Aug 2006 21:03:24 -0000 1.10 *************** *** 178,183 **** { if (GenomeManagement.RandomGenerator.Next(0,101) < (int)(mutationRate*100)) ! genome.SetGeneValue(GenomeManagement.RandomGenerator.Next(genome.MinValueForGenes, ! genome.MaxValueForGenes + 1), pos); } } --- 178,183 ---- { if (GenomeManagement.RandomGenerator.Next(0,101) < (int)(mutationRate*100)) ! genome.SetGeneValue(GenomeManagement.RandomGenerator.Next(genome.GetMinValueForGenes(pos), ! genome.GetMaxValueForGenes(pos) + 1), pos); } } *************** *** 188,193 **** if (GenomeManagement.RandomGenerator.Next(0,101) < (int)(mutationRate*100)) ! genome.SetGeneValue(GenomeManagement.RandomGenerator.Next(genome.MinValueForGenes, ! genome.MaxValueForGenes + 1), genePosition); } --- 188,193 ---- if (GenomeManagement.RandomGenerator.Next(0,101) < (int)(mutationRate*100)) ! genome.SetGeneValue(GenomeManagement.RandomGenerator.Next(genome.GetMinValueForGenes(genePosition), ! genome.GetMaxValueForGenes(genePosition) + 1), genePosition); } *************** *** 272,279 **** { initializeStaticMembers(parent1, parent2); - if(parent1.Size > (parent1.MaxValueForGenes - parent1.MinValueForGenes + 1)) - //it is impossible not to duplicate genes if size is too - // large for the range of variation of each gene - throw new Exception("Impossible to avoid duplicates with the given size!"); if(parent1.Size != parent2.Size) throw new Exception("Genomes must have the same size!"); --- 272,275 ---- Index: GeneticOptimizer.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b1_ADT/Optimizing/Genetic/GeneticOptimizer.cs,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** GeneticOptimizer.cs 4 Aug 2006 15:30:41 -0000 1.16 --- GeneticOptimizer.cs 7 Aug 2006 21:03:24 -0000 1.17 *************** *** 51,56 **** private int generationNumber; private int genomeSize; - private int minValueForGenes; - private int maxValueForGenes; private double totalFitness; private Genome bestGenome; --- 51,54 ---- *************** *** 211,216 **** this.generationNumber = generationNumber; this.genomeSize = this.genomeManager.GenomeSize; - this.minValueForGenes = this.genomeManager.MinValueForGenes; - this.maxValueForGenes = this.genomeManager.MaxValueForGenes; this.genomeComparer = new GenomeComparer(); this.cumulativeFitnessList = new ArrayList(this.PopulationSize); --- 209,212 ---- *************** *** 221,225 **** eliteNumber); this.generationCounter = 1; - this.genomeManager.CurrentGeneticOptimizer = this; } --- 217,220 ---- *************** *** 393,397 **** for (int i = 0; i < this.populationSize; i++) { ! Genome g = new Genome(this.genomeManager); g.CreateGenes(); g.CalculateFitness(); --- 388,392 ---- for (int i = 0; i < this.populationSize; i++) { ! Genome g = new Genome(this.genomeManager, this); g.CreateGenes(); g.CalculateFitness(); |