Skip to content

Commit 116c912

Browse files
committedAug 13, 2023
fix: revert formatting changes
1 parent 1e5a9ee commit 116c912

File tree

1 file changed

+27
-38
lines changed

1 file changed

+27
-38
lines changed
 

‎a-star/nba/index.js

+27-38
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
module.exports = nba;
22

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');
77

88
var NO_PATH = defaultSettings.NO_PATH;
99

@@ -13,13 +13,13 @@ module.exports.l1 = heuristics.l1;
1313
/**
1414
* Creates a new instance of pathfinder. A pathfinder has just one method:
1515
* `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+
*
1919
* "Yet another bidirectional algorithm for shortest paths" paper by Wim Pijls and Henk Post
20-
*
20+
*
2121
* The paper is available here: https://repub.eur.nl/pub/16100/ei2009-10.pdf
22-
*
22+
*
2323
* @param {ngraph.graph} graph instance. See https://github.com/anvaka/ngraph.graph
2424
* @param {Object} options that configures search
2525
* @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;
3131
* which makes this search equivalent to Dijkstra search.
3232
* @param {Function(a, b)} options.distance - a function that returns actual distance between two
3333
* nodes `a` and `b`. By default this is set to return graph-theoretical distance (always 1);
34-
*
34+
*
3535
* @returns {Object} A pathfinder with single method `find()`.
3636
*/
3737
function nba(graph, options) {
@@ -59,17 +59,16 @@ function nba(graph, options) {
5959
* @returns {Array} of nodes between `toId` and `fromId`. Empty array is returned
6060
* if no path is found.
6161
*/
62-
find: find,
62+
find: find
6363
};
6464

6565
function find(fromId, toId) {
6666
// I must apologize for the code duplication. This was the easiest way for me to
6767
// implement the algorithm fast.
6868
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);
7170
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);
7372

7473
pool.reset();
7574

@@ -78,7 +77,7 @@ function nba(graph, options) {
7877
// forward search and it runs from source node to target, while the other one
7978
// (backward search) runs from target to source.
8079

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
8281
// backward search.
8382

8483
// For oriented graph path finding, we need to reverse the graph, so that
@@ -94,11 +93,11 @@ function nba(graph, options) {
9493
// These two heaps store nodes by their underestimated values.
9594
var open1Set = new NodeHeap({
9695
compare: defaultSettings.compareF1Score,
97-
setNodeId: defaultSettings.setH1,
96+
setNodeId: defaultSettings.setH1
9897
});
9998
var open2Set = new NodeHeap({
10099
compare: defaultSettings.compareF2Score,
101-
setNodeId: defaultSettings.setH2,
100+
setNodeId: defaultSettings.setH2
102101
});
103102

104103
// This is where both searches will meet.
@@ -111,7 +110,7 @@ function nba(graph, options) {
111110
// If variable names like `f1`, `g1` are too confusing, please refer
112111
// to makeNBASearchStatePool.js file, which has detailed description.
113112
var startNode = pool.createNewState(from);
114-
nodeState.set(fromId, startNode);
113+
nodeState.set(fromId, startNode);
115114
startNode.g1 = 0;
116115
var f1 = heuristic(from, to);
117116
startNode.f1 = f1;
@@ -122,7 +121,7 @@ function nba(graph, options) {
122121
endNode.g2 = 0;
123122
var f2 = f1; // they should agree originally
124123
endNode.f2 = f2;
125-
open2Set.push(endNode);
124+
open2Set.push(endNode)
126125

127126
// the `cameFrom` variable is accessed by both searches, so that we can store parents.
128127
var cameFrom;
@@ -149,17 +148,14 @@ function nba(graph, options) {
149148

150149
cameFrom.closed = true;
151150

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) {
156152
graph.forEachLinkedNode(cameFrom.node.id, forwardVisitor);
157153
}
158154

159155
if (open1Set.length > 0) {
160156
// this will be used in reverse search
161157
f1 = open1Set.peek().f1;
162-
}
158+
}
163159
}
164160

165161
function reverseSearch() {
@@ -169,10 +165,7 @@ function nba(graph, options) {
169165
}
170166
cameFrom.closed = true;
171167

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) {
176169
graph.forEachLinkedNode(cameFrom.node.id, reverseVisitor);
177170
}
178171

@@ -193,13 +186,11 @@ function nba(graph, options) {
193186

194187
if (blocked(cameFrom.node, otherNode, link)) return;
195188

196-
var tentativeDistance =
197-
cameFrom.g1 + distance(cameFrom.node, otherNode, link);
189+
var tentativeDistance = cameFrom.g1 + distance(cameFrom.node, otherNode, link);
198190

199191
if (tentativeDistance < otherSearchState.g1) {
200192
otherSearchState.g1 = tentativeDistance;
201-
otherSearchState.f1 =
202-
tentativeDistance + heuristic(otherSearchState.node, to);
193+
otherSearchState.f1 = tentativeDistance + heuristic(otherSearchState.node, to);
203194
otherSearchState.p1 = cameFrom;
204195
if (otherSearchState.h1 < 0) {
205196
open1Set.push(otherSearchState);
@@ -208,7 +199,7 @@ function nba(graph, options) {
208199
}
209200
}
210201
var potentialMin = otherSearchState.g1 + otherSearchState.g2;
211-
if (potentialMin < lMin) {
202+
if (potentialMin < lMin) {
212203
lMin = potentialMin;
213204
minNode = otherSearchState;
214205
}
@@ -225,13 +216,11 @@ function nba(graph, options) {
225216

226217
if (blocked(cameFrom.node, otherNode, link)) return;
227218

228-
var tentativeDistance =
229-
cameFrom.g2 + distance(cameFrom.node, otherNode, link);
219+
var tentativeDistance = cameFrom.g2 + distance(cameFrom.node, otherNode, link);
230220

231221
if (tentativeDistance < otherSearchState.g2) {
232222
otherSearchState.g2 = tentativeDistance;
233-
otherSearchState.f2 =
234-
tentativeDistance + heuristic(from, otherSearchState.node);
223+
otherSearchState.f2 = tentativeDistance + heuristic(from, otherSearchState.node);
235224
otherSearchState.p2 = cameFrom;
236225
if (otherSearchState.h2 < 0) {
237226
open2Set.push(otherSearchState);
@@ -247,7 +236,7 @@ function nba(graph, options) {
247236
}
248237

249238
function visitN2Oriented(otherNode, link) {
250-
// we are going backwards, graph needs to be reversed.
239+
// we are going backwards, graph needs to be reversed.
251240
if (link.toId === cameFrom.node.id) return visitN2(otherNode, link);
252241
}
253242
function visitN1Oriented(otherNode, link) {

0 commit comments

Comments
 (0)
Please sign in to comment.