Homework 1 Search
In this homework assignment, you will be working with Python 3 and modifying the search.py file within the provided search.zip archive. Your task is to implement two functions: aStarSearch and manhattanHeuristic. You will be using the Pacman environment for testing and evaluating your search algorithm. The environment was created at UC Berkeley (http://ai.berkeley.edu ).
Project Overview:
The provided search.zip file contains the following components:
1. search.py: This is the main Python file that you need to modify to implement the A* search algorithm with the Manhattan heuristic.
2. Google Colab Notebook (https://colab.research.google.com/drive/1r_zlADKx_xeSDk7n0usxs0HUFB0a8SFo? usp=sharing ): This notebook offers overall guidance on solving search problems.
Tasks:
-
Implement the manhattanHeuristic function in the search.py file. This function should calculate the Manhattan heuristic for a given state.
-
Implement the aStarSearch function in the search.py file. This function should perform A* search using a heuristic to find the optimal route.
Usage Instructions:
To test your implementation, you can use the following commands in the terminal:
• Breadth First Search: python pacman.py -l mediumMaze -p SearchAgent -a fn=bfs
• Depth First Search: python pacman.py -l mediumMaze -p SearchAgent -a fn=dfs
• A* Search with Euclidean Heuristic: python pacman.py -l mediumMaze -p SearchAgent -a fn=astar,heuristic=euclideanHeuristic
• A* Search with Manhattan Heuristic (Your Implementation): python pacman.py -l mediumMaze -p SearchAgent -a fn=astar,heuristic=manhattanHeuristic
Submission Instructions:
You only need to upload the modified search.py file to the Moodle platform. Ensure that your implementation correctly returns the following lists for the aStarSearch function:
-
actions: The final route the agent must complete.
-
visitedNodes: Nodes visited during the search process to find the optimal route.
-
visitedCosts: The costs (heuristic + actual cost to get there) of the visited nodes.
Be careful not to leave any print functions in this file!
Example Solution (for the tinyMaze test case):
For the test case python pacman.py -l tinyMaze -p SearchAgent -a fn=astar,heuristic=manhattanHeuristic, the expected output should be:
actions: [(0, 'South'), (0, 'South'), (0, 'West'), (0, 'South'), (0, 'West'), (0, 'West'), (0, 'South'), (0, 'West')]
visitedNodes: [(5, 5), (5, 4), (4, 5), (5, 3), (3, 5), (4, 3), (2, 5), (4, 2), (1, 5), (3, 2), (1, 4), (2, 2), (1, 3), (2, 1), (1, 1)]
visitedCosts: [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8]
Example Solution (for the mediumMaze test case):
For the test case python pacman.py -l mediumMaze -p SearchAgent -a fn=astar,heuristic=manhattanHeuristic, the expected output should be:
actions: [(0, 'West'), (0, 'West'), (0, 'West'), (0, 'West'), (0, 'West'), (0, 'West'), (0, 'West'), (0, 'West'), (0, 'West'), (0, 'South'), (0, 'South'), (0, 'East'), (0, 'East'), (0, 'South'), (0, 'South'), (0, 'South'), (0, 'West'), (0, 'West'), (0, 'West'), (0, 'North'), (0, 'West'), (0, 'West'), (0, 'West'), (0, 'West'), (0, 'South'), (0, 'South'), (0, 'South'), (0, 'East'), (0, 'East'), (0, 'East'), (0, 'East'), (0, 'East'), (0, 'East'), (0, 'East'), (0, 'South'), (0, 'South'), (0, 'South'), (0, 'South'), (0, 'South'), (0, 'South'), (0, 'South'), (0, 'West'), (0, 'West'), (0, 'West'), (0, 'West'), (0, 'West'), (0, 'West'), (0, 'West'), (0, 'West'), (0, 'West'), (0, 'West'), (0, 'West'), (0, 'West'), (0, 'West'), (0, 'West'), (0, 'West'), (0, 'West'), (0, 'West'), (0, 'South'), (0, 'West'), (0, 'West'), (0, 'West'), (0, 'West'), (0, 'West'), (0, 'West'), (0, 'West'), (0, 'West'), (0, 'West')]
visitedNodes: [(34, 16), (34, 15), (33, 16), (34, 14), (32, 16), (33, 14), (31, 16), (32, 14), (30, 16), (31, 14), (29, 16), (30, 14), (28, 16), (30, 13), (27, 16), (30, 12), (26, 16), (25, 16), (25, 15), (24, 16), (25, 14), (23, 16), (22, 16), (21, 16), (20, 16), (19, 16), (18, 16), (17, 16), (16, 16), (15, 16), (14, 16), (13, 16), (12, 16), (11, 16), (10, 16), (9, 16), (8, 16), (7, 16), (6, 16), (5, 16), (4, 16), (3, 16), (2, 16), (1, 16), (1, 15), (1, 14), (1, 13), (1, 12), (1, 11), (1, 10), (1, 9), (1, 8), (1, 7), (31, 12), (26, 14),(2, 7), (32, 12), (27, 14), (3, 7), (27, 13), (27, 12),
(27, 11), (26, 11), (25, 11), (24, 11), (33, 12), (4, 7), (24, 12), (23, 12), (22, 12), (21, 12), (20, 12), (20, 11), (19, 12), (20, 10), (19, 11), (18, 12), (20, 9), (17, 12), (16, 12), (15, 12), (14, 12), (14, 11), (13, 12), (14, 10), (12, 12), (12, 11), (12, 10), (11, 10), (10, 10), (34, 12), (4, 8), (21, 9), (17, 13), (15, 10), (12, 13), (10, 11), (34, 11), (34, 10), (33, 10), (32, 10), (31, 10), (30, 10), (30, 9), (29, 10), (30, 8), (4, 9), (22, 9), (17, 14), (16,10), (12, 14), (10, 12), (31, 8), (16, 14), (15, 14), (14, 14), (13, 14), (4, 10), (23, 9), (17, 10), (10, 13), (32, 8), (3, 10), (17, 9), (17, 8), (16, 8), (15, 8), (14, 8), (13, 8), (12, 8), (11, 8), (11, 7), (11, 6), (10, 6), (9, 6), (8, 6), (7, 6), (6, 6), (6,5), (5, 5), (4, 5), (3, 5), (2, 5), (1, 5), (1, 4), (1, 3), (4, 11), (24, 9), (10, 14), (33, 8), (2, 3), (9, 14), (8, 14), (8, 13), (8, 12), (8, 11), (8, 10), (8, 9), (8, 8), (7, 8), (6, 8), (4, 12), (25, 9), (34, 8), (3, 3), (6, 9), (34, 7), (34, 6), (33, 6), (32, 6), (31, 6), (30, 6), (30, 5), (29, 6), (30, 4), (30, 3), (4, 13), (26, 9), (4, 3), (6, 10), (31, 4), (31, 3), (4, 14), (27, 9), (5, 3), (6, 11), (32, 4), (27, 8), (27, 7), (27, 6), (27, 5), (27, 4), (27, 3), (27, 2), (27, 1), (26, 2), (25, 2), (24, 2), (23, 2), ( 22, 2), (21, 2), (20, 2), (19, 2), (18, 2), (17, 2), (16, 2), (15, 2), (14, 2), (13, 2), (12, 2), (11, 2), (10, 2), (10, 1), (9, 1), (8, 1), (7, 1), (6, 1), (5, 1), (4, 1), (3, 1), (2, 1), (1, 1)]
visitedCosts: [48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 50, 50, 50, 52, 52, 52, 52, 52, 52, 52, 52, 52, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 58, 58, 58, 58, 58, 58, 58, 58, 58, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 66, 66, 66, 66, 66, 66, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68]
48, 48,
48, 48,
54, 54,
54, 56,
58, 58,
60, 60,
62, 62,
64, 64,
68, 68,
68, 68,