-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnearestNeighbourTsp.js
171 lines (135 loc) · 5.02 KB
/
nearestNeighbourTsp.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
var halfdane = halfdane || {};
halfdane.tsp = halfdane.tsp || {};
halfdane.tsp.enhancePoints = function (points) {
var distances = {};
function pointToCity(point) {
point.distanceTo = function (otherPoint) {
if (distances[point.toString() + "_" + otherPoint.toString()]) {
return distances[point.toString() + "_" + otherPoint.toString()];
}
if (distances[otherPoint.toString() + "_" + point.toString()]) {
return distances[otherPoint.toString() + "_" + point.toString()];
}
var xDistance = Math.abs(point.x - otherPoint.x),
yDistance = Math.abs(point.y - otherPoint.y),
distance = (xDistance * xDistance) + (yDistance * yDistance);
distances[this.toString() + "_" + otherPoint.toString()] = distance;
return distance;
};
point.toString = function () {
if (!point.name) {
point.name = "(" + point.x + "/" + point.y + ")";
}
return point.name;
};
return point;
}
return $(points).map(function (index, point) {
return pointToCity(point);
});
};
// generate single path tween points:
// * create delaunay triangulation
// * solve tsp on triangulation the greedy way.
halfdane.tsp.nearestNeighbour = function (points, drawEdgesFunction) {
var triangles = triangulate(halfdane.tsp.enhancePoints(points)),
tourPoints = [],
pointmap = {},
currentPoint,
maxTripSentry = 2000000000,
tenPercent = Math.round(points.length / 10),
count;
function createPointMap(triangles) {
var map = {};
map.allPoints = function () {
return Object.getOwnPropertyNames(map);
};
map.endpointsFromPoint = function (point) {
return map.endpointsFromString(point.toString());
};
map.endpointsFromString = function (pointName) {
return map[pointName];
};
map.hasEndpoints = function (point) {
return pointmap.endpointsFromPoint(point) !== undefined;
};
map.remove = function (point) {
delete map[point.toString()];
};
function addPointAsProperty(point, triangle, map) {
if (!map[point.toString()]) {
map[point.toString()] = [];
}
if ((triangle.a.x !== point.x || triangle.a.y !== point.y)) {
map[point.toString()].push(triangle.a);
}
if ((triangle.b.x !== point.x || triangle.b.y !== point.y)) {
map[point.toString()].push(triangle.b);
}
if ((triangle.c.x !== point.x || triangle.c.y !== point.y)) {
map[point.toString()].push(triangle.c);
}
}
$(triangles).each(function (index, triangle) {
addPointAsProperty(triangle.a, triangle, map);
addPointAsProperty(triangle.b, triangle, map);
addPointAsProperty(triangle.c, triangle, map);
});
return map;
}
pointmap = createPointMap(triangles);
// start with any point
currentPoint = triangles[0].a;
tourPoints.push(currentPoint);
while (true) {
var currentEndpoints = pointmap.endpointsFromPoint(currentPoint),
nearest = maxTripSentry,
p2 = undefined;
count += 1;
// remove current point from point map
pointmap.remove(currentPoint);
// choose nearest neighbour from current point
$(currentEndpoints).each(function (index, point) {
var d = currentPoint.distanceTo(point);
if (pointmap.hasEndpoints(point) && d < nearest) {
p2 = point;
nearest = d;
}
});
if (!p2) {
// choose any closest point as endpoint
$(pointmap.allPoints()).each(function (index, pointname) {
$(pointmap.endpointsFromString(pointname)).each(function (index, point) {
var d = currentPoint.distanceTo(point);
if (pointmap.hasEndpoints(point) && d < nearest) {
p2 = point;
nearest = d;
}
});
});
}
if (!p2) {
drawEdgesFunction(tourPoints, tourPoints.length + " Punkte", triangles);
return tourPoints;
}
// add edge to edges
tourPoints.push(p2);
// use edge's endpoint as current point
currentPoint = p2;
if (tourPoints.length % (tenPercent) === 0 ){
drawEdgesFunction(tourPoints, tourPoints.length + " Punkte", triangles);
}
}
};
halfdane.tsp.greedyTest = function (pointsCount) {
'use strict';
var points = [],
i;
for (i = 0; i <= pointsCount; i += 1) {
points.push({
x: Math.random() * 200,
y: Math.random() * 200
});
}
halfdane.tsp.nearestNeighbour(points, halfdane.drawEdges);
};