-
Notifications
You must be signed in to change notification settings - Fork 317
Expand file tree
/
Copy pathtests.js
More file actions
263 lines (210 loc) · 7.36 KB
/
tests.js
File metadata and controls
263 lines (210 loc) · 7.36 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
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
/* global Graph, astar, ok, test, equal */
test( "Sanity Checks", function() {
ok (typeof Graph !== "undefined", "Graph exists");
ok (typeof astar !== "undefined", "Astar exists");
});
test( "Basic Horizontal", function() {
var result1 = runSearch([[1],[1]], [0,0], [1,0]);
equal (result1.text, "(1,0)", "One step down");
var result2 = runSearch([[1],[1],[1]], [0,0], [2,0]);
equal (result2.text, "(1,0)(2,0)", "Two steps down");
var result3 = runSearch([[1],[1],[1],[1]], [0,0], [3,0]);
equal (result3.text, "(1,0)(2,0)(3,0)", "Three steps down");
});
test( "Basic Vertical", function() {
var result1 = runSearch([[1, 1]], [0,0], [0,1]);
equal (result1.text, "(0,1)", "One step across");
var result2 = runSearch([[1, 1, 1]], [0,0], [0,2]);
equal (result2.text, "(0,1)(0,2)", "Two steps across");
var result3 = runSearch([[1, 1, 1, 1]], [0,0], [0,3]);
equal (result3.text, "(0,1)(0,2)(0,3)", "Three steps across");
});
test( "Basic Weighting", function() {
var result1 = runSearch([[1, 1],
[2, 1]], [0,0], [1,1]);
equal (result1.text, "(0,1)(1,1)", "Takes less weighted path");
var result2 = runSearch([[1, 2],
[1, 1]], [0,0], [1,1]);
equal (result2.text, "(1,0)(1,1)", "Takes less weighted path");
});
test( "Pathfinding", function() {
var result1 = runSearch([
[1,1,1,1],
[0,1,1,0],
[0,0,1,1]
], [0,0], [2,3]);
equal (result1.text, "(0,1)(1,1)(1,2)(2,2)(2,3)", "Result is expected");
});
test( "Diagonal Pathfinding", function() {
var result1 = runSearch(new Graph([
[1,1,1,1],
[0,1,1,0],
[0,0,1,1]
], { diagonal: true}), [0,0], [2,3]);
equal (result1.text, "(1,1)(2,2)(2,3)", "Result is expected");
});
test( "Pathfinding to closest", function() {
var result1 = runSearch([
[1,1,1,1],
[0,1,1,0],
[0,0,1,1]
], [0,0], [2,1], {closest: true});
equal (result1.text, "(0,1)(1,1)", "Result is expected - pathed to closest node");
var result2 = runSearch([
[1,0,1,1],
[0,1,1,0],
[0,0,1,1]
], [0,0], [2,1], {closest: true});
equal (result2.text, "", "Result is expected - start node was closest node");
var result3 = runSearch([
[1,1,1,1],
[0,1,1,0],
[0,1,1,1]
], [0,0], [2,1], {closest: true});
equal (result3.text, "(0,1)(1,1)(2,1)", "Result is expected - target node was reachable");
});
test( "Path costs", function() {
var graph1 = new Graph([
[1,2,2],
[1,1,2],
[1,1,2],
], { diagonal: true});
var straightNeighbor = graph1.grid[1][0].getCost(graph1.grid[0][0]),
diagonalNeighbor = graph1.grid[1][1].getCost(graph1.grid[0][0]),
diagonalWeightedNeighbor = graph1.grid[2][2].getCost(graph1.grid[1][1]);
equal ((straightNeighbor == 1) , true, "Result is expected - neighbor cost is 1");
equal ((diagonalNeighbor > 1 && diagonalNeighbor < 2) , true, "Result is expected - diagonal neighbor cost is more than 1, less than 2");
equal ((diagonalWeightedNeighbor > 2 && diagonalWeightedNeighbor < 4), true, "Result is expected - diagonal neighbor cost for 2 weight node is more than 2, less than 4");
});
function runSearch(graph, start, end, options) {
if (!(graph instanceof Graph)) {
graph = new Graph(graph);
}
start = graph.grid[start[0]][start[1]];
end = graph.grid[end[0]][end[1]];
var sTime = new Date(),
result = astar.search(graph, start, end, options),
eTime = new Date();
return {
result: result,
text: pathToString(result),
time: (eTime - sTime)
};
}
function pathToString(result) {
return result.map(function(node) {
return "(" + node.x + "," + node.y + ")";
}).join("");
}
test( "GPS Pathfinding", function() {
var data = [
{name: "Paris", lat: 48.8567, lng: 2.3508},
{name: "Lyon", lat: 45.76, lng: 4.84},
{name: "Marseille", lat: 43.2964, lng: 5.37},
{name: "Bordeaux", lat: 44.84, lng: -0.58},
{name: "Cannes", lat: 43.5513, lng: 7.0128},
{name: "Toulouse", lat: 43.6045, lng: 1.444},
{name: "Reims", lat: 49.2628, lng: 4.0347}
],
links = {
"Paris": ["Lyon", "Bordeaux", "Reims"],
"Lyon": ["Paris", "Marseille"],
"Marseille": ["Lyon", "Cannes", "Toulouse"],
"Bordeaux": ["Toulouse", "Paris"],
"Cannes": ["Marseille"],
"Toulouse": ["Marseille", "Bordeaux"],
"Reims": ["Paris"]
};
function CityGraph(data, links) {
this.nodes = [];
this.links = links;
this.cities = {};
for (var i = 0; i < data.length; ++i) {
var city = data[i],
obj = new CityNode(city.name, city.lat, city.lng);
if (this.nodes.indexOf(obj) == -1) {
this.nodes.push(obj);
}
this.cities[obj.name] = obj;
}
this.init();
}
CityGraph.prototype.init= Graph.prototype.init;
CityGraph.prototype.cleanDirty = Graph.prototype.cleanDirty;
CityGraph.prototype.markDirty = Graph.prototype.markDirty;
CityGraph.prototype.neighbors = function (node) {
var neighbors = [],
ids = this.links[node.name];
for (var i = 0, len = ids.length; i < len; ++i) {
var name = ids[i],
neighbor = this.cities[name];
neighbors.push(neighbor);
}
return neighbors;
};
function CityNode(name, lat, lng) {
this.name = name;
this.lat = lat;
this.lng = lng;
this.longRad = this.lng * Math.PI / 180;
this.latRad = this.lat * Math.PI / 180;
}
CityNode.prototype.weight = 1;
CityNode.prototype.toString = function() {
return "[" + this.name + " (" + this.lat + ", " + this.lng + ")]";
};
CityNode.prototype.isWall = function() {
return this.weight === 0;
};
// Heuristic function
CityNode.prototype.GPS_distance = function(city) {
var x = (city.longRad - this.longRad) * Math.cos((this.latRad + city.latRad)/2),
y = city.latRad - this.latRad,
res = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)) * 6371;
return res;
};
// Real cost function
CityNode.prototype.getCost = function(city) {
// Re-use heuristic function for now
// TODO: Determine the real distance between cities (from another data set)
return this.GPS_distance(city);
};
CityNode.prototype.clearanceLower = function(clearance) {
return this.clearance < clearance;
};
var graph = new CityGraph(data, links);
var start = graph.cities["Paris"],
end = graph.cities["Cannes"];
var GPSheuristic = function(node0, node1) {
return node0.GPS_distance(node1);
};
var result = astar.search(graph, start, end, {heuristic: GPSheuristic});
equal(result.length, 3, "Cannes is 3 cities away from Paris");
equal(result[0].name, "Lyon", "City #1 is Lyon");
equal(result[1].name, "Marseille", "City #2 is Marseille");
equal(result[2].name, "Cannes", "City #3 is Cannes");
});
// // https://gist.github.com/bgrins/581352
// function runBasic() {
// var graph = new Graph([
// [1,1,1,1],
// [0,1,1,0],
// [0,0,1,1]
// ]);
// var start = graph.grid[0][0];
// var end = graph.grid[1][2];
// var result = astar.search(graph, start, end);
// return "<pre>" + result.join(", ") + "</pre>";
// }
// $(function() {
// $("#runall").click(function() {
// var result1 = runTest([
// [1,1,1,1],
// [0,1,1,0],
// [0,0,1,1]
// ], [0,0], [2,3]);
// $("#test-output").append(result1.text);
// $("#test-output").append(runBasic());
// return false;
// });
// });