cd ~/cs63/labs git clone git@...
The objectives of this lab are to:
For this lab you will modify the following files:
We will be using a text-based version of the Traffic Jam game in this lab. Complete the following steps to familiarize yourself with the game.
./TrafficJam.py puzzles/biggerA.txtYou will see a picture of the board and a list of valid moves you may make (as shown below). In this case the gird is 6x6, but the grid size can change between puzzles. The exit is marked by two vertical bars. Each car is represented by a unique number. In this example, cars 1, 2, and 6 are positioned horizontally and can only move left or right, while cars 0, 3, 4, and 5 are positioned vertically and can only move up or down. Cars may be different lengths but are always only one grid square wide. Empty locations are marked by a dash. The goal of the game is to move the car labeled 0 to the exit.
| | 1 1 2 2 3 - 0 - - - 3 - 0 - - - 3 - - - 4 - 5 - - - 4 - 5 - - - 4 6 6 - a: car0 down b: car4 up c: car6 right Select move (or q to quit):You should be able to solve this particular puzzle in 7 moves.
[['1', '1', '2', '2', '3', '-'], ['0', '-', '-', '-', '3', '-'], ['0', '-', '-', '-', '3', '-'], ['-', '-', '4', '-', '5', '-'], ['-', '-', '4', '-', '5', '-'], ['-', '-', '4', '-', '6', '6']]
In this lab you will re-implement BFS, this time using a Node class to create a graph-based search.
create a node to store the initial game board add this node to the FIFO frontier while frontier not empty get currNode from frontier if currNode's state is a goal show the path return increment the expanded node counter if currNode's state is not in visited add state to visited find all possible moves from currNode's state for each move determine the nextState if nextState is not in visited create a node for nextState add this node to the frontier report that the game has no solution
Next you will implement A* search. The key difference between informed searches, like A*, and uninformed searches, like BFS, is that they estimate the cost associated with each node. For A* this cost is the sum of how much work it took to get to a node and an estimate of how far it is to the goal from a node.
For the Traffic Jam Game the cost of getting to a node will simply be the number of actions taken, which is stored in the depth of the node. To estimate the distance to the goal you will use a heuristic function.
Complete the implementation of informed search as follows:
create a node to store the initial game board add this node to the PQ frontier with a priority of 0 while frontier not empty get currNode from frontier if currNode's state is goal show the path return increment the expanded node counter if currNode's state is not in visited state to visited find all possible moves from currNode's state for each move determine the nextState if nextState is not in visited create a node for nextState set priority based on depth and the heuristic add this node to the frontier with priority report that the game has no solution
For example in the starting board for puzA.txt there are two cars blocking the exit and car0 needs to move up 2 rows to reach the exit, so the heuristic should return 4. While in the starting board for puzB.txt there are three cars blocking the exit and car0 needs to move up 3 rows to reach the exit, thus the heuristic should return 6.
Test that your heuristic returns the correct values for all the game boards given in the puzzles directory.
Once you've completed informed search you are ready to compare the performance of the two search methods.
Complete the table provided in the experiments file. The columns for 'Informed expanded' and 'Percent gain' should be based on the blockingHeuristic. You should add two columns to the table: one for the number of states expaned and one for the percent gain with your better heuristic. Here is an empty table with the extra columns added. You should calculate percent gain as (1 - A* / BFS) * 100. After the table, add a paragraph reflecting on when A* shows the largest gains over BFS.
This is not required, and should only be attempted once you have completed the required portions of this lab.
First, try implementing greedy search (only considering the heuristic value, and ignoring the cost so far). How does this affect path length? Number of nodes expanded?
Next, come up with a puzzle on which greedy search performs badly but A* search performs well. There will be a Piazza post for sharing such puzzles; a small bonus will be awarded for particularly good puzzles.
cd ~/cs63/labs/01 git add UninformedSearch.py InformedSearch.py experiments git add puzzles/mypuz.txt git commit git push