valantonini/AStar

Weights are ignored

Closed this issue ยท 4 comments

Expected

A* algorithm respects weights:

Given a weighted graph, a source node and a goal node, the algorithm [A*] finds the shortest path (with respect to the given weights) from source to goal.

Given a grid with weighted tiles around walls:
image

An agent should avoid hugging walls.

Actual

Agent ignores weights (positive or negative) and hugs walls:
image

Hi @fholmqvist,

The implementation never made use of the weights but I think somewhere the code mentions values other than 1 could be used for a weight in the future. It's come up a couple times and I wrote a short blog post on how you can change 1 line to achieve weighting. I believe xpoveda also came to a similar approach in a similar issue .

Are you using the source code directly or the nuget package?

Since this has been asked a few times, I have implemented weightings. It can be opted into via options to maintain backwards compatability for those who rely on the current behaviour.

var level = @"1111115
              1511151
              1155511
              1111111";
var world = Helper.ConvertStringToPathfinderGrid(level);
var opts = new PathFinderOptions { Weighting = Weighting.Positive };
var pathfinder = new PathFinder(world, opts);

A prerelease of this can be found on Nuget

That made all the difference, I appreciate the nice replies and excellent work. Thank you! ๐Ÿ™‚

you're welcome, thank you for the kind words.