26

Beside A*, BFS, DFS and the like, what are other good path-finding algorithms/heuristics popularly used in Pacman? I don't think the ones I mentioned will work if there're more than one fruits for pacman to find.

I need some good path-finding algorithms that PacMan can use to finish the maze with the least possible step-count. I've tried to search around for a guideline, but so far no luck. A* with Manhattan distance is mentioned everywhere but it will only work with mazes with only one (or two? or maybe up to a few?) fruit to get.

BTW, to keep things simple, assuming that there're no ghosts around.

Some examples from the original PacMan problems: First, Second and Third

amit
  • 175,853
  • 27
  • 231
  • 333
IcySnow
  • 851
  • 2
  • 14
  • 23
  • 3
    not sure if this is what you mean but theres a great article here: http://gameinternals.com/post/2072558330/understanding-pac-man-ghost-behavior – krystan honour Apr 03 '12 at 14:09
  • 1
    What is the question exactly? how to get all the fruits with the shortest path [I guess not, this is variation of TSP, and you seem to be aware of it when you ask for heuristic]? Get the fruits With a short [but not shortest] path? – amit Apr 03 '12 at 14:11
  • Thanks. However I need algorithms/heuristics for PacMan to automatically find the best route (route with the least step count) and finish the maze, not something for the ghosts. – IcySnow Apr 03 '12 at 14:12
  • The only heuristic I've found and can think of so far to use with A* is Manhattan distance. Is there something else I'm not aware of? – IcySnow Apr 03 '12 at 14:15
  • @IcySnow: How many fruits are we talking about? – amit Apr 03 '12 at 14:16
  • One maze with full of them and one with 4 in the 4 corners. – IcySnow Apr 03 '12 at 14:18
  • Manhattan distance is the best heuristic for a grid where you can only move in horizontal and vertical directions, because it will never overestimate the distance between start and goal, i.e. it is an "admissable" heuristic. – Eric Apr 03 '12 at 14:22
  • There is a [nice blog post](https://medium.com/@robertgrosse/solving-the-traveling-pacman-problem-39c0872428bc) talking about this. It may be worth a read. – GWW Nov 29 '17 at 04:59

11 Answers11

22

Heuristic which worked for me if you know the look of labyrinth:

  1. Find real distance between two currently furthest fruits in labyrinth - let's call that x.
  2. Find real distance from current Pacman position to the closer of previous two fruits - let's call that y.

Then, answer is just: x + y.

Note that real distances are not Manhattan distances, but real distances in maze - you can calculate that (even precalculate if you want) because you know the look of labyrinth (you know all the walls, ...). This information (real distance between some two points in maze) is static because walls don't change.

The interpretation of this x + y formula could be something like this:

  • x - either way, you will have to travel this distance, at least at the end
  • y - while you are at the some of the two furthest fruits, it's better to collect the food that is near to it so you don't have to go back

If you are solving this as a part of Berkeley AI class project, for calculation of real distance between two points you could use function mazeDistance(pos1, pos2, gameState) which is already implemented and is using your implementation of bfs. Also, this heuristic is admissible and consistent, at least for their test cases. By the way, with this heuristic I managed to expand just 376 nodes in trickySearch maze.

Antonio Jurić
  • 541
  • 4
  • 15
  • Great heuristic , but a little bit time-consuming to compute. – Chen Chen Apr 05 '17 at 05:02
  • The distances can be precalculated for all points in a given maze though, and the resulting list used efficiently. – Ramon Oct 06 '17 at 04:44
  • this might be admissible, but it doesn't give the best results (in term of how many states we expanded). In Berkeley's trickySearch, I expanded 14227 nodes (where less then 7000 is the expected) – Alon Gouldman Nov 24 '19 at 12:29
  • How can I prove that this heuristic is consistent? – Adam Oct 30 '21 at 17:24
17

You comment says you are looking for shortest path. This problem is a variation of TSP on a planar graph, and thus is NP-Hard.

Possible heuristics function for A* that can solve the problem but is not admissible [thus the path found is not guaranteed to be optimal]:

Sum of manhattan distances from all fruits to the agent.

You can also use an edmissible heuristic, of #fruits - but it will take a long time.

If you are looking for optimal, well - it is hard. You can try all permutations of fruits, and check the total distance you need to travel. This solution is factorial in the number of fruits, and if it is greater then 20 - with naive bruteforce - it will take too long. You can somehow make it better by reducing the problem to TSP, and use dynamic-programming solution, which is also exponential, or some heuristical solutions to TSP.


One can also improve the non-admissible heuristic solution to provide an any-time algorithm:

iteratively run A* with a decreasing heuristic function: h(v) = h'(v) / m, where h' is the heuristic function on last iteration of A*, and m > 1. This guarantees that at some point, your heuristic function h will be admissible - and the solution found will be optimal. However, each iteration is expected to take much longer then the previous one [exponentially longer..]

amit
  • 175,853
  • 27
  • 231
  • 333
8

I found the closest approx food(using manhattan distances) but for my heuristic I used the actual distance from my position to the closest food. To this I added 1 for all those food points that dont share row or column with my position or closest food point.

Because the food points that do share row or col with my position or closest food position would be eaten while going from my position to the closest food and Ive already counted the cost of this in the actual distance I mentioned in the second line.

So, in short: heuristic = mazeDistance(my position, estimated closest food) + points left out

This was admissible and consistent. With this I was expanding 5500 nodes and got a 5/4 on the FoodHeuristic. https://github.com/sukritiverma1996/Intro-to-AI-course

Sukriti Verma
  • 81
  • 1
  • 1
5

I know this is old, but there are probably a lot of other people out there looking to solve this problem (it's part of Berkeley's free AI class). There's a lot of brute force suggestions, so I'll contribute a fairly simple one that gets pretty close and IS ADMISSIBLE:

  1. Find the nearest fruit
  2. Remove that fruit from the list of remaining fruits and add the distance to the total
  3. Find the nearest fruit to this fruit
  4. return to step 2 and repeat until there are no more fruits
  5. return the total

edit: Previous claim that this is an admissible heuristic is false. Sorry!

bendl
  • 1,583
  • 1
  • 18
  • 41
3

You could brute force it for small numbers of fruits in a reasonable sized maze.

  • Create a graph with a node for each piece of fruit in the maze.
  • Use A* or similar to find the distance between each pair of fruits. (You will need O(n^2) runs of A* to get all the pairwise distances between n fruits.)
  • Link the nodes (fruits) in the graph with edges weighted by distance.
  • Find the cheapest cycle in the graph (visiting all nodes at least once) by brute force. See cheapest cost traveral on complete graph.
Community
  • 1
  • 1
Li-aung Yip
  • 12,320
  • 5
  • 34
  • 49
2

I found two solutions.

The first is Antonio Juric's solution above, which calculates an excellent heuristic. However, this uses mazeDistance(), which calls BFS(), multiple times. This makes it very slow to calculate, and is akin to solving the problem using UCS, then using your solution to solve it again using A*.

My other solution, which checks 8475 nodes for me in trickySearch (but is twice as fast as the first solution), is as follows:

Let pos = the pacman's current position Let near = the coordinates of the closest piece of food by manhattan distance. Let MHD(a,b) = the Manhattan distance between a and b. Let far = the piece of food with maximum MHD(far,near).

The heuristic is calculated to be MHD(pos,near) + MHD(near,far). This is admissible and consistent. It is not as good of a heuristic, but it's much faster to calculate.

Guest_test
  • 21
  • 1
1

For eating all dots problem, I used the heuristic value as the maximum value of all the manhattan distances from all the food points to the current Pacman position. This is admissible as Pacman needs to travel at least this much distance to reach to the goal point. It is also consistent since the manhattan distance heuristic from a single point is always consistent. It expanded 9551 search nodes in the tricky search problem.

For corners food problems, use the heuristic as the sum of the first closest food from the Pacman. Relocate the Pacman to this food position, then repeat the previous step until all food pellets are get eaten. This is a greedy approach, which need not be admissible and consistent but this is a special scenario and it can be shown that this heuristic is admissible and consistent by considering all the cases. It expanded 485 nodes in the medium search problem.

Ankit Garg
  • 11
  • 1
  • Is there a mathematical algorithm that can accompany these remarks? – Michael Nelles Mar 18 '20 at 09:03
  • @Nelles, this is in reference to the UC Berkeley AI Pacman search assignment. As far as the numbers (nodes expanded) are concerned, they are obtained by running the program. The prooves for admissibility and consistency of these heuristics are trivial and thus not included. – Ankit Garg Mar 18 '20 at 09:36
0

if you want to reduce the number of nodes expanded and don't care about running time, I would recommend using Minimum Spanning Tree, the cost of edge should be mazeDistance and using a priorityQueue, every time adding a node into visited node, look for the nearest node to just added node and then adding it to visited node, until all the food node has been added into visited node. If you are doing with AI course problem, the node expanded should be very low.

Haobo_X
  • 77
  • 1
  • 8
0

Assuming this is for the Berkeley AI project:

In the general case, finding the shortest path that visits every dot is NP-hard. However, that does not mean it is hard in practice. The reason is because there are fixed parameter tractable algorithms and the Pacman mazes provided fall under the case of graphs that are easy to solve.

In particular, for any given branch-width, the shortest path can be found in time polynomial in the size of the graph (but exponential in the branch width of the graph) by a simple application of dynamic programming. This does not violate the NP-hardness since arbitrary graphs can have a large branch width, but it means that the problem can be solved efficiently if you only care about graphs that have a low branch width. The Pacman mazes provided have poor connectivity, and hence a low branch width.

For more details, see this post.

Antimony
  • 37,781
  • 10
  • 100
  • 107
0

Heuristic that I used that took about 9 seconds to find the path by expanding 1844 nodes for trickySearch:

  1. Find the food that is nearest to pacman's current position using the mazeDistance function. Let's say that the maze distance to this nearest food is min_dist and the position of this food is nearest_food.

  2. Find the food that is farthest to pacman's current position using the same method as above. Let's call the position of this food farthest_food.

  3. Now the heuristic that is returned is min_dist + mazeDistance(nearest_food, farthest_food)

This heuristic is consistent because if there are only 2 food points in the maze, the shortest path would be to collect the nearest food and then the farthest food. And as the number of food grows, it naturally takes more steps to collect the additional food.

-1

in a given game state, say md(x) is the manhattan distance from pacman to node x, consider minmd(X) as a function that return xmin s.t md(xmin)<=md(x) for all x in X. let X be the set of foods pacman has got left to eat.

Than think about it - if you consider a relaxation of your pacman world in which there are no walls, pacman cant walk less than md(xmin) where xmin=minmd(X) to eat some fruit, and then (after pacman has moved to xmin in order to eat it) if it wants to eat another fruit he must go no less than md(xmin1) where xmin1=minmd(X-{xmin}) and so on. return the sum of the mds pacman walked from xmin to xmin1 to xmin2 and so on and since this is an optimal solution to the relaxation you got yourself a great admissible and cosistent heuristic function!

Another point to consider, is that you can even get a better heuristic if you consider the walls, this is much tougher problem so i didnt get into it much, but I noticed that if you bound pacman in a rectangle with the next optimal fruit, he will have to pay at least 2 more actions if theres some FULL vertical or horizontal wall line between them because he would have to exit the bounding rectangle and re-enter it again paying at least 2 actions while doing so for each such wall. This can be further examined and you can also find more special features in this rectangle.

EDIT:

This heuristics is not admissible, thanks @Alon Gouldman for seeing that.

Ofek Ron
  • 8,354
  • 13
  • 55
  • 103
  • 1
    this is not admissible. In case there are two fruits that have the same `manhatten_distance`, this algorithm might fail. – Alon Gouldman Nov 24 '19 at 12:14
  • @AlonGouldman maybe i wasnt clear, added a clearification see (after pacman has moved to xmin) – Ofek Ron Nov 30 '19 at 00:00
  • 1
    I understood. consider this: https://ibb.co/J5rhp3v. When pacman will reach point X, the heuristic will spit a value that is greater then the real value – Alon Gouldman Dec 01 '19 at 07:27
  • I dont see why you think that – Ofek Ron Dec 01 '19 at 07:58
  • 1
    if i understand correctly, your heuristic will greedily collect all the corners. Am I right? if so, then (in the picture i sent), pacman (when standing on X) will want to collect the neerest corner, and then the other two corners. that will spit the value of 14 - while the real shortest value would be 12. – Alon Gouldman Dec 02 '19 at 10:38
  • how is 12 possible when you are standing on X and have 4 foods on all corners? – Ofek Ron Dec 03 '19 at 14:56
  • 1
    Pacman will eat the left-button corner, and then he will go to x. Then he will return 14 instead of 12. – Alon Gouldman Dec 04 '19 at 15:12
  • You are considering walls the algorithm doesnt. therefore after left bottom the next min distance food is right bottom and so on – Ofek Ron Dec 09 '19 at 09:23
  • 1
    you are right - when pacman will be in the left-bottom corner, the heuristic will spit a valid value. but at some point during the run, pacman will be at X, and then your heuristic will spit 14, while the real value (h*) is 12! please, tell me - after some iterations, when pacman will be at X - what will the heuristic return? – Alon Gouldman Dec 09 '19 at 09:28
  • it would depend on what food left to eat – Ofek Ron Dec 09 '19 at 15:58
  • 1
    lets walk step by step, along with the pacman: 1. A* is also optimal, so we know it will output the best path. if the image I sent is the start state, so the quickest way to collect all corners would be to first go for the left-bottom corner. It doesnt make sense for pacman to eat all other corners and only then go back to the left-bottom. So, the optimal path would first eat the bottom-left, and after that to go the eat the other corners. – Alon Gouldman Dec 10 '19 at 19:50
  • 1
    2. after pacman is in the left-bottom corner, he would want to go get the other corners. ANYWAYS, he must go to X - because that is the only way out of the walls. – Alon Gouldman Dec 10 '19 at 19:50
  • 1
    3. when pacman reaches X, the left-bottom corner is gone. at this point, pacman will calculate the heuristic value of x - which will yield 14. but an optimal path would cost only 12 steps - so the heuristic is not admissible. Am I missing something? – Alon Gouldman Dec 10 '19 at 19:53
  • 2
    You are right. this heuristics is not admissible after all – Ofek Ron Dec 15 '19 at 04:46