-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
273 lines (233 loc) · 8.66 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
let nodes = [];
let nodeCount = 0;
let stepCount = 0; // untuk melacak stepnya
const distanceTableBody = document.querySelector('#distanceTable tbody');
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const flowText = document.getElementById('flowText');
// untuk menambahkan simpul baru
function addNode(x, y) {
nodeCount++;
const node = {
id: nodeCount,
x: x,
y: y
};
nodes.push(node);
updateDistanceTable();
draw();
logFlow(`Added node ${node.id} at coordinates (${node.x.toFixed(2)}, ${node.y.toFixed(2)})`);
logFlow(`Recomputing sub-tours after adding node ${node.id}`);
logSubTours();
}
// untuk memperbarui tabel jarak
function updateDistanceTable() {
distanceTableBody.innerHTML = '';
nodes.forEach((node1, index1) => {
nodes.forEach((node2, index2) => {
if (index1 !== index2) {
const distance = calculateDistance(node1, node2);
const row = document.createElement('tr');
row.innerHTML = `
<td>${node1.id}</td>
<td>${node2.id}</td>
<td>${distance.toFixed(2)}</td>
`;
distanceTableBody.appendChild(row);
}
});
});
}
// Fungsi untuk mengurutkan tour berdasarkan jalur yang berkelanjutan
function orderTour(tour) {
if (tour.length === 0) return [];
const orderedTour = [tour[0]];
let currentTo = tour[0].to;
while (orderedTour.length < tour.length) {
const nextPath = tour.find(path => path.from === currentTo);
if (nextPath) {
orderedTour.push(nextPath);
currentTo = nextPath.to;
} else {
break;
}
}
return orderedTour;
}
// Fungsi untuk menghitung jarak antara dua simpul
function calculateDistance(node1, node2) {
const dx = node2.x - node1.x;
const dy = node2.y - node1.y;
return Math.sqrt(dx * dx + dy * dy);
}
// Fungsi untuk menggambar garis antara simpul-simpul pada kanvas
function drawLines() {
ctx.strokeStyle = '#8174A0'; // Garis default berwarna abu-abu keunguan
ctx.lineWidth = 2;
nodes.forEach((node1, index1) => {
nodes.forEach((node2, index2) => {
if (index1 !== index2) {
ctx.beginPath();
ctx.moveTo(node1.x, node1.y);
ctx.lineTo(node2.x, node2.y);
ctx.stroke();
ctx.closePath();
}
});
});
drawShortestPath();
}
// Fungsi untuk menggambar rute terpendek dengan warna hijau
function drawShortestPath() {
const { tour } = calculateShortestPath();
ctx.strokeStyle = 'lime'; // Garis rute terpendek
ctx.lineWidth = 3;
tour.forEach((path) => {
const node1 = nodes[path.from - 1];
const node2 = nodes[path.to - 1];
ctx.beginPath();
ctx.moveTo(node1.x, node1.y);
ctx.lineTo(node2.x, node2.y);
ctx.stroke();
ctx.closePath();
});
}
// Fungsi untuk menghitung jarak antara dua node berdasarkan ID
function getDistance(id1, id2) {
const node1 = nodes.find(node => node.id === id1);
const node2 = nodes.find(node => node.id === id2);
return calculateDistance(node1, node2);
}
// Fungsi untuk menghitung biaya penambahan node baru ke dalam tour
function calculateInsertionCost(tour, newNodeId) {
let minCost = Infinity;
let bestInsertion = null;
const allPossibleInsertions = [];
for (let i = 0; i < tour.length; i++) {
const current = tour[i];
const distanceCurrent = getDistance(current.from, current.to);
const distanceNewFrom = getDistance(current.from, newNodeId);
const distanceNewTo = getDistance(newNodeId, current.to);
const insertionCost = distanceNewFrom + distanceNewTo - distanceCurrent;
allPossibleInsertions.push({ from: current.from, to: current.to, newNodeId, insertionCost });
if (insertionCost < minCost) {
minCost = insertionCost;
bestInsertion = { from: current.from, to: current.to, newNodeId };
}
}
return { bestInsertion, allPossibleInsertions };
}
// Fungsi untuk menghitung rute terpendek menggunakan algoritma CIH
function calculateShortestPath() {
if (nodes.length < 2) return { tour: [], subTours: [] };
// Inisialisasi dengan dua node pertama
let tour = [
{ from: nodes[0].id, to: nodes[1].id },
{ from: nodes[1].id, to: nodes[0].id }
];
const subTours = [[...tour]];
// Tambahkan node lain ke dalam tour menggunakan CIH
for (let i = 2; i < nodes.length; i++) {
const newNodeId = nodes[i].id;
const { bestInsertion, allPossibleInsertions } = calculateInsertionCost(tour, newNodeId);
// Log semua kemungkinan sub-tours
allPossibleInsertions.forEach((insertion, index) => {
const tempTour = tour.filter(edge => edge.from !== insertion.from || edge.to !== insertion.to);
tempTour.push({ from: insertion.from, to: insertion.newNodeId });
tempTour.push({ from: insertion.newNodeId, to: insertion.to });
subTours.push([...tempTour]);
});
if (bestInsertion) {
// Sisipkan node baru ke dalam tour
tour = tour.filter(edge => edge.from !== bestInsertion.from || edge.to !== bestInsertion.to);
tour.push({ from: bestInsertion.from, to: bestInsertion.newNodeId });
tour.push({ from: bestInsertion.newNodeId, to: bestInsertion.to });
// Simpan sub-tour saat ini
subTours.push([...tour]);
}
}
return { tour, subTours };
}
// Fungsi untuk menghitung total jarak sebuah sub-tour
function calculateTourDistance(tour) {
return tour.reduce((total, path) => total + getDistance(path.from, path.to), 0);
}
// Fungsi untuk mencatat langkah-langkah flow
function logFlow(message, isFinal = false) {
const formattedMessage = `Step ${stepCount}: ${message}`;
const logEntry = document.createElement('div');
logEntry.textContent = formattedMessage;
// Tambahkan warna khusus jika ini adalah final chosen sub-tour
if (isFinal) {
logEntry.style.color = '#0fff03'; // warna chosen sub-tour
}
flowText.appendChild(logEntry);
flowText.scrollTop = flowText.scrollHeight;
stepCount++;
}
// Fungsi untuk mencatat sub-tours dengan format lebih mudah dibaca
function logSubTours() {
const { subTours } = calculateShortestPath();
const chosenSubTour = subTours[subTours.length - 1]; // Sub-tour terakhir adalah yang terpilih
logFlow('Evaluating sub-tours:');
subTours.forEach((subTour, index) => {
const isChosen = subTour === chosenSubTour;
const subTourDetails = subTour.map(path => `(${path.from} -> ${path.to})`).join(', ');
const totalDistance = calculateTourDistance(subTour).toFixed(2);
logFlow(`Sub-tour ${index + 1}: ${subTourDetails}, Total Distance: ${totalDistance}${isChosen ? ' is chosen' : ''}`, isChosen);
});
if (chosenSubTour) {
const orderedTour = orderTour(chosenSubTour);
const orderedDetails = orderedTour.map(path => `(${path.from} -> ${path.to})`).join(', ');
logFlow(`Final chosen: ${orderedDetails}`, true); // Teks berwarna untuk final
}
}
// Fungsi untuk menggambar simpul pada kanvas
function drawNodes() {
nodes.forEach(node => {
// Menggambar simpul
ctx.beginPath();
ctx.arc(node.x, node.y, 14, 0, Math.PI * 2);
ctx.fillStyle = '#C30E59';
ctx.fill();
// stroke simpul
ctx.strokeStyle = '#DA498D';
ctx.lineWidth = 1.5;
ctx.stroke();
ctx.closePath();
// label pada simpul
ctx.fillStyle = 'white';
ctx.font = '17px Times New Roman';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(node.id, node.x, node.y);
});
}
// Fungsi untuk menggambar semua elemen
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawLines();
drawNodes();
}
// Tambahkan event listener untuk klik pada kanvas
canvas.addEventListener('click', (event) => {
const rect = canvas.getBoundingClientRect();
const x = event.clientX - rect.left;
const y = event.clientY - rect.top;
addNode(x, y);
});
function resetApp() {
// Hapus semua node
nodes = [];
nodeCount = 0;
stepCount = 0;
// Kosongkan tabel jarak
distanceTableBody.innerHTML = '';
// Kosongkan log flow
flowText.value = '';
flowText.innerText = '';
// Bersihkan canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Log pesan bahwa aplikasi telah direset
// logFlow("Application has been reset.");
}