Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. <?
  2.  
  3. $length=10;
  4.  
  5. function set_routes($num_elements){
  6. $elements_length=array();
  7. $count=$num_elements;
  8. for ($i=1; $i <$num_elements ; $i++) {
  9.  
  10. for ($j=$i+1; $j <$count+1; $j++) {
  11. $elements_length[$i][$j]=rand(1,42);
  12. }
  13.  
  14. }
  15. return $elements_length;
  16. }
  17. $route=set_routes($length);
  18.  
  19. function chromosome($length){
  20. $chromosome=array();
  21. for ($i=0; $i <$length ; $i++) {
  22. $chromosome[]=$i+1;
  23. }
  24. for ($i=$length-1; $i >-1 ; $i--) {
  25. $j=rand(0,$length-1);
  26. $buf=$chromosome[$i];
  27. $chromosome[$i]=$chromosome[$j];
  28. $chromosome[$j]=$buf;
  29. }
  30. return $chromosome;
  31. }
  32.  
  33. function chr_len($chr){
  34. $route=0;
  35. for ($i=0; $i < count($chr)-1 ; $i++) {
  36. if ($chr[$i]<$chr[$i+1]){
  37. $route+=$GLOBALS['route'][$chr[$i]][$chr[$i+1]];
  38. }else{
  39. $route+=$GLOBALS['route'][$chr[$i+1]][$chr[$i]];
  40. }
  41.  
  42. }
  43. return $route;
  44. }
  45.  
  46. function crossingover($chr1,$chr2,$length){
  47. $count=round($length/2);
  48. $count=$count+round($count/2);
  49. $child1=array();
  50. $child2=array();
  51. for ($i=0; $i <$count ; $i++) {
  52. $child1[]=$chr1[$i];
  53. $child2[]=$chr2[$i];
  54. }
  55. for ($i=$count; $i <$length ; $i++) {
  56. $child1[]=$chr2[$i];
  57. $child2[]=$chr1[$i];
  58. }
  59.  
  60. if (chr_len($child1)<chr_len($child2)){
  61. return $child1;
  62. }else{
  63. return $child2;
  64. }
  65. }
  66.  
  67.  
  68. crossingover(chromosome($length),chromosome($length),$length);
  69. $population=array();
  70. for ($i=0; $i <70000 ; $i++) {
  71. $population[]=chromosome($length);
  72. }
  73. do{
  74. $child=crossingover($population[rand(0,count($population)-1)],$population[rand(0,count($population)-1)],$length);
  75. }while (count(array_unique($child)<$length)) ;
  76. print_r($child);
  77. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement