Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <meta http-equiv="X-UA-Compatible" content="ie=edge">
- <title>Spesyvets Laba 607m</title>
- <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>
- <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
- </head>
- <body>
- <!-- Готовим контейнер для диаграммы -->
- <div id="content" align="center">
- <div id="chart_div" style="width: 900px; height: 500px;"></div>
- </div>
- <script>
- 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],
- 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],
- mas = [],
- x0 = 40,
- y0 = 40,
- d_max = 17.0,
- r = [],
- x_r = [],
- sum_x_r = 0,
- x_n = [],
- Q = [],
- F = [],
- C = [];
- C[0] = 10100;
- C[1] = 35;
- C[2] = 2800;
- C[3] = 4500;
- C[4] = 3000;
- let sum = 0, max = 0, el, basetoclient = [];
- var pairarray = [], v = 1, savearr = [], savearrgc = [], savearr1 = [], vuzol = [], distance, indexs = 0, gc = [];
- for(let i = 0; i < x.length; i++) {
- mas[i] = [];
- mas[i][0] = i+1;
- mas[i][1] = x[i];
- mas[i][2] = y[i];
- }
- for (i = 0; i < 4; i++) {
- r[i] = Math.random()*1;
- }
- F[0] = d_max*(1+r[0]);
- for (n = 1; n < 4; n++) {
- F[n] = F[n-1]*(1+r[n]);
- }
- for (e = 0; e < x.length; e++) {
- x_r[e] = Math.random()*1;
- sum_x_r += x_r[e];
- }
- for (k = 0; k < x.length; k++) {
- x_n[k] = x_r[k]/sum_x_r;
- }
- var orders = [];
- for (var t = 0; t < F.length; t++){
- orders[t] = [];
- for (var j = 0; j < x.length; j++){
- orders[t][j] = F[t] * x_n[j];
- }
- }
- var max_ord = orders[0][0];
- for (var t = 0; t < F.length; t++){
- for (var j = 0; j < x.length; j++){
- if (orders[t][j] > max_ord) max_ord = orders[t][j];
- }
- }
- var D_sim = [];
- for(let i = 0; i < x.length; i++)
- D_sim.push([]);
- for (let j = 0; j < x.length; j++) {
- for (let t = 0; t < x.length; t++) {
- D_sim[j][t] = Math.sqrt(Math.pow(x[j]-x[t], 2) + Math.pow(y[j]-y[t], 2));
- D_sim[j][t] = parseFloat(D_sim[j][t].toFixed(2));
- }
- }
- for (let i = 0; i < mas.length; i++) {
- basetoclient[i] = [];
- basetoclient[i][0] = i + 1;
- basetoclient[i][1] = Math.sqrt(Math.pow(mas[i][1] - x0, 2) + Math.pow(mas[i][2] - y0, 2));
- basetoclient[i][1] = parseFloat(basetoclient[i][1].toFixed(2));
- }
- var ctx = document.getElementById("myChart");
- // var scatterChart = new Chart(ctx, {
- // type: 'scatter',
- // data: {
- // datasets: [{
- // label: 'Замовники',
- // data: x.map(function(v,i){return {x: v, y: y[i]};}),
- // backgroundColor: ["#008000"]
- // }, {
- // label: 'Базовий вузол',
- // data: [{
- // x: x0,
- // y: y0
- // }],
- // backgroundColor: ["#f00"]
- // }
- // ]
- // },
- // options: {
- // scales: {
- // xAxes: [{
- // type: 'linear',
- // position: 'bottom'
- // }]
- // }
- // }
- // });
- google.charts.load('current', {'packages':['corechart']});
- google.charts.setOnLoadCallback(drawChart);
- function drawChart() {
- var data = google.visualization.arrayToDataTable([
- ['X', 'Y', {'type': 'string', 'role': 'style'}],
- [ x0, y0, 'point { size: 10; fill-color: #a52714; }'],
- [ x[0], y[0], null],
- [ x[1], y[1], null],
- [ x[2], y[2], null],
- [ x[3], y[3], null],
- [ x[4], y[4], null],
- [ x[5], y[5], null],
- [ x[6], y[6], null],
- [ x[7], y[7], null],
- [ x[8], y[8], null],
- [ x[9], y[9], null],
- [ x[10], y[10], null],
- [ x[11], y[11], null],
- [ x[12], y[12], null],
- [ x[13], y[13], null],
- [ x[14], y[14], null],
- [ x[15], y[15], null],
- [ x[16], y[16], null],
- [ x[17], y[17], null],
- [ x[18], y[18], null],
- [ x[19], y[19], null],
- [ x[20], y[20], null],
- [ x[21], y[21], null],
- [ x[22], y[22], null],
- [ x[23], y[23], null],
- [ x[24], y[24], null],
- [ x0, y0, 'point { size: 10; fill-color: #a52714; }']
- ]);
- var options = {
- legend: 'none',
- lineWidth: 1
- };
- var chart = new google.visualization.ScatterChart(document.getElementById('chart_div'));
- chart.draw(data, options);
- }
- toBuildGC();
- function mySort(a, b) {
- return b[2] - a[2];
- }
- function toBuildGC() {
- // ctx.clearRect(0, 0, 545, 540);
- // gc.splice(0, gc.length);
- // toDrawCanvas(ctx);
- // ctx.strokeStyle = "blue";
- //save
- for (let i = 0; i < mas.length; i++) {
- for (let j = v; j < mas.length; j++) {
- if (i == j) { j++ };
- pairarray.push(i + "," + j);
- }
- v++;
- }
- console.log(pairarray);
- // console.log("pairs");
- //console.log(pairarray);
- //console.log(" saving таблиця");
- for (let i = 0; i < 300; i++) {
- savearr[i] = [];
- savearr1[i] = [];
- savearrgc[i] = [];
- vuzol = pairarray[i].split(",");
- console.log(vuzol[0][1]);
- savearr[i][0] = parseFloat((basetoclient[vuzol[0]][1]).toFixed(2));
- savearr[i][1] = parseFloat((basetoclient[vuzol[1]][1]).toFixed(2));
- savearr[i][2] = parseFloat((D_sim[vuzol[0]][vuzol[1]]).toFixed(2));
- //для позначення номерів вузлів
- savearr1[i][0] = parseFloat(vuzol[0]) + 1;
- savearr1[i][1] = parseFloat(vuzol[1]) + 1;
- savearr1[i][2] = parseFloat((savearr[i][0] + savearr[i][1] - savearr[i][2]).toFixed(2));
- //console.log(savearr[i][0]+" + "+savearr[i][1]+" - "+ savearr[i][2] );
- //console.log("s "+savearr1[i][2] );//console.log(savearr1[0]);
- //для Гамільтонового циклу
- savearrgc[i][0] = parseFloat(vuzol[0]) + 1;
- savearrgc[i][1] = parseFloat(vuzol[1]) + 1;
- savearrgc[i][2] = parseFloat((savearr[i][0] + savearr[i][1] - savearr[i][2]).toFixed(2));
- }
- savearr = savearr.sort(mySort);
- savearr1 = savearr1.sort(mySort);
- savearrgc = savearrgc.sort(mySort);
- console.log(savearr);
- let last, index, toDelete, toDelete1, count = 0;
- gc.push(0);
- gc.push(savearr1[0][0]);
- gc.push(savearr1[0][1]);
- toDelete = gc[1];
- delDupl(toDelete);
- last = gc[2];
- for (let i = 0; i < savearrgc.length; i++) {
- index = savearrgc[i].indexOf(last);
- if (index >= 0) {
- count++;
- ///знаходження найкоротшого шляху
- if (count == 1) {
- let dis1, dis2, stock3;
- if (index == 0) {
- stock3 = savearrgc[i][index + 1] - 1;
- } else if (index == 1) {
- stock3 = savearrgc[i][index - 1] - 1;
- }
- //0-15-16-17
- dis1 = basetoclient[gc[1] - 1][1] + D_sim[gc[1] - 1][gc[2] - 1] + D_sim[gc[2] - 1][stock3];
- //0-16-15-17
- dis2 = basetoclient[gc[2] - 1][1] + D_sim[gc[2] - 1][gc[1] - 1] + D_sim[gc[1] - 1][stock3];
- //вибираэмо коротший шлях
- if (dis2 < dis1) {
- gc[1] = savearr1[0][1];
- gc[2] = savearr1[0][0];
- last = gc[2];
- // console.log("d1= " + dis1 + " d2= " + dis2);
- toDelete1 = gc[1];
- }
- }
- if (index == 0) {
- gc.push(savearrgc[i][index + 1]);
- } else if (index == 1) {
- gc.push(savearrgc[i][index - 1]);
- }
- last = gc[gc.length - 1];
- toDelete = gc[gc.length - 2];
- delDupl(toDelete);
- delDupl(toDelete1);
- i = 0;
- }
- }
- gc.push(0);
- console.log(" gc: " + gc);
- // for (let i = 0; i < gc.length; i++) {
- // if (i == 0) {
- // ctx.moveTo(x0 * scale + 25, 520 - y0 * scale);
- // } else {
- // for (let j = 0; j < mas.length; j++) {
- // if (mas[j][0] == gc[i]) {
- // ctx.lineTo(mas[j][1] * scale + 25, 520 - mas[j][2] * scale);
- // }
- // }
- // } if (i == (gc.length - 1)) {
- // ctx.lineTo(x0 * scale + 25, 520 - y0 * scale);
- // }
- // ctx.stroke();
- // }
- }
- function delDupl(delstring) {
- for (let i = 0; i < savearrgc.length; i++) {
- if (savearrgc[i].indexOf(delstring) >= 0) {
- savearrgc[i].splice(0, 3);
- }
- }
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement