Advertisement
h-collector

Untitled

Jan 14th, 2012
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.33 KB | None | 0 0
  1.     protected function doOnePtCrossover(Chromosome $Chrom1, Chromosome $Chrom2) {
  2.        
  3.         $genesX = /* @var $Chrom1 Solution */ $Chrom1->genes;
  4.         $genesY = /* @var $Chrom2 Solution */ $Chrom2->genes;  
  5.  
  6.         $chromsLeftX = array();
  7.         $chromsLeftY = array();
  8.  
  9.         $lPoint = mt_rand(1, $this->chromosomeDim - 2);
  10.         $rPoint = $this->chromosomeDim - 1;
  11.         for ($i = $lPoint; $i < $rPoint; ++$i){
  12.             $tmpX       = $genesX[$i];
  13.             $tmpY       = $genesY[$i];
  14.             $genesX[$i] = $tmpY;
  15.             $genesY[$i] = $tmpX;
  16.            
  17.             @$chromsLeftX[$tmpX] -= 1;
  18.             @$chromsLeftX[$tmpY] += 1;
  19.             @$chromsLeftY[$tmpY] -= 1;
  20.             @$chromsLeftY[$tmpX] += 1;
  21.            
  22.         }
  23.         //remove dulicates, satisfy demands
  24.         $minIdxX     = $rPoint;
  25.         $minIdxY     = $rPoint;
  26.         $chromsLeftX = array_filter($chromsLeftX);
  27.         $chromsLeftY = array_filter($chromsLeftY);
  28.         arsort($chromsLeftX);
  29.         arsort($chromsLeftY);
  30.         for ($run = true, $i = 0; $run && $i <= $rPoint; ++$i) {
  31.             $run = false;
  32.             if(!empty($chromsLeftX)){
  33.                 $tmpX = $genesX[$i];
  34.                 if (isset($chromsLeftX[$tmpX]) && $chromsLeftX[$tmpX] > 0) {
  35.                     end($chromsLeftX);
  36.                     $genesX[$i] = ($key = key($chromsLeftX));
  37.                     if (--$chromsLeftX[$tmpX] === 0) unset($chromsLeftX[$tmpX]);
  38.                     if (++$chromsLeftX[$key]  === 0) unset($chromsLeftX[$key]);
  39.                     if ($i < $minIdxX) $minIdxX = $i;
  40.                 }
  41.                 $run = true;
  42.             }
  43.             if(!empty($chromsLeftY)){
  44.                 $tmpY = $genesY[$i];
  45.                 if (isset($chromsLeftY[$tmpY]) && $chromsLeftY[$tmpY] > 0) {
  46.                     end($chromsLeftY);
  47.                     $genesY[$i] = ($key = key($chromsLeftY));
  48.  
  49.                     if (--$chromsLeftY[$tmpY] === 0) unset($chromsLeftY[$tmpY]);
  50.                     if (++$chromsLeftY[$key]  === 0) unset($chromsLeftY[$key]);
  51.                     if ($i < $minIdxY) $minIdxY = $i;
  52.                 }
  53.                 $run = true;
  54.             }
  55.         }
  56.        
  57.         $Chrom1->recalculateHighlyFitPatterns($minIdxX);
  58.         $Chrom2->recalculateHighlyFitPatterns($minIdxY);
  59.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement