Advertisement
Konark

Untitled

Nov 29th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.79 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Spesyvets Laba 607m</title>
  8. <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  9. <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>
  10. <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
  11. </head>
  12. <body>
  13.  
  14. <!-- Готовим контейнер для диаграммы -->
  15. <div id="content" align="center">
  16. <div id="chart_div" style="width: 900px; height: 500px;"></div>
  17. </div>
  18.  
  19. <script>
  20.  
  21. var x = [26, 50, 55, 54, 60, 47, 30, 30, 12, 15, 16, 21, 50, 51, 50, 48, 12, 15, 29, 54, 55, 67, 10, 6, 65, 40],
  22. y = [29, 40, 50, 10, 15, 66, 60, 50, 17, 14, 19, 48, 30, 42, 15, 21, 38, 56, 39, 38, 57, 41, 70, 25, 27, 60],
  23. mas = [],
  24. x0 = 40,
  25. y0 = 40,
  26. d_max = 17.0,
  27. r = [],
  28. x_r = [],
  29. sum_x_r = 0,
  30. x_n = [],
  31. Q = [],
  32. F = [],
  33. C = [];
  34.  
  35. C[0] = 10100;
  36. C[1] = 35;
  37. C[2] = 2800;
  38. C[3] = 4500;
  39. C[4] = 3000;
  40.  
  41. let sum = 0, max = 0, el, basetoclient = [];
  42. var pairarray = [], v = 1, savearr = [], savearrgc = [], savearr1 = [], vuzol = [], distance, indexs = 0, gc = [];
  43.  
  44. for(let i = 0; i < x.length; i++) {
  45. mas[i] = [];
  46. mas[i][0] = i+1;
  47. mas[i][1] = x[i];
  48. mas[i][2] = y[i];
  49. }
  50.  
  51. for (i = 0; i < 4; i++) {
  52. r[i] = Math.random()*1;
  53. }
  54.  
  55. F[0] = d_max*(1+r[0]);
  56.  
  57. for (n = 1; n < 4; n++) {
  58. F[n] = F[n-1]*(1+r[n]);
  59. }
  60.  
  61. for (e = 0; e < x.length; e++) {
  62. x_r[e] = Math.random()*1;
  63. sum_x_r += x_r[e];
  64. }
  65.  
  66. for (k = 0; k < x.length; k++) {
  67. x_n[k] = x_r[k]/sum_x_r;
  68. }
  69.  
  70. var orders = [];
  71.  
  72. for (var t = 0; t < F.length; t++){
  73. orders[t] = [];
  74.  
  75. for (var j = 0; j < x.length; j++){
  76. orders[t][j] = F[t] * x_n[j];
  77. }
  78. }
  79.  
  80. var max_ord = orders[0][0];
  81.  
  82. for (var t = 0; t < F.length; t++){
  83. for (var j = 0; j < x.length; j++){
  84. if (orders[t][j] > max_ord) max_ord = orders[t][j];
  85. }
  86. }
  87.  
  88. var D_sim = [];
  89.  
  90.  
  91. for(let i = 0; i < x.length; i++)
  92. D_sim.push([]);
  93. for (let j = 0; j < x.length; j++) {
  94. for (let t = 0; t < x.length; t++) {
  95. D_sim[j][t] = Math.sqrt(Math.pow(x[j]-x[t], 2) + Math.pow(y[j]-y[t], 2));
  96. D_sim[j][t] = parseFloat(D_sim[j][t].toFixed(2));
  97. }
  98. }
  99.  
  100. for (let i = 0; i < mas.length; i++) {
  101. basetoclient[i] = [];
  102. basetoclient[i][0] = i + 1;
  103. basetoclient[i][1] = Math.sqrt(Math.pow(mas[i][1] - x0, 2) + Math.pow(mas[i][2] - y0, 2));
  104. basetoclient[i][1] = parseFloat(basetoclient[i][1].toFixed(2));
  105. }
  106. var ctx = document.getElementById("myChart");
  107. // var scatterChart = new Chart(ctx, {
  108. // type: 'scatter',
  109. // data: {
  110. // datasets: [{
  111. // label: 'Замовники',
  112. // data: x.map(function(v,i){return {x: v, y: y[i]};}),
  113. // backgroundColor: ["#008000"]
  114. // }, {
  115. // label: 'Базовий вузол',
  116. // data: [{
  117. // x: x0,
  118. // y: y0
  119. // }],
  120. // backgroundColor: ["#f00"]
  121. // }
  122. // ]
  123. // },
  124. // options: {
  125. // scales: {
  126. // xAxes: [{
  127. // type: 'linear',
  128. // position: 'bottom'
  129. // }]
  130. // }
  131. // }
  132. // });
  133.  
  134. google.charts.load('current', {'packages':['corechart']});
  135. google.charts.setOnLoadCallback(drawChart);
  136. function drawChart() {
  137. var data = google.visualization.arrayToDataTable([
  138. ['X', 'Y', {'type': 'string', 'role': 'style'}],
  139. [ x0, y0, 'point { size: 10; fill-color: #a52714; }'],
  140. [ x[0], y[0], null],
  141. [ x[1], y[1], null],
  142. [ x[2], y[2], null],
  143. [ x[3], y[3], null],
  144. [ x[4], y[4], null],
  145. [ x[5], y[5], null],
  146. [ x[6], y[6], null],
  147. [ x[7], y[7], null],
  148. [ x[8], y[8], null],
  149. [ x[9], y[9], null],
  150. [ x[10], y[10], null],
  151. [ x[11], y[11], null],
  152. [ x[12], y[12], null],
  153. [ x[13], y[13], null],
  154. [ x[14], y[14], null],
  155. [ x[15], y[15], null],
  156. [ x[16], y[16], null],
  157. [ x[17], y[17], null],
  158. [ x[18], y[18], null],
  159. [ x[19], y[19], null],
  160. [ x[20], y[20], null],
  161. [ x[21], y[21], null],
  162. [ x[22], y[22], null],
  163. [ x[23], y[23], null],
  164. [ x[24], y[24], null],
  165. [ x0, y0, 'point { size: 10; fill-color: #a52714; }']
  166. ]);
  167.  
  168. var options = {
  169. legend: 'none',
  170. lineWidth: 1
  171. };
  172.  
  173. var chart = new google.visualization.ScatterChart(document.getElementById('chart_div'));
  174.  
  175. chart.draw(data, options);
  176. }
  177.  
  178.  
  179.  
  180. toBuildGC();
  181.  
  182. function mySort(a, b) {
  183. return b[2] - a[2];
  184. }
  185.  
  186.  
  187. function toBuildGC() {
  188. // ctx.clearRect(0, 0, 545, 540);
  189. // gc.splice(0, gc.length);
  190. // toDrawCanvas(ctx);
  191. // ctx.strokeStyle = "blue";
  192. //save
  193.  
  194. for (let i = 0; i < mas.length; i++) {
  195. for (let j = v; j < mas.length; j++) {
  196. if (i == j) { j++ };
  197. pairarray.push(i + "," + j);
  198. }
  199. v++;
  200. }
  201.  
  202. console.log(pairarray);
  203.  
  204. // console.log("pairs");
  205. //console.log(pairarray);
  206. //console.log(" saving таблиця");
  207. for (let i = 0; i < 300; i++) {
  208. savearr[i] = [];
  209. savearr1[i] = [];
  210. savearrgc[i] = [];
  211. vuzol = pairarray[i].split(",");
  212. console.log(vuzol[0][1]);
  213. savearr[i][0] = parseFloat((basetoclient[vuzol[0]][1]).toFixed(2));
  214. savearr[i][1] = parseFloat((basetoclient[vuzol[1]][1]).toFixed(2));
  215. savearr[i][2] = parseFloat((D_sim[vuzol[0]][vuzol[1]]).toFixed(2));
  216. //для позначення номерів вузлів
  217. savearr1[i][0] = parseFloat(vuzol[0]) + 1;
  218. savearr1[i][1] = parseFloat(vuzol[1]) + 1;
  219. savearr1[i][2] = parseFloat((savearr[i][0] + savearr[i][1] - savearr[i][2]).toFixed(2));
  220. //console.log(savearr[i][0]+" + "+savearr[i][1]+" - "+ savearr[i][2] );
  221. //console.log("s "+savearr1[i][2] );//console.log(savearr1[0]);
  222. //для Гамільтонового циклу
  223. savearrgc[i][0] = parseFloat(vuzol[0]) + 1;
  224. savearrgc[i][1] = parseFloat(vuzol[1]) + 1;
  225. savearrgc[i][2] = parseFloat((savearr[i][0] + savearr[i][1] - savearr[i][2]).toFixed(2));
  226. }
  227. savearr = savearr.sort(mySort);
  228. savearr1 = savearr1.sort(mySort);
  229. savearrgc = savearrgc.sort(mySort);
  230. console.log(savearr);
  231. let last, index, toDelete, toDelete1, count = 0;
  232. gc.push(0);
  233. gc.push(savearr1[0][0]);
  234. gc.push(savearr1[0][1]);
  235. toDelete = gc[1];
  236. delDupl(toDelete);
  237. last = gc[2];
  238. for (let i = 0; i < savearrgc.length; i++) {
  239. index = savearrgc[i].indexOf(last);
  240. if (index >= 0) {
  241. count++;
  242. ///знаходження найкоротшого шляху
  243. if (count == 1) {
  244. let dis1, dis2, stock3;
  245. if (index == 0) {
  246. stock3 = savearrgc[i][index + 1] - 1;
  247. } else if (index == 1) {
  248. stock3 = savearrgc[i][index - 1] - 1;
  249. }
  250. //0-15-16-17
  251. dis1 = basetoclient[gc[1] - 1][1] + D_sim[gc[1] - 1][gc[2] - 1] + D_sim[gc[2] - 1][stock3];
  252. //0-16-15-17
  253. dis2 = basetoclient[gc[2] - 1][1] + D_sim[gc[2] - 1][gc[1] - 1] + D_sim[gc[1] - 1][stock3];
  254. //вибираэмо коротший шлях
  255. if (dis2 < dis1) {
  256. gc[1] = savearr1[0][1];
  257. gc[2] = savearr1[0][0];
  258. last = gc[2];
  259. // console.log("d1= " + dis1 + " d2= " + dis2);
  260. toDelete1 = gc[1];
  261. }
  262. }
  263.  
  264. if (index == 0) {
  265. gc.push(savearrgc[i][index + 1]);
  266. } else if (index == 1) {
  267. gc.push(savearrgc[i][index - 1]);
  268. }
  269. last = gc[gc.length - 1];
  270. toDelete = gc[gc.length - 2];
  271. delDupl(toDelete);
  272. delDupl(toDelete1);
  273. i = 0;
  274. }
  275. }
  276. gc.push(0);
  277. console.log(" gc: " + gc);
  278. // for (let i = 0; i < gc.length; i++) {
  279. // if (i == 0) {
  280. // ctx.moveTo(x0 * scale + 25, 520 - y0 * scale);
  281. // } else {
  282. // for (let j = 0; j < mas.length; j++) {
  283. // if (mas[j][0] == gc[i]) {
  284. // ctx.lineTo(mas[j][1] * scale + 25, 520 - mas[j][2] * scale);
  285. // }
  286. // }
  287. // } if (i == (gc.length - 1)) {
  288. // ctx.lineTo(x0 * scale + 25, 520 - y0 * scale);
  289. // }
  290. // ctx.stroke();
  291. // }
  292. }
  293.  
  294. function delDupl(delstring) {
  295. for (let i = 0; i < savearrgc.length; i++) {
  296. if (savearrgc[i].indexOf(delstring) >= 0) {
  297. savearrgc[i].splice(0, 3);
  298. }
  299. }
  300. }
  301.  
  302. </script>
  303. </body>
  304. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement