[Quantproject-developers] QuantProject/b7_Scripts/TickerSelectionTesting GenomeManipulator.cs, 1.8
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2007-01-03 23:20:49
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv4394/b7_Scripts/TickerSelectionTesting Modified Files: GenomeManipulator.cs GenomeManagerForWeightedEfficientPortfolio.cs GenomeManagerForEfficientPortfolio.cs GenomeManagerForEfficientOTCCTOPortfolio.cs Log Message: Deleted the mutationRate parameter from the IGenomeManager's Mutate method. Now classes implementing Mutate method just need to pass only the genome that has to be mutated to the method. As the mutationRate is now used only by the GeneticOptimizer, all the methods that effectively mutate genes of a given genome don't need anymore the mutation rate parameter. Index: GenomeManagerForEfficientPortfolio.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/GenomeManagerForEfficientPortfolio.cs,v retrieving revision 1.26 retrieving revision 1.27 diff -C2 -d -r1.26 -r1.27 *** GenomeManagerForEfficientPortfolio.cs 22 Oct 2006 18:30:18 -0000 1.26 --- GenomeManagerForEfficientPortfolio.cs 3 Jan 2007 23:20:14 -0000 1.27 *************** *** 295,299 **** } ! public virtual void Mutate(Genome genome, double mutationRate) { // in this implementation only one gene is mutated --- 295,299 ---- } ! public virtual void Mutate(Genome genome) { // in this implementation only one gene is mutated *************** *** 312,316 **** genome.GetMaxValueForGenes(genePositionToBeMutated) + 1); } ! GenomeManagement.MutateOneGene(genome, mutationRate, genePositionToBeMutated, newValueForGene); } --- 312,316 ---- genome.GetMaxValueForGenes(genePositionToBeMutated) + 1); } ! GenomeManagement.MutateOneGene(genome, genePositionToBeMutated, newValueForGene); } Index: GenomeManagerForEfficientOTCCTOPortfolio.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/GenomeManagerForEfficientOTCCTOPortfolio.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** GenomeManagerForEfficientOTCCTOPortfolio.cs 17 Sep 2006 21:48:39 -0000 1.2 --- GenomeManagerForEfficientOTCCTOPortfolio.cs 3 Jan 2007 23:20:14 -0000 1.3 *************** *** 38,42 **** /// </summary> [Serializable] ! public class GenomeManagerForEfficientOTCCTOPortfolio : GenomeManagerForEfficientPortfolio { --- 38,42 ---- /// </summary> [Serializable] ! public class GenomeManagerForEfficientOTCCTOPortfolio : GenomeManagerForWeightedEfficientPortfolio { Index: GenomeManagerForWeightedEfficientPortfolio.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/GenomeManagerForWeightedEfficientPortfolio.cs,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** GenomeManagerForWeightedEfficientPortfolio.cs 7 Aug 2006 21:03:24 -0000 1.5 --- GenomeManagerForWeightedEfficientPortfolio.cs 3 Jan 2007 23:20:13 -0000 1.6 *************** *** 143,167 **** return returnValue; } ! ! public override void Mutate(Genome genome, double mutationRate) { - // in this implementation only one gene is mutated int genePositionToBeMutated = GenomeManagement.RandomGenerator.Next(genome.Size); ! int newValueForGene = GenomeManagement.RandomGenerator.Next(genome.GetMinValueForGenes(genePositionToBeMutated), ! genome.GetMaxValueForGenes(genePositionToBeMutated) +1); ! ! while(genePositionToBeMutated%2 == 1 && ! GenomeManipulator.IsTickerContainedInGenome(newValueForGene,genome)) ! //while in the proposed genePositionToBeMutated has to be stored ! //a new gene pointing to a ticker and ! //the proposed newValueForGene points to a ticker ! //already stored in the given genome { ! newValueForGene = GenomeManagement.RandomGenerator.Next(genome.GetMinValueForGenes(genePositionToBeMutated), ! genome.GetMaxValueForGenes(genePositionToBeMutated) +1); } - GenomeManagement.MutateOneGene(genome, mutationRate, - genePositionToBeMutated, newValueForGene); } } --- 143,233 ---- return returnValue; } ! ! #region override Mutate ! ! //OLD VERSION ! // public override void Mutate(Genome genome, double mutationRate) ! // { ! // // in this implementation only one gene is mutated ! // int genePositionToBeMutated = GenomeManagement.RandomGenerator.Next(genome.Size); ! // int newValueForGene = GenomeManagement.RandomGenerator.Next(genome.GetMinValueForGenes(genePositionToBeMutated), ! // genome.GetMaxValueForGenes(genePositionToBeMutated) +1); ! // ! // while(genePositionToBeMutated%2 == 1 && ! // GenomeManipulator.IsTickerContainedInGenome(newValueForGene,genome)) ! // //while in the proposed genePositionToBeMutated has to be stored ! // //a new gene pointing to a ticker and ! // //the proposed newValueForGene points to a ticker ! // //already stored in the given genome ! // { ! // newValueForGene = GenomeManagement.RandomGenerator.Next(genome.GetMinValueForGenes(genePositionToBeMutated), ! // genome.GetMaxValueForGenes(genePositionToBeMutated) +1); ! // } ! // GenomeManagement.MutateOneGene(genome, mutationRate, ! // genePositionToBeMutated, newValueForGene); ! // } ! ! private int mutate_MutateOnlyOneWeight_getNewWeight(Genome genome, int genePositionToBeMutated) ! { ! int returnValue; ! double partOfGeneToSubtractOrAdd = 0.25; ! int geneValue = Math.Abs(genome.GetGeneValue(genePositionToBeMutated)); ! int subtractOrAdd = GenomeManagement.RandomGenerator.Next(2); ! if(subtractOrAdd == 1)//subtract a part of the gene value from the gene value itself ! returnValue = geneValue - Convert.ToInt32(partOfGeneToSubtractOrAdd*geneValue); ! else ! returnValue = Math.Min(genome.GetMaxValueForGenes(genePositionToBeMutated), ! geneValue + Convert.ToInt32(partOfGeneToSubtractOrAdd*geneValue)); ! return returnValue; ! } ! ! private void mutate_MutateOnlyOneWeight(Genome genome) { int genePositionToBeMutated = GenomeManagement.RandomGenerator.Next(genome.Size); ! while(genePositionToBeMutated%2 == 1 ) ! //while the proposed genePositionToBeMutated points to a ticker ! genePositionToBeMutated = GenomeManagement.RandomGenerator.Next(genome.Size); ! ! int newValueForGene = this.mutate_MutateOnlyOneWeight_getNewWeight(genome, genePositionToBeMutated); ! ! GenomeManagement.MutateOneGene(genome, genePositionToBeMutated, newValueForGene); ! } ! ! private void mutate_MutateAllGenes(Genome genome) ! { ! for(int genePositionToBeMutated = 0; ! genePositionToBeMutated < genome.Genes().Length; ! genePositionToBeMutated ++) { ! int newValueForGene = ! GenomeManagement.RandomGenerator.Next(genome.GetMinValueForGenes(genePositionToBeMutated), ! genome.GetMaxValueForGenes(genePositionToBeMutated) + 1); ! while(genePositionToBeMutated%2 == 1 ! && GenomeManipulator.IsTickerContainedInGenome(newValueForGene,genome)) ! //while in the given position has to be stored ! //a new gene pointing to a ticker and ! //the proposed newValueForGene points to a ticker ! //already stored in the given genome ! // a new newalueForGene has to be generated ! newValueForGene = GenomeManagement.RandomGenerator.Next(genome.GetMinValueForGenes(genePositionToBeMutated), ! genome.GetMaxValueForGenes(genePositionToBeMutated) + 1); ! ! genome.SetGeneValue(newValueForGene, genePositionToBeMutated); } } + + //new version + //mutation means just a change in one single weight + //or in a complete new genome (with a probability of 50%) + public override void Mutate(Genome genome) + { + int mutateOnlyOneWeight = GenomeManagement.RandomGenerator.Next(2); + if(mutateOnlyOneWeight == 1) + this.mutate_MutateOnlyOneWeight(genome); + else//mutate all genome's genes + this.mutate_MutateAllGenes(genome); + } + + #endregion } Index: GenomeManipulator.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/GenomeManipulator.cs,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** GenomeManipulator.cs 22 Aug 2006 19:09:35 -0000 1.8 --- GenomeManipulator.cs 3 Jan 2007 23:20:12 -0000 1.9 *************** *** 261,265 **** } ! } } --- 261,293 ---- } ! ! /// <summary> ! /// Returns true if a given gene, when decoded by the ! /// GenomeManagerForEfficientPortfolio, refers to a ! /// ticker already contained in a given genome ! /// </summary> ! /// <param name="geneValueCorrespondingToATicker">Gene, corresponding to a certain ticker, that has to be checked</param> ! /// <param name="genome">Genome containing or not the ticker geneCorrespondingToATicker refers to</param> ! /// <param name="genesPointingToTickers">The int array that contains positions of genes pointing ! /// to tickers inside genome</param> ! public static bool IsTickerContainedInGenome(int geneValueCorrespondingToATicker, ! Genome genome, int[] genePositionsPointingToTickers) ! { ! bool returnValue = false; ! for(int i = 0; i<genePositionsPointingToTickers.Length; i++) ! { ! returnValue = (genome.HasGene(geneValueCorrespondingToATicker, ! genePositionsPointingToTickers[i]) || ! (geneValueCorrespondingToATicker < 0 && ! genome.HasGene(Math.Abs(geneValueCorrespondingToATicker)-1, ! genePositionsPointingToTickers[i]) ) || ! (geneValueCorrespondingToATicker >= 0 && ! genome.HasGene(- Math.Abs(geneValueCorrespondingToATicker)- 1, ! genePositionsPointingToTickers[i])) ); ! if(returnValue) ! i = genePositionsPointingToTickers.Length;//exit from FOR ! } ! return returnValue; ! } } } |