-2

I have to program a robot to find the shortest distance to the goal location. In an arena which contains obstacles at random locations(not known beforehand).

The problem is, I will be given an array representing a grid. In this grid, 0 stands for movable space, 1 for obstacle. I have to program it to find the shortest path to the goal location of my choice, avoiding all the obstacles(walls).

Is A* a good approach for this problem or is there even a better approach?

Mohsin Anees
  • 698
  • 2
  • 9
  • 25
  • In short, yes A* is the way to go. – Vesper Jun 26 '15 at 12:42
  • Is there a generic way of assigning a heuristic function to any case? – Mohsin Anees Jun 26 '15 at 12:53
  • Your task looks most like plain pathfinding, so the distance function should be used (1 between two adjacent zeroes, too_much if either cell is 1). – Vesper Jun 26 '15 at 12:56
  • Can you please explain your last comment with more detail? – Mohsin Anees Jun 26 '15 at 13:01
  • Check this [A* pathfinding slow](http://stackoverflow.com/questions/10934722/a-pathfinding-slow). And it seems that I've missed on what is heuristics, I just went without optimizing the simple breadth first algorithm last time I made an A* implementation (for a 60x60 grid it wasn't slowing me in the slightest, so I left it as is). – Vesper Jun 26 '15 at 13:45

1 Answers1

2

As @Vesper said in the comments, A* is the way to go. As for the heuristic...

If your robot is restricted to moving left/right and up/down, typically Manhattan (or Taxicab or L1) distance is used as a heuristic function:

h(x,y) = |x - x_goal| + |y - y_goal|

It should be obvious that if there are no obstacles, then moving |x - x_goal| steps right or left, then |y - y_goal| steps up or down (or y, then x) cannot be longer than the actual shortest path to the goal with obstacles, therefore the heuristic is admissible.

If your robot can move on the diagonal, then Manhattan distance is no longer admissible, so you can use the Euclidean (or L2) distance:

h(x,y) = sqrt( (x - x_goal)^2 + (y - y_goal)^2 )

This is the straight-line distance between the two points and will never be any longer than any other possible path.

This makes Euclidean distance not only admissible for diagonal motion (at any angle), it also admissible for the case above where movement is restricted to the x and y directions. But in the case of restricted movement, Manhattan distance will generally give a path length that is closer to the actual path length, and so may speed up your pathfinding, and it's faster to compute.

beaker
  • 16,331
  • 3
  • 32
  • 49