-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBF.js
More file actions
42 lines (39 loc) · 1.34 KB
/
Copy pathBF.js
File metadata and controls
42 lines (39 loc) · 1.34 KB
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
function initializeBF() {
BFBestEver = BFOrder.slice();
BFBestRecordDist = Infinity;
BFStatsCurr = [];
BFStatsBest = [];
}
function startBF() {
var i = floor(random(1, BFOrder.length - 1));
var j = floor(random(1, BFOrder.length - 1));
BFSwap(BFOrder, i, j);
var BFCurrDist = BFCalcDist(BFCities, BFOrder);
if (BFCurrDist < BFBestRecordDist) {
BFBestEver = BFOrder.slice();
BFBestRecordDist = BFCurrDist;
document.getElementById('BFBestRoute').textContent = BFBestEver.map(item => { return item + 1 });
document.getElementById('BFBestDistance').textContent = BFBestRecordDist.toFixed(4);
}
document.getElementById('BFCurrRoute').textContent = BFOrder.map(item => { return item + 1 });
document.getElementById('BFCurrDistance').textContent = BFCurrDist.toFixed(4);
BFStatsCurr.push(BFCurrDist);
BFStatsBest.push(BFBestRecordDist);
}
function BFSwap(arr, i, j) {
var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
function BFCalcDist(points, order) {
var sum = 0;
for (let i = 0; i < order.length - 1; i++) {
var cityAIndex = order[i];
var cityA = points[cityAIndex];
var cityBIndex = order[i + 1];
var cityB = points[cityBIndex];
var d = dist(cityA.x, cityA.y, cityB.x, cityB.y);
sum += d;
}
return sum;
}