[Quantproject-developers] QuantProject/b1_ADT/Optimizing/Genetic GenomeManagement.cs, 1.7, 1.8
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2006-07-02 19:51:39
|
Update of /cvsroot/quantproject/QuantProject/b1_ADT/Optimizing/Genetic In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv13547/b1_ADT/Optimizing/Genetic Modified Files: GenomeManagement.cs Log Message: Added MixGenesWithoutDuplicates method to the class (it is a general method that can be used in general by IGenomeManager objects) Index: GenomeManagement.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b1_ADT/Optimizing/Genetic/GenomeManagement.cs,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** GenomeManagement.cs 7 Aug 2005 08:20:53 -0000 1.7 --- GenomeManagement.cs 2 Jul 2006 19:51:36 -0000 1.8 *************** *** 43,47 **** static GenomeManagement() { ! RandomGenerator = new Random((int)DateTime.Now.Ticks); childs = new Genome[2]; } --- 43,47 ---- static GenomeManagement() { ! RandomGenerator = new Random(ConstantsProvider.SeedForRandomGenerator); childs = new Genome[2]; } *************** *** 54,57 **** --- 54,64 ---- //the two childs now points to their parents maskForChilds = new int[childs.Length, genomeSize]; + for(int i = 0; i<genomeSize; i++) + { + maskForChilds[0,i]=1; + maskForChilds[1,i]=2; + } + //maskForChilds has been set in order to re-create + //a copy of parents by using setChildsUsingMaskForChilds() } *************** *** 190,193 **** --- 197,285 ---- } + + #region MixGenesWithoutDuplicates + + private static int[] genePositionsOfParent1NotPresentInParent2(Genome parent1, + Genome parent2) + { + int[] returnValue = new int[parent1.Size]; + for(int i = 0; i < returnValue.Length; i++) + { + returnValue[i] = - 1; + int geneValue = parent1.GetGeneValue(i); + if(geneValue >= 0) + { + if(!parent2.HasGene(geneValue) && + !parent2.HasGene(-Math.Abs(geneValue) - 1)) + returnValue[i] = i; + } + else + { + if(!parent2.HasGene(geneValue) && + !parent2.HasGene(Math.Abs(geneValue) - 1)) + returnValue[i] = i; + } + } + return returnValue; + } + + private static void setMaskForChildsForMixingWithoutDuplicates(Genome parent1, Genome parent2) + + { + int[] genePlacesOfParent1NotPresentInParent2 = + genePositionsOfParent1NotPresentInParent2(parent1, parent2); + int[] genePlacesOfParent2NotPresentInParent1 = + genePositionsOfParent1NotPresentInParent2(parent2, parent1); + bool justExchangedAtPreviousPosition = false; + for(int i = 0;i<parent1.Size;i++) + { + if(!justExchangedAtPreviousPosition) + //exchanges between genes of parents in childs + //must follow an alternate pattern, in order to + //avoid plain copy of parents in childs when all genes + //of the first parent are not in the second one (and viceversa) + { + if(genePlacesOfParent2NotPresentInParent1[i]!= - 1) + { + maskForChilds[0, i] = 2; + justExchangedAtPreviousPosition = true; + } + if(genePlacesOfParent1NotPresentInParent2[i]!= - 1) + { + maskForChilds[1, i] = 1; + justExchangedAtPreviousPosition = true; + } + } + else + justExchangedAtPreviousPosition = false; + } + } + + /// <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| - 1 + /// or y = -|x| -1</param> + 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 + // 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!"); + + setMaskForChildsForMixingWithoutDuplicates(parent1, parent2); + setChildsUsingMaskForChilds(parent1, parent2); + //throwExcIfAChildHasDuplicateGenes(); //just for debugging purposes + return childs; + } + #endregion } } |