18

I am working with A-star algorithm, whereing I have a 2D grid and some obstacles. Now, I have only vertical and horizontal obstacles only, but they could vary densely.

Now, the A-star works well (i.e. shortest path found for most cases), but if I try to reach from the top left corner to the bottom right, then I see sometimes, the path is not shortest, i.e. there is some clumsiness in the path.

The path seem to deviate from the what the shortest should path should be.

Now here is what I am doing with my algorithm. I start from the source, and moving outward while calculating the value of the neighbours, for the distance from source + distance from destination, I keep choosing the minimum cell, and keep repeating until the cell I encounter is the destination, at which point I stop.

My question is, why is A-star not guaranteed to give me the shortest path. Or is it? and I am doing something wrong?

Thanks.

Kraken
  • 23,393
  • 37
  • 102
  • 162

3 Answers3

26

A-star is guaranteed to provide the shortest path according to your metric function (not necessarily 'as the bird flies'), provided that your heuristic is "admissible", meaning that it never over-estimates the remaining distance.

Check this link: http://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html

In order to assist in determining your implementation error, we will need details on both your metric, and your heuristic.

Update:
OP's metric function is 10 for an orthogonal move, and 14 for a diagonal move.

OP's heuristic only considers orthogonal moves, and so is "inadmissible"; it overestimates by ignoring the cheaper diagonal moves.

The only cost to an overly conservative heuristic is that additional nodes are visited before finding the minimum path; the cost of an overly aggressive heuristic is a non-optimal path possibl e being returned. OP should use a heuristic of:

        7 * (deltaX + deltaY)

which is a very slight underestimate on the possibility of a direct diagonal path, and so should also be performant.

Update #2:
To really squeeze out performance, this is close to an optimum while still being very fast:

7 * min(deltaX,deltaY) + 10 * ( max(deltaX,deltaY) - min(deltaX,deltaY) )

Update #3:
The 7 above is derived from 14/2, where 14 is the diagonal cost in the metric.

Only your heuristic changes; the metric is "a business rule" and drives all the rest. If you are interested on A-star for a hexagonal grid, check out my project here: http://hexgridutilities.codeplex.com/

Update #4 (on performance):
My impression of A-star is that it staggers between regions of O(N^2) performance and areas of almost O(N) performance. But this is so dependent on the grid or graph, the obstacle placement, and the start and end points, that it is hard to generalize. For grids and graphs of known particular shapes or flavours there are a variety of more efficient algorithms, but they often get more complicated as well; TANSTAAFL.

Pieter Geerkens
  • 11,775
  • 2
  • 32
  • 52
  • The metric function would be what? – Kraken Apr 26 '13 at 22:30
  • How you "measure" distance between nodes of your graph (or cells in your grid). – Pieter Geerkens Apr 26 '13 at 22:31
  • Probably Manhattan distance. Perhaps distance to end. It depends also on if there is any additional cost for entering different grid squares beyond that of obstacles (which would be very high indeed.) – Michael Dorgan Apr 26 '13 at 22:33
  • 1
    @Pieter Geerkens fromSource=fromSourceOfParent+10(for horizontal or vertical movement); or 14(for slant movement) fromDestination = 10*(positiveDifference(destY,currentY) + positiveDifference(destX,currentX)); – Kraken Apr 26 '13 at 22:34
  • @PieterGeerkens Is that what you meant? – Kraken Apr 26 '13 at 22:36
  • @Kraken: Yes. Your "from Source" is fine, though 1.4 is slightly less than a true sqrt(2). Can you show a small example of what you consider an incorrect path? – Pieter Geerkens Apr 26 '13 at 22:38
  • @PieterGeerkens Ohh, So i should make consider the Diagonal Moves for heuristic function too? That would correct the problem? – Kraken Apr 26 '13 at 22:42
  • @Kraken: use 7 * (deltaX + deltaY) instead of 10 * (deltaX + deltaY) – Pieter Geerkens Apr 26 '13 at 22:45
  • @PieterGeerkens Added the pic. Well, normally I am very good at Maths, but I can not understand how you got 7. If you can, kindly explain that. Thanks alot. – Kraken Apr 26 '13 at 22:47
  • @PieterGeerkens `7 * min(deltaX,deltaY) ` is for heuristic and the other for metrics right? And yeah, seems to be getting alot better with the Update #1. – Kraken Apr 26 '13 at 22:52
  • @K 7 = 14/2, where I use 14 because that is your diagonal cost. – Pieter Geerkens Apr 26 '13 at 22:53
  • 1
    Yes, only your heuristic changes; your metric is "a business rule" and dries all the rest. If you are interested on A-tar for a hexagonal grid, check out my project here: http://hexgridutilities.codeplex.com/ – Pieter Geerkens Apr 26 '13 at 22:54
0

I'm sure you are doing something wrong(Maybe some implementation flaw,your idea with A* sounds correct). A* guarantee gives the shortest path, it can be proved in math.

See this wiki pages will gives you all the information to solve your problem .

Sayakiss
  • 6,878
  • 8
  • 61
  • 107
-6

NO

A* is one of the fastest pathfinder algorithms but, it doesn't necessarily give the shortest path. If you are looking for correctness over time then it's best to use dijkstra's algorithm.

seaotternerd
  • 6,298
  • 2
  • 47
  • 58
Sandri
  • 5
  • 10
    A* is guaranteed to find the shortest path if the conditions are met (i.e. if you are using an admissible heuristic). – seaotternerd Nov 24 '13 at 05:43
  • ^ With the right heuristic A* guarantees the shortest path, with a bad heuristic A* still guarantees the shortest path but is nothing but a BFS and with a different heuristic A* can give you a path that's not guaranteed to be the shortest. – Sir Rogers Jul 15 '18 at 12:49