1
1
module . exports = nba ;
2
2
3
- var NodeHeap = require ( " ../NodeHeap" ) ;
4
- var heuristics = require ( " ../heuristics" ) ;
5
- var defaultSettings = require ( " ../defaultSettings.js" ) ;
6
- var makeNBASearchStatePool = require ( " ./makeNBASearchStatePool.js" ) ;
3
+ var NodeHeap = require ( ' ../NodeHeap' ) ;
4
+ var heuristics = require ( ' ../heuristics' ) ;
5
+ var defaultSettings = require ( ' ../defaultSettings.js' ) ;
6
+ var makeNBASearchStatePool = require ( ' ./makeNBASearchStatePool.js' ) ;
7
7
8
8
var NO_PATH = defaultSettings . NO_PATH ;
9
9
@@ -13,13 +13,13 @@ module.exports.l1 = heuristics.l1;
13
13
/**
14
14
* Creates a new instance of pathfinder. A pathfinder has just one method:
15
15
* `find(fromId, toId)`.
16
- *
17
- * This is implementation of the NBA* algorithm described in
18
- *
16
+ *
17
+ * This is implementation of the NBA* algorithm described in
18
+ *
19
19
* "Yet another bidirectional algorithm for shortest paths" paper by Wim Pijls and Henk Post
20
- *
20
+ *
21
21
* The paper is available here: https://repub.eur.nl/pub/16100/ei2009-10.pdf
22
- *
22
+ *
23
23
* @param {ngraph.graph } graph instance. See https://github.com/anvaka/ngraph.graph
24
24
* @param {Object } options that configures search
25
25
* @param {Function(a, b, link) } options.blocked - a function that returns `true` if the link between
@@ -31,7 +31,7 @@ module.exports.l1 = heuristics.l1;
31
31
* which makes this search equivalent to Dijkstra search.
32
32
* @param {Function(a, b) } options.distance - a function that returns actual distance between two
33
33
* nodes `a` and `b`. By default this is set to return graph-theoretical distance (always 1);
34
- *
34
+ *
35
35
* @returns {Object } A pathfinder with single method `find()`.
36
36
*/
37
37
function nba ( graph , options ) {
@@ -59,17 +59,16 @@ function nba(graph, options) {
59
59
* @returns {Array } of nodes between `toId` and `fromId`. Empty array is returned
60
60
* if no path is found.
61
61
*/
62
- find : find ,
62
+ find : find
63
63
} ;
64
64
65
65
function find ( fromId , toId ) {
66
66
// I must apologize for the code duplication. This was the easiest way for me to
67
67
// implement the algorithm fast.
68
68
var from = graph . getNode ( fromId ) ;
69
- if ( ! from )
70
- throw new Error ( "fromId is not defined in this graph: " + fromId ) ;
69
+ if ( ! from ) throw new Error ( 'fromId is not defined in this graph: ' + fromId ) ;
71
70
var to = graph . getNode ( toId ) ;
72
- if ( ! to ) throw new Error ( " toId is not defined in this graph: " + toId ) ;
71
+ if ( ! to ) throw new Error ( ' toId is not defined in this graph: ' + toId ) ;
73
72
74
73
pool . reset ( ) ;
75
74
@@ -78,7 +77,7 @@ function nba(graph, options) {
78
77
// forward search and it runs from source node to target, while the other one
79
78
// (backward search) runs from target to source.
80
79
81
- // Everywhere where you see `1` it means it's for the forward search. `2` is for
80
+ // Everywhere where you see `1` it means it's for the forward search. `2` is for
82
81
// backward search.
83
82
84
83
// For oriented graph path finding, we need to reverse the graph, so that
@@ -94,11 +93,11 @@ function nba(graph, options) {
94
93
// These two heaps store nodes by their underestimated values.
95
94
var open1Set = new NodeHeap ( {
96
95
compare : defaultSettings . compareF1Score ,
97
- setNodeId : defaultSettings . setH1 ,
96
+ setNodeId : defaultSettings . setH1
98
97
} ) ;
99
98
var open2Set = new NodeHeap ( {
100
99
compare : defaultSettings . compareF2Score ,
101
- setNodeId : defaultSettings . setH2 ,
100
+ setNodeId : defaultSettings . setH2
102
101
} ) ;
103
102
104
103
// This is where both searches will meet.
@@ -111,7 +110,7 @@ function nba(graph, options) {
111
110
// If variable names like `f1`, `g1` are too confusing, please refer
112
111
// to makeNBASearchStatePool.js file, which has detailed description.
113
112
var startNode = pool . createNewState ( from ) ;
114
- nodeState . set ( fromId , startNode ) ;
113
+ nodeState . set ( fromId , startNode ) ;
115
114
startNode . g1 = 0 ;
116
115
var f1 = heuristic ( from , to ) ;
117
116
startNode . f1 = f1 ;
@@ -122,7 +121,7 @@ function nba(graph, options) {
122
121
endNode . g2 = 0 ;
123
122
var f2 = f1 ; // they should agree originally
124
123
endNode . f2 = f2 ;
125
- open2Set . push ( endNode ) ;
124
+ open2Set . push ( endNode )
126
125
127
126
// the `cameFrom` variable is accessed by both searches, so that we can store parents.
128
127
var cameFrom ;
@@ -149,17 +148,14 @@ function nba(graph, options) {
149
148
150
149
cameFrom . closed = true ;
151
150
152
- if (
153
- cameFrom . f1 < lMin &&
154
- cameFrom . g1 + f2 - heuristic ( from , cameFrom . node ) < lMin
155
- ) {
151
+ if ( cameFrom . f1 < lMin && ( cameFrom . g1 + f2 - heuristic ( from , cameFrom . node ) ) < lMin ) {
156
152
graph . forEachLinkedNode ( cameFrom . node . id , forwardVisitor ) ;
157
153
}
158
154
159
155
if ( open1Set . length > 0 ) {
160
156
// this will be used in reverse search
161
157
f1 = open1Set . peek ( ) . f1 ;
162
- }
158
+ }
163
159
}
164
160
165
161
function reverseSearch ( ) {
@@ -169,10 +165,7 @@ function nba(graph, options) {
169
165
}
170
166
cameFrom . closed = true ;
171
167
172
- if (
173
- cameFrom . f2 < lMin &&
174
- cameFrom . g2 + f1 - heuristic ( cameFrom . node , to ) < lMin
175
- ) {
168
+ if ( cameFrom . f2 < lMin && ( cameFrom . g2 + f1 - heuristic ( cameFrom . node , to ) ) < lMin ) {
176
169
graph . forEachLinkedNode ( cameFrom . node . id , reverseVisitor ) ;
177
170
}
178
171
@@ -193,13 +186,11 @@ function nba(graph, options) {
193
186
194
187
if ( blocked ( cameFrom . node , otherNode , link ) ) return ;
195
188
196
- var tentativeDistance =
197
- cameFrom . g1 + distance ( cameFrom . node , otherNode , link ) ;
189
+ var tentativeDistance = cameFrom . g1 + distance ( cameFrom . node , otherNode , link ) ;
198
190
199
191
if ( tentativeDistance < otherSearchState . g1 ) {
200
192
otherSearchState . g1 = tentativeDistance ;
201
- otherSearchState . f1 =
202
- tentativeDistance + heuristic ( otherSearchState . node , to ) ;
193
+ otherSearchState . f1 = tentativeDistance + heuristic ( otherSearchState . node , to ) ;
203
194
otherSearchState . p1 = cameFrom ;
204
195
if ( otherSearchState . h1 < 0 ) {
205
196
open1Set . push ( otherSearchState ) ;
@@ -208,7 +199,7 @@ function nba(graph, options) {
208
199
}
209
200
}
210
201
var potentialMin = otherSearchState . g1 + otherSearchState . g2 ;
211
- if ( potentialMin < lMin ) {
202
+ if ( potentialMin < lMin ) {
212
203
lMin = potentialMin ;
213
204
minNode = otherSearchState ;
214
205
}
@@ -225,13 +216,11 @@ function nba(graph, options) {
225
216
226
217
if ( blocked ( cameFrom . node , otherNode , link ) ) return ;
227
218
228
- var tentativeDistance =
229
- cameFrom . g2 + distance ( cameFrom . node , otherNode , link ) ;
219
+ var tentativeDistance = cameFrom . g2 + distance ( cameFrom . node , otherNode , link ) ;
230
220
231
221
if ( tentativeDistance < otherSearchState . g2 ) {
232
222
otherSearchState . g2 = tentativeDistance ;
233
- otherSearchState . f2 =
234
- tentativeDistance + heuristic ( from , otherSearchState . node ) ;
223
+ otherSearchState . f2 = tentativeDistance + heuristic ( from , otherSearchState . node ) ;
235
224
otherSearchState . p2 = cameFrom ;
236
225
if ( otherSearchState . h2 < 0 ) {
237
226
open2Set . push ( otherSearchState ) ;
@@ -247,7 +236,7 @@ function nba(graph, options) {
247
236
}
248
237
249
238
function visitN2Oriented ( otherNode , link ) {
250
- // we are going backwards, graph needs to be reversed.
239
+ // we are going backwards, graph needs to be reversed.
251
240
if ( link . toId === cameFrom . node . id ) return visitN2 ( otherNode , link ) ;
252
241
}
253
242
function visitN1Oriented ( otherNode , link ) {
0 commit comments