Skip to content

Commit

Permalink
Merge branch 'bemisguided-feat/add-blocked-paths'
Browse files Browse the repository at this point in the history
  • Loading branch information
anvaka committed Aug 12, 2023
2 parents ce4aa16 + 257d863 commit 7946996
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 1 deletion.
8 changes: 8 additions & 0 deletions a-star/a-greedy-star.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ function aStarBi(graph, options) {
// whether traversal should be considered over oriented graph.
var oriented = options.oriented;

var blocked = options.blocked;
if (!blocked) blocked = defaultSettings.blocked;

var heuristic = options.heuristic;
if (!heuristic) heuristic = defaultSettings.heuristic;

Expand Down Expand Up @@ -182,6 +185,11 @@ function aStarBi(graph, options) {
return;
}

if (blocked(otherSearchState.node, cameFrom.node, link)) {
// Path is blocked. Ignore this route
return;
}

if (canExit(otherSearchState, cameFrom)) {
// this node was opened by alternative opener. The sets intersect now,
// we found an optimal path, that goes through *this* node. However, there
Expand Down
8 changes: 8 additions & 0 deletions a-star/a-star.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ function aStarPathSearch(graph, options) {
// whether traversal should be considered over oriented graph.
var oriented = options.oriented;

var blocked = options.blocked;
if (!blocked) blocked = defaultSettings.blocked;

var heuristic = options.heuristic;
if (!heuristic) heuristic = defaultSettings.heuristic;

Expand Down Expand Up @@ -112,6 +115,11 @@ function aStarPathSearch(graph, options) {
otherSearchState.open = 1;
}

if (blocked(otherNode, cameFrom.node, link)) {
// Path is blocked. Ignore this route
return;
}

var tentativeDistance = cameFrom.distanceToSource + distance(otherNode, cameFrom.node, link);
if (tentativeDistance >= otherSearchState.distanceToSource) {
// This would only make our path longer. Ignore this route.
Expand Down
5 changes: 5 additions & 0 deletions a-star/defaultSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
// Path search settings
heuristic: blindHeuristic,
distance: constantDistance,
blocked: neverBlocked,
compareFScore: compareFScore,
NO_PATH: NO_PATH,

Expand All @@ -29,6 +30,10 @@ function constantDistance(/* a, b */) {
return 1;
}

function neverBlocked(/* a, b, c */) {
return false;
}

function compareFScore(a, b) {
var result = a.fScore - b.fScore;
// TODO: Can I improve speed with smarter ties-breaking?
Expand Down
21 changes: 21 additions & 0 deletions dist/ngraph.path.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ function aStarBi(graph, options) {
// whether traversal should be considered over oriented graph.
var oriented = options.oriented;

var blocked = options.blocked;
if (!blocked) blocked = defaultSettings.blocked;

var heuristic = options.heuristic;
if (!heuristic) heuristic = defaultSettings.heuristic;

Expand Down Expand Up @@ -307,6 +310,11 @@ function aStarBi(graph, options) {
return;
}

if (blocked(otherSearchState.node, cameFrom.node, link)) {
// Path is blocked. Ignore this route
return;
}

if (canExit(otherSearchState, cameFrom)) {
// this node was opened by alternative opener. The sets intersect now,
// we found an optimal path, that goes through *this* node. However, there
Expand Down Expand Up @@ -394,6 +402,9 @@ function aStarPathSearch(graph, options) {
// whether traversal should be considered over oriented graph.
var oriented = options.oriented;

var blocked = options.blocked;
if (!blocked) blocked = defaultSettings.blocked;

var heuristic = options.heuristic;
if (!heuristic) heuristic = defaultSettings.heuristic;

Expand Down Expand Up @@ -468,6 +479,11 @@ function aStarPathSearch(graph, options) {
otherSearchState.open = 1;
}

if (blocked(otherNode, cameFrom.node, link)) {
// Path is blocked. Ignore this route
return;
}

var tentativeDistance = cameFrom.distanceToSource + distance(otherNode, cameFrom.node, link);
if (tentativeDistance >= otherSearchState.distanceToSource) {
// This would only make our path longer. Ignore this route.
Expand Down Expand Up @@ -510,6 +526,7 @@ module.exports = {
// Path search settings
heuristic: blindHeuristic,
distance: constantDistance,
blocked: neverBlocked,
compareFScore: compareFScore,
NO_PATH: NO_PATH,

Expand All @@ -532,6 +549,10 @@ function constantDistance(/* a, b */) {
return 1;
}

function neverBlocked(/* a, b, c */) {
return false;
}

function compareFScore(a, b) {
var result = a.fScore - b.fScore;
// TODO: Can I improve speed with smarter ties-breaking?
Expand Down
2 changes: 1 addition & 1 deletion dist/ngraph.path.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ declare module "ngraph.path" {
quitFast?: boolean
heuristic?: (from: Node<NodeData>, to: Node<NodeData>) => number
distance?: (from: Node<NodeData>, to: Node<NodeData>, link: Link<LinkData>) => number
blocked?: (from: Node<NodeData>, to: Node<NodeData>, link: Link<LinkData>) => boolean
}

interface PathFinder<NodeData> {
Expand Down
43 changes: 43 additions & 0 deletions test/a-star.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,49 @@ test('it can find weighted', t => {
t.end();
});

test('A* does not follow blocked paths', t => {
let graph = createGraph();

graph.addLink('a', 'b', {blocked: true});
graph.addLink('a', 'c', {blocked: false});
graph.addLink('c', 'd', {blocked: false});
graph.addLink('b', 'd', {blocked: false});


var pathFinder = aStar(graph, {
blocked(a, b, link) {
return link.data.blocked;
}
});
let path = pathFinder.find('a', 'd');

t.equal(path[0].id, 'd', 'd is here');
t.equal(path[1].id, 'c', 'c is here');
t.equal(path[2].id, 'a', 'a is here');
t.end();
});

test('A* greedy does not follow blocked paths', t => {
let graph = createGraph();

graph.addLink('a', 'b', {blocked: true});
graph.addLink('a', 'c', {blocked: false});
graph.addLink('c', 'd', {blocked: false});
graph.addLink('b', 'd', {blocked: false});

var pathFinder = aGreedy(graph, {
blocked(a, b, link) {
return link.data.blocked;
}
});
let path = pathFinder.find('a', 'd');

t.equal(path[2].id, 'd', 'd is here');
t.equal(path[1].id, 'c', 'c is here');
t.equal(path[0].id, 'a', 'a is here');
t.end();
});

test('A* can find directed path', t => {
let graph = createGraph();

Expand Down

0 comments on commit 7946996

Please sign in to comment.