Search code examples
searchartificial-intelligencea-star

How to impure A* algorithm to support multi-searching in a maze


If I have a A* function that supports finding the optimal path from a starting point to a target in a maze, how should I modify the heuristic function to be admissible so that if there are multiple targets the function still return the optimal result.


Solution

  • Assuming that the problem is to visit only one target:

    The first solution that comes to mind is to loop over all possible targets, compute the admissible heuristic value for each of them, and then finally return the minimum of those values as the final heuristic value. That way you're sure that the heuristic is still admissible (does not overestimate the distance to any of the targets).

    EDIT: Now assuming that the problem is to visit all targets:

    In this case, A* may not even necessarily be the best algorithm to use. You can use A* with the heuristic as described above to first find the shortest path to the closest target. Then, upon reaching the first (closest) target, you can run it again in the same way to find the next closest target, etc.

    But this may not give an optimal overall solution. It is possible that it is beneficial to first visit the second closest target (for example), because in some cases that may enable a cheaper follow-up path to the second target.

    If you want an optimal overall solution, you'll want to have a look at a different overall approach probably. Your problem will be similar to the Travelling Salesman Problem (though not exactly the same, since I think you're allowed to visit the same point multiple times for example). This link may also be able to help you.