$25
In this assignment, you will build general-purpose search algorithms and apply them to solving puzzles. In Part 1, you will be in charge of a "Pacman"-like agent that needs to find a path through maze to eat a dot or "food pellet." In Part 2, you will need to find a single path that goes through all the dots in the maze.
Programming language
This MP will be written in Python. If you've never used Python before, you should start getting used to it. A good place to start is the Python Tutorial (also available in hardcopy form). You should install version 3.6 or 3.7 on your computer, as well as the pygame graphics package.
Your code may import extra modules, but only ones that are part of the standard python library . Unless otherwise specified in the instructions for a specific MP, the only external library available during our grading process will be pygame. For example: in mp1, numpy is not allowed.
Part 1: Basic Pathfinding
Consider the problem of finding the shortest path from a given start state while eating one or more dots or "food pellets." The image at the top of this page illustrates the simple scenario of a single dot, which in this case can be viewed as the unique goal state. The maze layout will be given to you in a simple text format, where '%' stands for walls, 'P' for the starting position, and '.' for the dot(s) (see sample maze.txt). All step costs are equal to one.
Implement the state representation, transition model, and goal test needed for solving the problem in the general case of multiple dots. For the state representation, besides your current position in the maze, is there anything else you need to keep track of? For the goal test, keep in mind that in the case of multiple dots, the Pacman does not necessarily have a unique ending position. Next, implement a unified top-level search routine that can work with all of the following search strategies, as covered in class and/or the textbook (Note: you don’t need to find the best solution when using DFS):
• Depth-first search
• Breadth-first search
• Greedy best-first search
• A* search
For this part of the assignment, use the Manhattan distance from the current position to the goal as the heuristic function for greedy and A* search.
Run each of the four search strategies on the following inputs:
• Medium maze (mediumMaze.txt)
• Big maze (bigMaze.txt)
• Open maze (openMaze.txt)
The provided code will generate a pretty picture of your solution. Your report should include
• The solution pictures.
• The length of the path. Include both the start and goal positions as part of your path and path length.
• Number of nodes expanded by the search algorithm.
Part 2: Search with multiple dots
Now consider the harder problem of finding the shortest path through a maze while hitting multiple dots. Once again, the Pacman is initially at P, but now there is no single goal position. Instead, the goal is achieved whenever the Pacman manages to eat all the dots. Once again, we assume unit step costs.
As instructed in Part 1, your state representation, goal test, and transition model should already be adapted to deal with this scenario. The next challenge is to solve the following inputs using A* search using an admissible heuristic designed by you:
• Tiny search (tinySearch.txt)
• Small search (smallSearch.txt)
• Medium search (mediumSearch.txt)
You should be able to handle the tiny search using uninformed BFS. In fact, it is a good idea to try that first for debugging purposes, to make sure your representation works with multiple dots. However, to successfully handle all the inputs, it is crucial to come up with a good heuristic. For full credit, your heuristic should be admissible and should permit you to find the solution for the medium search in a reasonable amount of time. In your report, explain the heuristic you chose, and discuss why it is admissible and whether it leads to an optimal solution.
Hints: In the past almost all working solutions to this problem have used a heuristic based on the minimum spanning tree. The minimum spanning tree of a set of points can be computed easily via Kruskal's algorithm or Prim's algorithm. If T is the total length of the edges in the minimum spanning tree, then the shortest path connecting all the points must have length between T and 2T. Now, suppose you are at some location (x,y) with a set of S dots still to reach. Your heuristic function h might be the sum of the distance from (x,y) to the nearest dot, plus the MST length for the dots in S.
Provided Code Skeleton
We have provided (.tar file or .zip file) all the code to get you started on your MP, which means you will only have to write the search functions. Do not modify provided code. You will only have to modify search.py. maze.py
• getStart() :- Returns a tuple of the starting position, (row, col)
• getObjectives() :- Returns a list of tuples that correspond to the dot positions, [(row1, col1), (row2, col2)]
• isValidMove(row, col) :- Returns the boolean True if the (row, col) position is valid. Returns False otherwise.
• getNeighbors(row, col) :- Given a position, returns the list of tuples that correspond to valid neighbor positions. This will return at most 4 neighbors but may return less.
There are 4 methods to implement in this file, namely bfs(maze), dfs(maze), greedy(maze), and astar(maze). (You may need to add another named search method if you implement an additional search method for extra credit.) Each of these functions takes in a maze instance and should return both the path taken (as a list of tuples) and the number of states explored. The maze instance provided will already be instantiated, and the above methods will be accessible.
To understand how to run the MP, read the provided README.md or run python3 mp1.py -h into your terminal. The following command will display a maze and let you create a path manually using the arrow keys.
python mp1.py --human maps/maze.txt
The following command will run your astar search method on the maze.
python mp1.py --method astar maps/maze.txt
You can also save your output picture as a file in tga format. If your favorite document formatter doesn't handle tga, tools such as gimp can convert it to other formats (e.g. jpg).
1. Title Page:
List of all team members, course number and section for which each member is registered, date on which the report was written
2. Section I:
Algorithms (Search). This section should describe algorithms and data structures used for all four search strategies. Answer questions like: what is a state? What is the frontier? Do you maintain an explored states list? How are repeated states detected and managed?
3. Section II:
Algorithms (A* and Greedy BFS). This section should describe the heuristic(s) used for A* and Greedy BFS, for both the single dot and multiple-dot situations Provide proof that the heuristics for A* are admissible.
4. Section III:
Results (Basic Pathfinding). For every algorithm in part 1, (DFS, BFS, Greedy, A*), and every one of the mazes, (medium, big, open), give the maze screenshot with the computed path, the solution cost and the number of expanded nodes (12 cases total)
5. Section IV:
Results (Search with multiple dots). For part 2, for each of three mazes (tiny, small, medium), give the solution screenshot, solution cost, and number of expanded nodes for your A* algorithm.