-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathindex.js
267 lines (220 loc) · 8.9 KB
/
index.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
module.exports = nba;
var NodeHeap = require('../NodeHeap');
var heuristics = require('../heuristics');
var defaultSettings = require('../defaultSettings.js');
var makeNBASearchStatePool = require('./makeNBASearchStatePool.js');
var NO_PATH = defaultSettings.NO_PATH;
module.exports.l2 = heuristics.l2;
module.exports.l1 = heuristics.l1;
/**
* Creates a new instance of pathfinder. A pathfinder has just one method:
* `find(fromId, toId)`.
*
* This is implementation of the NBA* algorithm described in
*
* "Yet another bidirectional algorithm for shortest paths" paper by Wim Pijls and Henk Post
*
* The paper is available here: https://repub.eur.nl/pub/16100/ei2009-10.pdf
*
* @param {ngraph.graph} graph instance. See /~https://github.com/anvaka/ngraph.graph
* @param {Object} options that configures search
* @param {Function(a, b, link)} options.blocked - a function that returns `true` if the link between
* nodes `a` and `b` are blocked paths. This function is useful for temporarily blocking routes
* while allowing the graph to be reused without rebuilding.
* @param {Function(a, b)} options.heuristic - a function that returns estimated distance between
* nodes `a` and `b`. This function should never overestimate actual distance between two
* nodes (otherwise the found path will not be the shortest). Defaults function returns 0,
* which makes this search equivalent to Dijkstra search.
* @param {Function(a, b)} options.distance - a function that returns actual distance between two
* nodes `a` and `b`. By default this is set to return graph-theoretical distance (always 1);
*
* @returns {Object} A pathfinder with single method `find()`.
*/
function nba(graph, options) {
options = options || {};
// whether traversal should be considered over oriented graph.
var oriented = options.oriented;
var quitFast = options.quitFast;
var blocked = options.blocked;
if (!blocked) blocked = defaultSettings.blocked;
var heuristic = options.heuristic;
if (!heuristic) heuristic = defaultSettings.heuristic;
var distance = options.distance;
if (!distance) distance = defaultSettings.distance;
// During stress tests I noticed that garbage collection was one of the heaviest
// contributors to the algorithm's speed. So I'm using an object pool to recycle nodes.
var pool = makeNBASearchStatePool();
return {
/**
* Finds a path between node `fromId` and `toId`.
* @returns {Array} of nodes between `toId` and `fromId`. Empty array is returned
* if no path is found.
*/
find: find
};
function find(fromId, toId) {
// I must apologize for the code duplication. This was the easiest way for me to
// implement the algorithm fast.
var from = graph.getNode(fromId);
if (!from) throw new Error('fromId is not defined in this graph: ' + fromId);
var to = graph.getNode(toId);
if (!to) throw new Error('toId is not defined in this graph: ' + toId);
pool.reset();
// I must also apologize for somewhat cryptic names. The NBA* is bi-directional
// search algorithm, which means it runs two searches in parallel. One is called
// forward search and it runs from source node to target, while the other one
// (backward search) runs from target to source.
// Everywhere where you see `1` it means it's for the forward search. `2` is for
// backward search.
// For oriented graph path finding, we need to reverse the graph, so that
// backward search visits correct link. Obviously we don't want to duplicate
// the graph, instead we always traverse the graph as non-oriented, and filter
// edges in `visitN1Oriented/visitN2Oritented`
var forwardVisitor = oriented ? visitN1Oriented : visitN1;
var reverseVisitor = oriented ? visitN2Oriented : visitN2;
// Maps nodeId to NBASearchState.
var nodeState = new Map();
// These two heaps store nodes by their underestimated values.
var open1Set = new NodeHeap({
compare: defaultSettings.compareF1Score,
setNodeId: defaultSettings.setH1
});
var open2Set = new NodeHeap({
compare: defaultSettings.compareF2Score,
setNodeId: defaultSettings.setH2
});
// This is where both searches will meet.
var minNode;
// The smallest path length seen so far is stored here:
var lMin = Number.POSITIVE_INFINITY;
// We start by putting start/end nodes to the corresponding heaps
// If variable names like `f1`, `g1` are too confusing, please refer
// to makeNBASearchStatePool.js file, which has detailed description.
var startNode = pool.createNewState(from);
nodeState.set(fromId, startNode);
startNode.g1 = 0;
var f1 = heuristic(from, to);
startNode.f1 = f1;
open1Set.push(startNode);
var endNode = pool.createNewState(to);
nodeState.set(toId, endNode);
endNode.g2 = 0;
var f2 = f1; // they should agree originally
endNode.f2 = f2;
open2Set.push(endNode)
// the `cameFrom` variable is accessed by both searches, so that we can store parents.
var cameFrom;
// this is the main algorithm loop:
while (open2Set.length && open1Set.length) {
if (open1Set.length < open2Set.length) {
forwardSearch();
} else {
reverseSearch();
}
if (quitFast && minNode) break;
}
var path = reconstructPath(minNode);
return path; // the public API is over
function forwardSearch() {
cameFrom = open1Set.pop();
if (cameFrom.closed) {
return;
}
cameFrom.closed = true;
if (cameFrom.f1 < lMin && (cameFrom.g1 + f2 - heuristic(from, cameFrom.node)) < lMin) {
graph.forEachLinkedNode(cameFrom.node.id, forwardVisitor);
}
if (open1Set.length > 0) {
// this will be used in reverse search
f1 = open1Set.peek().f1;
}
}
function reverseSearch() {
cameFrom = open2Set.pop();
if (cameFrom.closed) {
return;
}
cameFrom.closed = true;
if (cameFrom.f2 < lMin && (cameFrom.g2 + f1 - heuristic(cameFrom.node, to)) < lMin) {
graph.forEachLinkedNode(cameFrom.node.id, reverseVisitor);
}
if (open2Set.length > 0) {
// this will be used in forward search
f2 = open2Set.peek().f2;
}
}
function visitN1(otherNode, link) {
var otherSearchState = nodeState.get(otherNode.id);
if (!otherSearchState) {
otherSearchState = pool.createNewState(otherNode);
nodeState.set(otherNode.id, otherSearchState);
}
if (otherSearchState.closed) return;
if (blocked(cameFrom.node, otherNode, link)) return;
var tentativeDistance = cameFrom.g1 + distance(cameFrom.node, otherNode, link);
if (tentativeDistance < otherSearchState.g1) {
otherSearchState.g1 = tentativeDistance;
otherSearchState.f1 = tentativeDistance + heuristic(otherSearchState.node, to);
otherSearchState.p1 = cameFrom;
if (otherSearchState.h1 < 0) {
open1Set.push(otherSearchState);
} else {
open1Set.updateItem(otherSearchState.h1);
}
}
var potentialMin = otherSearchState.g1 + otherSearchState.g2;
if (potentialMin < lMin) {
lMin = potentialMin;
minNode = otherSearchState;
}
}
function visitN2(otherNode, link) {
var otherSearchState = nodeState.get(otherNode.id);
if (!otherSearchState) {
otherSearchState = pool.createNewState(otherNode);
nodeState.set(otherNode.id, otherSearchState);
}
if (otherSearchState.closed) return;
if (blocked(cameFrom.node, otherNode, link)) return;
var tentativeDistance = cameFrom.g2 + distance(cameFrom.node, otherNode, link);
if (tentativeDistance < otherSearchState.g2) {
otherSearchState.g2 = tentativeDistance;
otherSearchState.f2 = tentativeDistance + heuristic(from, otherSearchState.node);
otherSearchState.p2 = cameFrom;
if (otherSearchState.h2 < 0) {
open2Set.push(otherSearchState);
} else {
open2Set.updateItem(otherSearchState.h2);
}
}
var potentialMin = otherSearchState.g1 + otherSearchState.g2;
if (potentialMin < lMin) {
lMin = potentialMin;
minNode = otherSearchState;
}
}
function visitN2Oriented(otherNode, link) {
// we are going backwards, graph needs to be reversed.
if (link.toId === cameFrom.node.id) return visitN2(otherNode, link);
}
function visitN1Oriented(otherNode, link) {
// this is forward direction, so we should be coming FROM:
if (link.fromId === cameFrom.node.id) return visitN1(otherNode, link);
}
}
}
function reconstructPath(searchState) {
if (!searchState) return NO_PATH;
var path = [searchState.node];
var parent = searchState.p1;
while (parent) {
path.push(parent.node);
parent = parent.p1;
}
var child = searchState.p2;
while (child) {
path.unshift(child.node);
child = child.p2;
}
return path;
}