Advertisement
h-collector

Untitled

Jan 14th, 2012
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.14 KB | None | 0 0
  1.     protected void doOnePtCrossover(Chromosome Chrom1, Chromosome Chrom2)
  2.     {
  3.         String sNewChrom1, sNewChrom2;
  4.         int iCrossoverPoint;
  5.         String sChrom1, sChrom2;
  6.  
  7.         iCrossoverPoint = getRandom(chromosomeDim - 2);
  8.         sChrom1 = Chrom1.getGenesAsStr();
  9.         sChrom2 = Chrom2.getGenesAsStr();
  10.  
  11.         // CREATE OFFSPRING ONE
  12.         sNewChrom1 =
  13.             sChrom1.substring(0, iCrossoverPoint)
  14.                 + sChrom2.substring(iCrossoverPoint, chromosomeDim);
  15.  
  16.         // CREATE OFFSPRING TWO
  17.         sNewChrom2 =
  18.             sChrom2.substring(0, iCrossoverPoint)
  19.                 + sChrom1.substring(iCrossoverPoint, chromosomeDim);
  20.  
  21.         ((ChromChars) Chrom1).setGenesFromStr(sNewChrom1);
  22.         ((ChromChars) Chrom2).setGenesFromStr(sNewChrom2);
  23.     }
  24.  
  25.     protected void doOnePtCrossover(Chromosome Chrom1, Chromosome Chrom2)
  26.     {
  27.         super.doOnePtCrossover(Chrom1, Chrom2); //do normal crossover
  28.  
  29.         //  Now eliminate any duplicated genes by replacing duplicates with genes which
  30.         //were left out of the chromosome.
  31.         //  For example if gene space = 'ABCDEFGH' and sChrom1 = 'ABCDDEFG', then the
  32.         //gene 'D' is duplicated and 'H' has been left out. Swap the 2nd 'D' with 'H' and
  33.         //the problem is fixed.
  34.  
  35.         String sChrom1 = getChromWithoutDuplicates(Chrom1.getGenesAsStr());
  36.         String sChrom2 = getChromWithoutDuplicates(Chrom2.getGenesAsStr());
  37.         ((ChromChars)Chrom1).setGenesFromStr(sChrom1);
  38.         ((ChromChars)Chrom2).setGenesFromStr(sChrom2);
  39.     }
  40.  
  41.     protected String getChromWithoutDuplicates(String sChromosome)
  42.     {
  43.         int iPos;
  44.         int iRandomGeneLeftOut;
  45.         String sGene, sGenesLeftOut, sRestOfChrom;
  46.  
  47.         //first get a string (which acts as a list) of all genes left OUT of this chrom
  48.         sGenesLeftOut = "";
  49.         for (int i = 0; i < this.possGeneValues.length(); i++)
  50.         {
  51.             sGene = "" + this.possGeneValues.charAt(i);
  52.             iPos = sChromosome.indexOf(sGene);
  53.             if (iPos < 0) //this gene not found in chromosome
  54.                 sGenesLeftOut += sGene;
  55.         }
  56.  
  57.         if (sGenesLeftOut.length() == 0) //no duplicate genes, so exit
  58.             return (sChromosome);
  59.  
  60.         StringBuffer sbChromosome = new StringBuffer(sChromosome);
  61.         StringBuffer sbGenesLeftOut = new StringBuffer(sGenesLeftOut);
  62.  
  63.         for (int i = 0; i < chromosomeDim; i++)
  64.         {
  65.             sGene = "" + sbChromosome.charAt(i);
  66.             sRestOfChrom = sbChromosome.substring(i + 1, chromosomeDim);
  67.  
  68.             iPos = sRestOfChrom.indexOf(sGene);
  69.             if (iPos > -1) //gene also found in a later part of the chromosome, it is duplicated!
  70.             {
  71.                 //assign this duplicate gene a random value from the list of genes left out
  72.                 iRandomGeneLeftOut = getRandom(sbGenesLeftOut.length());
  73.                 sbChromosome.setCharAt(iPos + i + 1, sbGenesLeftOut.charAt(iRandomGeneLeftOut));
  74.  
  75.                 //now take this "gene left out" out of the list (string) of available genes
  76.                 sbGenesLeftOut.deleteCharAt(iRandomGeneLeftOut);
  77.             }
  78.         }
  79.  
  80.         return (sbChromosome.toString());
  81.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement