Skip to content

Commit 526acd4

Browse files
committed
Move gps heuristic function to tests.js
Also clearer segmentation between heuristic function and cost function.
1 parent 959e0c4 commit 526acd4

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

astar.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,6 @@ var astar = {
150150
var d1 = Math.abs (pos1.x - pos0.x);
151151
var d2 = Math.abs (pos1.y - pos0.y);
152152
return (D * (d1 + d2)) + ((D2 - (2 * D)) * Math.min(d1, d2));
153-
},
154-
gps: function(node0, node1) {
155-
var x = (node1.longRad - node0.longRad) * Math.cos((node0.latRad + node1.latRad)/2),
156-
y = node1.latRad - node0.latRad,
157-
res = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)) * 6371;
158-
return res;
159153
}
160154
}
161155
};

test/tests.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,19 @@ test( "GPS Pathfinding", function() {
132132
CityNode.prototype.isWall = function() {
133133
return this.weight === 0;
134134
};
135+
// Heuristic function
136+
CityNode.prototype.GPS_distance = function(city) {
137+
var x = (city.longRad - this.longRad) * Math.cos((this.latRad + city.latRad)/2),
138+
y = city.latRad - this.latRad,
139+
res = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)) * 6371;
140+
return res;
141+
};
142+
// Real cost function
143+
CityNode.prototype.Real_distance = function(city) {
144+
// Re-use heuristic function for now
145+
// TODO: Determine the real distance between cities (from another data set)
146+
return this.GPS_distance(city);
147+
};
135148

136149
//---
137150

@@ -147,23 +160,26 @@ test( "GPS Pathfinding", function() {
147160
graph.cities = cities;
148161
graph.links = links;
149162

150-
var GPSheuristic = astar.heuristics.gps;
151-
152-
graph.neighbors = function (node) { // Override neighbors function for this specific graph
163+
// Override neighbors function for this specific graph
164+
graph.neighbors = function (node) {
153165
var neighbors = [],
154166
ids = this.links[node.name];
155167
for (var i = 0, len = ids.length; i < len; ++i) {
156168
var name = ids[i],
157169
neighbor = this.cities[name];
158-
neighbor.cost = GPSheuristic(node, neighbor); // Compute real cost!
170+
neighbor.cost = node.Real_distance(neighbor); // Compute real cost!
159171
neighbors.push(neighbor);
160172
}
161173
return neighbors;
162174
};
163175

164176
var start = cities["Paris"],
165177
end = cities["Cannes"];
166-
178+
179+
var GPSheuristic = function(node0, node1) {
180+
return node0.GPS_distance(node1);
181+
};
182+
167183
var result = astar.search(graph, start, end, {heuristic: GPSheuristic});
168184
equal(result.length, 3, "Cannes is 3 cities away from Paris");
169185
equal(result[0].name, "Lyon", "City #1 is Lyon");

0 commit comments

Comments
 (0)