[Quantproject-developers] QuantProject/b1_ADT/Optimizing/Genetic GenomeManagement.cs,1.5,1.6
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2005-07-31 19:53:53
|
Update of /cvsroot/quantproject/QuantProject/b1_ADT/Optimizing/Genetic In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15756/b1_ADT/Optimizing/Genetic Modified Files: GenomeManagement.cs Log Message: Moved specific methods for the GenomeManagerForTheEfficientPortfolio from GenomeManagement to the new GenomeManipulator class, created in the namespace QuantProject.Scripts.TickerSelectionTesting.EfficientPortfolios; - modified MixGenesWithoutDuplicates, that now tries to mix all genes of the two parents in offspring (without duplication) Index: GenomeManagement.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b1_ADT/Optimizing/Genetic/GenomeManagement.cs,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** GenomeManagement.cs 25 Jul 2005 22:30:32 -0000 1.5 --- GenomeManagement.cs 31 Jul 2005 19:53:44 -0000 1.6 *************** *** 65,68 **** --- 65,83 ---- } + 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); + } + } + + } /// <summary> /// It sets randomGenerator used by all other public methods *************** *** 103,264 **** } ! private static void setMaskForChildsForUniformCrossover() ! { ! for(int childIndex = 0; childIndex < 2; childIndex++) ! { ! for(int i = 0 ; i < GenomeManagement.genomeSize ; i++) ! { ! maskForChilds[childIndex,i] = GenomeManagement.RandomGenerator.Next(1,3); ! } ! } ! } ! ! 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 constToDiscoverGenesDuplicates) { - int returnValue = -1; - for(int genePos = 0 ; - genePos < GenomeManagement.genomeSize && returnValue == -1; - genePos++) - { - int geneValue = parent1.GetGeneValue(genePos); - if(!parent2.HasGene(geneValue) && - !parent2.HasGene(geneValue + constToDiscoverGenesDuplicates) && - !parent2.HasGene(geneValue - constToDiscoverGenesDuplicates)) - returnValue = genePos; - } - return returnValue; - } - - private static bool setMaskForChildsForMixingWithoutDuplicates(Genome parent1, Genome parent2, - int constToDiscoverGenesDuplicates) - { - bool returnValue = false; - int firstGenePosOfParent1NotPresentInParent2 = - firstGenePositionOfParent1NotPresentInParent2(parent1, parent2, constToDiscoverGenesDuplicates); - int firstGenePosOfParent2NotPresentInParent1 = - firstGenePositionOfParent1NotPresentInParent2(parent2, parent1, constToDiscoverGenesDuplicates); - 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); } } ! ! } ! /// <summary> ! /// This method returns an array of genomes based on ! /// a random recombination of the genes of parents ! /// </summary> 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; } /// <summary> ! /// This method returns an array of genomes based on ! /// an alternate fixed recombination of the genes of parents ! /// </summary> 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; } ! private static void launchExIfAChildHasDuplicateGenes(int constToDiscoverGenesDuplicates) ! { ! foreach(Genome gen in childs) ! { ! if(gen.HasSomeDuplicateGenes(constToDiscoverGenesDuplicates)) ! throw new Exception("A child with duplicate genes has been generated!"); ! } ! ! } ! /// <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 ! /// </summary> ! /// <param name="parent1">First genome parent from which genes are to be mixed in offspring</param> ! /// <param name="parents">Second genome parent from which genes are to be mixed in offspring</param> ! /// <param name="constToDiscoverGenesDuplicates">Gene y is a duplicate of gene x iff y = x or y = x + constToDiscoverGenesDuplicates</param> ! public static Genome[] MixGenesWithoutDuplicates(Genome parent1, Genome parent2, ! int constToDiscoverGenesDuplicates) ! { ! 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!"); ! ! if(setMaskForChildsForMixingWithoutDuplicates(parent1, parent2, ! constToDiscoverGenesDuplicates)) ! setChildsUsingMaskForChilds(parent1, parent2); ! launchExIfAChildHasDuplicateGenes(constToDiscoverGenesDuplicates); ! return childs; ! } ! ! ! static public void MutateAllGenes(Genome genome, double mutationRate) { for (int pos = 0 ; pos < genome.Size; pos++) --- 118,177 ---- } ! private static void setMaskForChildsForUniformCrossover() { for(int childIndex = 0; childIndex < 2; childIndex++) { ! for(int i = 0 ; i < GenomeManagement.genomeSize ; i++) ! { ! maskForChilds[childIndex,i] = GenomeManagement.RandomGenerator.Next(1,3); } } ! } /// <summary> ! /// This method returns an array of genomes based on ! /// a random recombination of the genes of parents ! /// </summary> 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; } + 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; + } + } + } /// <summary> ! /// This method returns an array of genomes based on ! /// an alternate fixed recombination of the genes of parents ! /// </summary> 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; } ! static public void MutateAllGenes(Genome genome, double mutationRate) { for (int pos = 0 ; pos < genome.Size; pos++) |