Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.31 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Identifier table</title>
  5. <script src="main.js"></script>
  6. </head>
  7. <body style="background-color: black; color: grey;">
  8. <?php
  9.  
  10. $parent = array();
  11. $solution = array();
  12.  
  13. for($i = 0; $i < 20; $i++) {
  14. $parent[$i] = rand(0, 20) * 180;
  15. //$parent[$i] = 1800;
  16. }
  17.  
  18. print_individual('numb', array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20));
  19. print_individual('parents', $parent);
  20.  
  21. for($i = 0; $i < 20; $i++) {
  22. $solution[$i] = 4 * sin(deg2rad($parent[$i] / 20)) + 10;
  23. }
  24.  
  25. print_individual('solutions', $solution);
  26.  
  27. $rate = survival_rate($solution);
  28. $sum = array_sum($rate);
  29.  
  30. for($i = 0; $i < 20; $i++) {
  31. $rate[$i] = ($rate[$i] / $sum) * 100;
  32. }
  33.  
  34. print_individual('survival rates', $rate);
  35.  
  36. $pairs = generate_pairs($rate);
  37. print_individual_arr('pairs', $pairs);
  38.  
  39. $new_generation = array();
  40. for($i = 0; $i < 20; $i++){
  41. $new_generation[$i] = one_point_cross($parent[$pairs[$i][0]], $parent[$pairs[$i][1]]);
  42. }
  43. print_individual('new_gen', $new_generation);
  44.  
  45. for($i = 0; $i < 50; $i++){
  46. echo '<div style="clear: left;"></div>';
  47. print_individual('parents', $new_generation);
  48. $solution = array();
  49.  
  50. for($j = 0; $j < 20; $j++) {
  51. $solution[$j] = 4 * sin(deg2rad($new_generation[$j] / 20)) + 10;
  52. }
  53.  
  54. print_individual('solutions', $solution);
  55.  
  56. $rate = survival_rate($solution);
  57. $sum = array_sum($rate);
  58.  
  59. for($j = 0; $j < 20; $j++) {
  60. $rate[$j] = ($rate[$j] / $sum) * 100;
  61. }
  62. print_individual('survival rates', $rate);
  63.  
  64. $pairs = generate_pairs($rate);
  65. print_individual_arr('pairs', $pairs);
  66.  
  67. $old_gen = $new_generation;
  68. $new_generation = array();
  69. for($j = 0; $j < 20; $j++){
  70. $new_generation[$j] = one_point_cross($old_gen[$pairs[$j][0]], $old_gen[$pairs[$j][1]]);
  71. }
  72. print_individual('new_gen', $new_generation);
  73. }
  74.  
  75. ?>
  76. </body>
  77. </html>
  78.  
  79. <?php
  80.  
  81. function print_individual_arr($name, $ind) {
  82. echo '<table style="float: left;">';
  83. echo '<th>' . "$name" . '<th>';
  84.  
  85. for($i = 0; $i < count($ind); $i++) {
  86. echo '<tr>' .
  87. '<td>';
  88. print_r($ind[$i]);
  89. echo '</td>' .
  90. '</tr>';
  91. }
  92.  
  93. echo '</table>';
  94. }
  95.  
  96. function survival_rate($sol) {
  97. $rate = array();
  98.  
  99. for($i = 0; $i < 20; $i++) {
  100. $rate[$i] = (100 - (abs($sol[$i] - 14) / 14) * 100);
  101. }
  102.  
  103. return $rate;
  104. }
  105.  
  106. function print_individual($name, $ind) {
  107. echo '<table style="float: left;">';
  108. echo '<th>' . "$name" . '<th>';
  109.  
  110. for($i = 0; $i < count($ind); $i++) {
  111. echo '<tr>' .
  112. '<td>' . $ind[$i] . '</td>' .
  113. '</tr>';
  114. }
  115.  
  116. echo '</table>';
  117. }
  118.  
  119. function generate_pairs($input)
  120. {
  121. $sum = array_sum($input);
  122. //echo '<br>'.$sum.'<br>';
  123. $pairs = array();
  124.  
  125. for($j = 0; $j < count($input); $j++){
  126. $rand[0] = rand(0,$sum);
  127. $elem_sum = 0;
  128. $i[0] = 0;
  129.  
  130. do {
  131. $elem_sum += $input[$i[0]];
  132. $i[0]++;
  133. } while ($elem_sum < $rand[0]);
  134.  
  135. do {
  136. $rand[1] = rand(0,$sum);
  137. $elem_sum = 0;
  138. $i[1] = 0;
  139.  
  140. do {
  141. $elem_sum += $input[$i[1]];
  142. $i[1]++;
  143. } while ($elem_sum < $rand[1]);
  144. } while($i[1] === $i[0]);
  145.  
  146. $pairs[$j] = array($i[0], $i[1]);
  147. }
  148.  
  149. return $pairs;
  150. }
  151.  
  152. function one_point_cross($arg1, $arg2) {
  153. //Float 2 string
  154. $strfir = $arg1 . "";
  155. $strsec = $arg2 . "";
  156.  
  157. //Amount of nums after coma
  158. $amcolfir = strlen(substr(strrchr($strfir, "."), 1));
  159. $amcolsec = strlen(substr(strrchr($strsec, "."), 1));
  160. $maxofam = max($amcolfir, $amcolsec);
  161.  
  162. //Doin' nums in dec sys
  163. $barg1 = $arg1 * pow(10, $maxofam);
  164. $barg2 = $arg2 * pow(10, $maxofam);
  165.  
  166. //Convert a num dec to bin
  167. $binfir = base_convert($barg1, 10, 2);
  168. $binsec = base_convert($barg2, 10, 2);
  169.  
  170. //REZAT' SUKU!!!!!111!!
  171. $strfir = $binfir . "";
  172. $strsec = $binsec . "";
  173.  
  174. $lenfir = strlen($strfir);
  175. $lensec = strlen($strsec);
  176.  
  177. //$accufir = substr($strfir, $acc);
  178. //$accusec = substr($strsec, 0, $acc);
  179.  
  180. //comparison of the number of digits in numbers & adding of a holes & XOR
  181.  
  182. $i = 0; //counter
  183. $holelessarr = "";
  184. $biggerarr = "";
  185.  
  186. $answarr = "";
  187. $answarrs = "";
  188.  
  189. $bigger = max($lenfir, $lensec);
  190. //if($bigger == $countfir)
  191. $less = min($lenfir, $lensec);
  192. $diff = ($bigger - $less);
  193. //Holes
  194. if ($diff>0) {
  195. for (; $i< $diff; $i++) {
  196. $holelessarr = $holelessarr . "0";
  197. }
  198. if($bigger == $lenfir) {
  199. //$holelessarr = $holelessarr . $strsec;
  200. $strsec = $holelessarr . $strsec;
  201. //$biggerarr = $strfir;
  202. } else {
  203. //$holelessarr = $holelessarr . $strfir;
  204. $strfir = $holelessarr . $strfir;
  205. //$biggerarr = $strsec;
  206. }
  207. } else {
  208. $holelessarr = $strfir;
  209. $biggerarr = $strsec;
  210. }
  211.  
  212. // Одноточечный кроссинговер
  213. $randnum = rand(1, $bigger);
  214.  
  215. for ($i = 0; $i < $randnum; $i++) {
  216. $answarr = $answarr . $strfir[$i];
  217. $answarrs = $answarrs . $strsec[$i];
  218. }
  219.  
  220. for ($i = $randnum; $i < $bigger; $i++) {
  221. $answarr = $answarr . $strsec[$i];
  222. $answarrs = $answarrs . $strfir[$i];
  223. //echo $i;
  224. }
  225.  
  226. $answer = base_convert($answarr, 2, 10) / pow(10, $maxofam);
  227. $answers = base_convert($answarrs, 2, 10) / pow(10, $maxofam);
  228.  
  229. if(4 * sin(deg2rad($answer / 20)) + 10 < 4 * sin(deg2rad($answers / 20)) + 10) {$answer = $answers;}
  230.  
  231. return mutation(base_convert($answer, 10, 2), 0);
  232. }
  233.  
  234. function mutation($binary, $chanse) {
  235.  
  236. $rand_numb = rand(0, 100);
  237.  
  238. if($rand_numb <= $chanse) {
  239. echo "BITCH";
  240. $bit_numb = rand(0, strlen($binary) -1);
  241.  
  242. if($binary[$bit_numb] == "0") {
  243. $binary[$bit_numb] = "1";
  244. } else {
  245. $binary[$bit_numb] = "0";
  246. }
  247. }
  248.  
  249. return base_convert($binary, 2, 10);
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement