Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add penalties for turns to avoid staircasing in A* algorithm #150

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/finders/AStarFinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ var DiagonalMovement = require('../core/DiagonalMovement');
* (defaults to manhattan).
* @param {number} opt.weight Weight to apply to the heuristic to allow for
* suboptimal paths, in order to speed up the search.
* @param {number} opt.avoidStarcasing Add penalties to discourage turning and

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param {number} opt.avoidStarcasing Add penalties to discourage turning and
* @param {boolean} opt.avoidStaircase Add penalties to discourage turning and

* causing a 'staircase' effect (defaults to false).
* @param {number} opt.turnPenalty Penalty to add to turning. Higher numbers
* discourage turning more (defaults to 1).
*/
function AStarFinder(opt) {
opt = opt || {};
Expand All @@ -24,6 +28,8 @@ function AStarFinder(opt) {
this.heuristic = opt.heuristic || Heuristic.manhattan;
this.weight = opt.weight || 1;
this.diagonalMovement = opt.diagonalMovement;
this.avoidStaircase = opt.avoidStaircase;
this.turnPenalty = opt.turnPenalty || 1;

if (!this.diagonalMovement) {
if (!this.allowDiagonal) {
Expand Down Expand Up @@ -59,9 +65,11 @@ AStarFinder.prototype.findPath = function(startX, startY, endX, endY, grid) {
endNode = grid.getNodeAt(endX, endY),
heuristic = this.heuristic,
diagonalMovement = this.diagonalMovement,
avoidStaircase = this.avoidStaircase,
turnPenalty = this.turnPenalty,
weight = this.weight,
abs = Math.abs, SQRT2 = Math.SQRT2,
node, neighbors, neighbor, i, l, x, y, ng;
lastDirection, node, neighbors, neighbor, i, l, x, y, ng;

// set the `g` and `f` value of the start node to be 0
startNode.g = 0;
Expand Down Expand Up @@ -98,6 +106,14 @@ AStarFinder.prototype.findPath = function(startX, startY, endX, endY, grid) {
// and calculate the next g score
ng = node.g + ((x - node.x === 0 || y - node.y === 0) ? 1 : SQRT2);

// if we're avoiding staircasing, add penalties if the direction
// will change
if (avoidStaircase) {
lastDirection = node.parent === undefined? undefined : { x : node.x - node.parent.x, y : node.y - node.parent.y };
var turned = lastDirection === undefined? 0 : lastDirection.x !== x - node.x || lastDirection.y !== y - node.y;
Comment on lines +112 to +113

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
lastDirection = node.parent === undefined? undefined : { x : node.x - node.parent.x, y : node.y - node.parent.y };
var turned = lastDirection === undefined? 0 : lastDirection.x !== x - node.x || lastDirection.y !== y - node.y;
lastDirection = node.parent === undefined ? undefined : { x: node.x - node.parent.x, y: node.y - node.parent.y };
var turned = lastDirection === undefined ? 0 : lastDirection.x !== x - node.x || lastDirection.y !== y - node.y;

ng += turnPenalty * turned;
}

// check if the neighbor has not been inspected yet, or
// can be reached with smaller cost from the current node
if (!neighbor.opened || ng < neighbor.g) {
Expand Down
8 changes: 7 additions & 1 deletion visual/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,14 @@ <h3>Options</h3>
<label class="option_label">Bi-directional</label> <br>
<input type="checkbox" class="dont_cross_corners">
<label class="option_label">Don't Cross Corners</label> <br>
<input class="spinner" name="astar_weight" value="1">
<input class="spinner astar_weight" name="astar_weight" value="1">
<label class="option_label">Weight</label> <br>
<br>
<input type="checkbox" class="avoid_staircase">
<label class="option_label">Avoid staircasing</label> <br>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<label class="option_label">Avoid staircasing</label> <br>
<label class="option_label">Avoid staircase</label> <br>

<input class="spinner turn_penalty" name="turn_penalty" value="1">
<label class="option_label">Turn Penalty</label> <br>

</div>
</div>

Expand Down
12 changes: 10 additions & 2 deletions visual/js/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ var Panel = {
'.bi-directional:checked').val() !=='undefined';
dontCrossCorners = typeof $('#astar_section ' +
'.dont_cross_corners:checked').val() !=='undefined';
avoidStaircase = typeof $('#astar_section ' +
'.avoid_staircase:checked').val() !=='undefined';

/* parseInt returns NaN (which is falsy) if the string can't be parsed */
weight = parseInt($('#astar_section .spinner').val()) || 1;
turnPenalty = parseInt($('#astar_section .turn_penalty').val()) || 1;
turnPenalty = turnPenalty >= 1 ? turnPenalty : 1; /* if negative or 0, use 1 */

/* parseInt returns NaN (which is falsy) if the string can't be parsed */
weight = parseInt($('#astar_section .astar_weight').val()) || 1;
weight = weight >= 1 ? weight : 1; /* if negative or 0, use 1 */

heuristic = $('input[name=astar_heuristic]:checked').val();
Expand All @@ -59,7 +65,9 @@ var Panel = {
allowDiagonal: allowDiagonal,
dontCrossCorners: dontCrossCorners,
heuristic: PF.Heuristic[heuristic],
weight: weight
weight: weight,
avoidStaircase: avoidStaircase,
turnPenalty: turnPenalty
});
}
break;
Expand Down
Loading