drstein7

ESCX Ver2

Nov 9th, 2020 (edited)
1,116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.07 KB | None | 0 0
  1. <?php
  2.  
  3.     $parent1 = array(1, 5, 7, 3, 6, 4, 2);
  4.     $parent2 = array(1, 6, 2, 4, 3, 5, 7);
  5.     $child = array_fill(0, count($parent1), null);
  6.      
  7.     //Αρχικοποίηση    
  8.     $bestValue = $parent1[0];
  9.     $x = $parent1[1];
  10.     $y = $parent2[1];
  11.     $child[0]=$parent1[0];
  12.  
  13.     for ($i = 1, $len = count($parent1); $i < $len; $i++) {
  14.  
  15.         //Καλύτερη τιμή
  16.         if ($x != $y) {
  17.             $value1 = distanceTo($bestValue,$x) + checkMin($x,$child,$len);
  18.             $value2 = distanceTo($bestValue,$y) + checkMin($y,$child,$len);
  19.             if ($value1 < $value2 ) {
  20.                 $bestValue = $x;
  21.             }
  22.             else {
  23.                 $bestValue = $y;
  24.             }
  25.         }
  26.         else {
  27.             $bestValue = $x;
  28.         }
  29.        
  30.         //Επόμενη γονίδιο
  31.         $x = checkNext($bestValue ,$parent1,$child);
  32.         $y = checkNext($bestValue ,$parent2,$child);
  33.  
  34.         //Καλύτερο γονίδιο στον απόγονο
  35.         $child[$i] = $bestValue;
  36.            
  37.     }
  38.    
  39.     echo 'Απόσταση πρώτου γονέα: '.totalDistance($parent1).'<br>';
  40.     echo 'Απόσταση δεύτερου γονέα: '.totalDistance($parent2).'<br>';
  41.     echo 'Απόσταση απογόνου: '.totalDistance($child).'<br>';
  42.     echo 'Απόγονος: ';
  43.     print_r ($child);
  44.  
  45.     function checkMin($x,$child,$len) {
  46.         $min = INF;
  47.         for ($i = 0; $i < $len; $i++) {
  48.             if (in_array($i,$child) === false) {
  49.                 $min = $min > distanceTo($x,$i) ?  distanceTo($x,$i) : $min;    
  50.             }        
  51.         }  
  52.  
  53.         return $min;      
  54.     }
  55.    
  56.     function checkNext($z, $parent, &$child) {  
  57.  
  58.         $flag = 0;
  59.         $position = 0;
  60.         $x = array_search($z,$parent);
  61.        
  62.         for ($i = $x, $len = count($parent)-1; $i < $len ; $i++) {
  63.             if (in_array($parent[$i+1],$child) === false) {
  64.                 $flag = 1;
  65.                 $position = $parent[$i+1] ;
  66.                 break;
  67.             }
  68.         }  
  69.        
  70.         if ($flag === 0) {    
  71.             for ($i = 0, $len = count($parent)-1; $i < $len ; $i++) {
  72.                 if (in_array($parent[$i],$child) === false) {
  73.                     $position = $parent[$i];
  74.                     break;
  75.                 }
  76.             }            
  77.         }
  78.  
  79.         return $position;
  80.     }
  81.    
  82.     function distanceTo($x,$y) {    
  83.    
  84.         $dis = array (
  85.             array(999,75,99,9,35,63,8),
  86.             array(51,999,86,46,88,29,20),
  87.             array(100,5,999,16,28,35,28),
  88.             array(20,45,11,999,59,53,49),
  89.             array(86,63,33,65,999,76,72),
  90.             array(36,53,89,31,21,999,52),
  91.             array(58,31,43,67,52,60,999)
  92.         );
  93.        
  94.         return $dis[$x-1][$y-1];
  95.     }
  96.    
  97.     function totalDistance($x) {
  98.         $d=0;
  99.         for ($i=0; $i<count($x)-1 ;$i++) {
  100.             $d+=distanceTo($x[$i],$x[$i+1]);
  101.         }
  102.         $d+=distanceTo($x[$i],$x[0]);
  103.         return $d;
  104.     }        
  105. ?>
Advertisement
Add Comment
Please, Sign In to add comment