[Quantproject-developers] QuantProject/b1_ADT/Optimizing/Genetic Genome.cs,1.2,1.3 GenomeManagement.
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2005-06-10 18:48:58
|
Update of /cvsroot/quantproject/QuantProject/b1_ADT/Optimizing/Genetic In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21005/b1_ADT/Optimizing/Genetic Modified Files: Genome.cs GenomeManagement.cs GenomeManagerTest.cs Log Message: Fixed bug in MixGenesWithoutDuplicates method in GenomeManagement class. Index: GenomeManagerTest.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b1_ADT/Optimizing/Genetic/GenomeManagerTest.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** GenomeManagerTest.cs 1 Dec 2004 22:36:11 -0000 1.1 --- GenomeManagerTest.cs 10 Jun 2005 18:48:45 -0000 1.2 *************** *** 72,75 **** --- 72,76 ---- //return GenomeManagement.AlternateFixedCrossover(parent1, parent2); return GenomeManagement.OnePointCrossover(parent1, parent2); + //return GenomeManagement.MixGenesWithoutDuplicates(parent1, parent2); } Index: Genome.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b1_ADT/Optimizing/Genetic/Genome.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Genome.cs 17 May 2005 23:03:36 -0000 1.2 --- Genome.cs 10 Jun 2005 18:48:44 -0000 1.3 *************** *** 109,118 **** } public Genome Clone() { Genome returnValue = new Genome(this.genomeManager); returnValue.Fitness = this.Fitness; returnValue.Meaning = this.Meaning; - returnValue.CopyValuesInGenes(this.genes); return returnValue; --- 109,122 ---- } + + /// <summary> + /// It creates an new genome, identical to the current instance + /// </summary> public Genome Clone() { Genome returnValue = new Genome(this.genomeManager); + returnValue.CopyValuesInGenes(this.genes); returnValue.Fitness = this.Fitness; returnValue.Meaning = this.Meaning; return returnValue; Index: GenomeManagement.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b1_ADT/Optimizing/Genetic/GenomeManagement.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** GenomeManagement.cs 1 Dec 2004 22:36:11 -0000 1.1 --- GenomeManagement.cs 10 Jun 2005 18:48:44 -0000 1.2 *************** *** 35,60 **** { ! public static Random RandomGenerator = new Random((int)DateTime.Now.Ticks); private static int genomeSize; private static Genome[] childs; private static int[,] maskForChilds; ! /* ! public GenomeManagement() { ! } ! */ /// <summary> ! /// Returns an array of genome (childs) based /// on classical one point crossover genes recombination /// </summary> public static Genome[] OnePointCrossover(Genome parent1, Genome parent2) { ! if(parent1.Size != parent2.Size) throw new Exception("Genomes must have the same size!"); ! GenomeManagement.genomeSize = parent1.Size; ! GenomeManagement.childs = new Genome[2]{parent1.Clone(),parent1.Clone()}; ! ! int pos = GenomeManagement.RandomGenerator.Next(0, parent1.Size); for(int i = 0 ; i < parent1.Size ; i++) { --- 35,76 ---- { ! public static Random RandomGenerator; private static int genomeSize; private static Genome[] childs; private static int[,] maskForChilds; ! ! static GenomeManagement() { ! RandomGenerator = new Random((int)DateTime.Now.Ticks); ! childs = new Genome[2]; } ! ! private static void initializeStaticMembers(Genome parent1, Genome parent2) ! { ! genomeSize = parent1.Size; ! childs[0] = parent1.Clone(); ! childs[1] = parent2.Clone(); ! //the two childs now points to their parents ! maskForChilds = new int[childs.Length, genomeSize]; ! } ! ! private static void assignFitnessAndMeaningToChilds() ! { ! foreach(Genome child in childs) ! { ! child.AssignMeaning(); ! child.CalculateFitness(); ! } ! } /// <summary> ! /// Returns an array of genome (length = 2) based /// on classical one point crossover genes recombination /// </summary> public static Genome[] OnePointCrossover(Genome parent1, Genome parent2) { ! GenomeManagement.initializeStaticMembers(parent1, parent2); ! if(parent1.Size != parent2.Size) throw new Exception("Genomes must have the same size!"); ! int pos = GenomeManagement.RandomGenerator.Next(0, parent1.Size); for(int i = 0 ; i < parent1.Size ; i++) { *************** *** 70,81 **** } } ! return GenomeManagement.childs; } private static void setMaskForChildsForUniformCrossover() { ! for(int childIndex = 0 ; ! childIndex < GenomeManagement.childs.Length; ! childIndex++) { for(int i = 0 ; i < GenomeManagement.genomeSize ; i++) --- 86,96 ---- } } ! GenomeManagement.assignFitnessAndMeaningToChilds(); ! return GenomeManagement.childs; } private static void setMaskForChildsForUniformCrossover() { ! for(int childIndex = 0; childIndex < 2; childIndex++) { for(int i = 0 ; i < GenomeManagement.genomeSize ; i++) *************** *** 88,145 **** private static void setMaskForChildsForAlternateFixedCrossover() { ! for(int childIndex = 0 ; ! childIndex < GenomeManagement.childs.Length; ! childIndex++) { ! for(int i = 0 ; i < GenomeManagement.genomeSize ; i++) { ! if(i%2 == 0) ! //index is even ! maskForChilds[childIndex, i] = 1; else ! // index is odd ! maskForChilds[childIndex, i] = 2; } } } ! ! private static void setMaskForChildsForMixingWithoutDuplicates(Genome parent1, Genome parent2) { ! for(int childIndex = 0 ; ! childIndex < GenomeManagement.childs.Length; ! childIndex++) { ! for(int i = 0 ; i < GenomeManagement.genomeSize ; i++) ! { ! if(parent2.HasGene(parent1.GetGeneValue(i)) || ! parent1.HasGene(parent2.GetGeneValue(i)))//index contains a common gene ! { ! maskForChilds[childIndex, i] = childIndex + 1; ! } ! else// index doesn't contain a common gene ! { ! if(i%2 == 0)//index is even ! maskForChilds[childIndex, i] = childIndex%2 + 1; ! else// index is odd ! maskForChilds[childIndex, i] = childIndex%2 + 2; ! } ! } } } - //it assumes just two parents only private static void setChildsUsingMaskForChilds(Genome parent1, Genome parent2) { ! for(int childIndex = 0 ; ! childIndex < GenomeManagement.childs.Length; ! childIndex++) { ! for(int i = 0 ; i < GenomeManagement.genomeSize ; i++) { ! if(maskForChilds[childIndex,i]==1) ! childs[childIndex].SetGeneValue(parent1.GetGeneValue(i), i); ! else//maskForChilds[childIndex,i]==2 ! childs[childIndex].SetGeneValue(parent2.GetGeneValue(i), i); } } --- 103,173 ---- private static void setMaskForChildsForAlternateFixedCrossover() { ! for(int childIndex = 0; childIndex < 2; childIndex++) { ! for(int genePos = 0; genePos < genomeSize; genePos++) { ! if(genePos%2 == 0) ! //gene position is even ! maskForChilds[childIndex, genePos] = 1; else ! // gene position is odd ! maskForChilds[childIndex, genePos] = 2; } } } ! ! private static int firstGenePositionOfParent1NotPresentInParent2(Genome parent1, ! Genome parent2) ! { ! int returnValue = -1; ! for(int genePos = 0 ; ! genePos < GenomeManagement.genomeSize && returnValue == -1; ! genePos++) ! { ! if(!parent2.HasGene(parent1.GetGeneValue(genePos))) ! returnValue = genePos; ! } ! return returnValue; ! } ! ! private static bool setMaskForChildsForMixingWithoutDuplicates(Genome parent1, Genome parent2) { ! bool returnValue = false; ! int firstGenePosOfParent1NotPresentInParent2 = ! firstGenePositionOfParent1NotPresentInParent2(parent1, parent2); ! int firstGenePosOfParent2NotPresentInParent1 = ! firstGenePositionOfParent1NotPresentInParent2(parent2, parent1); ! if(firstGenePosOfParent1NotPresentInParent2 > -1 && ! firstGenePosOfParent2NotPresentInParent1 > -1 ) ! //there is at least a gene in parent1 not present in parent2 and viceversa { ! for(int genePos = 0 ; genePos < GenomeManagement.genomeSize ; genePos++) ! { ! if(genePos == firstGenePosOfParent1NotPresentInParent2) ! maskForChilds[0, genePos] = 1; ! else ! maskForChilds[0, genePos] = 2; ! ! if(genePos == firstGenePosOfParent2NotPresentInParent1) ! maskForChilds[1, genePos] = 2; ! else ! maskForChilds[1, genePos] = 1; ! } ! returnValue = true; } + return returnValue; } private static void setChildsUsingMaskForChilds(Genome parent1, Genome parent2) { ! for(int childIndex = 0; childIndex < 2; childIndex++) { ! for(int genePos = 0 ; genePos < GenomeManagement.genomeSize ; genePos++) { ! if(maskForChilds[childIndex,genePos]==1) ! childs[childIndex].SetGeneValue(parent1.GetGeneValue(genePos), genePos); ! else//maskForChilds[childIndex,genePos]==2 ! childs[childIndex].SetGeneValue(parent2.GetGeneValue(genePos), genePos); } } *************** *** 153,166 **** public static Genome[] UniformCrossover(Genome parent1, Genome parent2) { if(parent1.Size != parent2.Size) throw new Exception("Genomes must have the same size!"); ! GenomeManagement.genomeSize = parent1.Size; ! GenomeManagement.childs = ! new Genome[2]{parent1.Clone(),parent1.Clone()}; ! GenomeManagement.maskForChilds = new int[childs.Length, GenomeManagement.genomeSize]; ! GenomeManagement.setMaskForChildsForUniformCrossover(); ! GenomeManagement.setChildsUsingMaskForChilds(parent1, parent2); ! return GenomeManagement.childs; } --- 181,191 ---- public static Genome[] UniformCrossover(Genome parent1, Genome parent2) { + initializeStaticMembers(parent1, parent2); if(parent1.Size != parent2.Size) throw new Exception("Genomes must have the same size!"); ! setMaskForChildsForUniformCrossover(); ! setChildsUsingMaskForChilds(parent1, parent2); ! return childs; } *************** *** 171,189 **** public static Genome[] AlternateFixedCrossover(Genome parent1, Genome parent2) { if(parent1.Size != parent2.Size) throw new Exception("Genomes must have the same size!"); ! GenomeManagement.genomeSize = parent1.Size; ! GenomeManagement.childs = ! new Genome[2]{parent1.Clone(),parent1.Clone()}; ! GenomeManagement.maskForChilds = new int[childs.Length, GenomeManagement.genomeSize]; ! GenomeManagement.setMaskForChildsForAlternateFixedCrossover(); ! GenomeManagement.setChildsUsingMaskForChilds(parent1, parent2); ! return GenomeManagement.childs; } /// <summary> /// This method returns an array of genomes based on ! /// a mix of the genes of parents, such that childs, /// if possible, are different from parents and, at /// the same time, childs' genes are not duplicated --- 196,211 ---- public static Genome[] AlternateFixedCrossover(Genome parent1, Genome parent2) { + initializeStaticMembers(parent1, parent2); if(parent1.Size != parent2.Size) throw new Exception("Genomes must have the same size!"); ! setMaskForChildsForAlternateFixedCrossover(); ! setChildsUsingMaskForChilds(parent1, parent2); ! return childs; } /// <summary> /// This method returns an array of genomes based on ! /// a mix of the genes of parents, such that the 2 childs, /// if possible, are different from parents and, at /// the same time, childs' genes are not duplicated *************** *** 191,194 **** --- 213,217 ---- public static Genome[] MixGenesWithoutDuplicates(Genome parent1, Genome parent2) { + initializeStaticMembers(parent1, parent2); if(parent1.Size > (parent1.MaxValueForGenes - parent1.MinValueForGenes + 1)) //it is impossible not to duplicate genes if size is too *************** *** 197,208 **** if(parent1.Size != parent2.Size) throw new Exception("Genomes must have the same size!"); - GenomeManagement.genomeSize = parent1.Size; - GenomeManagement.childs = - new Genome[2]{parent1.Clone(),parent1.Clone()}; - GenomeManagement.maskForChilds = new int[childs.Length, GenomeManagement.genomeSize]; - GenomeManagement.setMaskForChildsForMixingWithoutDuplicates(parent1, parent2); - GenomeManagement.setChildsUsingMaskForChilds(parent1, parent2); ! return GenomeManagement.childs; } --- 220,228 ---- if(parent1.Size != parent2.Size) throw new Exception("Genomes must have the same size!"); ! if(setMaskForChildsForMixingWithoutDuplicates(parent1, parent2)) ! setChildsUsingMaskForChilds(parent1, parent2); ! ! return childs; } |