Docsity

Programming Assignment 1 for Introduction to Artificial Intelligence | CMSC 421, Assignments of Computer Science

Toggle sidebar
bg1
CMSC 421 Programming Assignment 1 Fall 2006
Due Date: Thursday, September 28, 10PM
For this programming, you will write a program to play the game Rushhour. We’ve provided some
code infrastructure in Lisp and test files. Please start early. While not a large amount of code is
required for the final solution, you will need time for running and debugging, so plan accordingly!
Note: Code available from http://www.cs.umd.edu/class/fall2006/cmsc421/projects/pa1.tar.
RushHour
In this game, your goal is to drive a designated vehicle (represented by the letter R) out of the traffic
jam and escape to freedom. To do so, you must maneuver all the blocking cars and trucks out of
your way.
You can gain some intuition on how to solve this problem by playing web-based version of the game
at:
http://www.puzzles.com/products/RushHour/RHfromMarkRiedel/Jam.html
http://gamerival.grab.com/index.cfm?game=6389B19F
Note that the first url works properly using Internet Explorer, but not Firefox.
A 6x6 grid describes the traffic world. All vehicles occupy either 2 or 3 grid squares. A vehicle can
only move along its axis of orientation (i.e. horizontal vehicles can only move left and right, verticle
vehicles can only move up and down). The idea is to have the R car reach the state where it can
exit the grid.
Example: Take a look at the test file test2. In this puzzle, there is one other vehicle in addition to
your vehicle R. (Spaces marked NIL are empty.) In order to solve the puzzle, v1 needs to be moved
down one step, and then R needs to be moved to the right one step. When R reaches the end of the
row, the goal has been achieved.
Note: The red vehicle always starts somewhere in the 3rd row (i.e. x=2 in a 0-based array), and
must move off the board to the right. Also, assume the car can only move one space at a time to
any direction.
Vehicles (other than your red car) must stay on the board.
Algorithm
You will use depth-first (dfs), breadth-first search (bfs) and A* to solve the RushHour game. Your
function should find a solution if one exist.
Code Infrastructure
Copy the code, rush-hour-basics.lisp, to your directory. Read the documentation in the file thor-
oughly. A couple of notes on the code:
1. Class - game: This class is used to define the game. It has four members: name, board,
parent (i.e., parent game) and depth (depth in search tree). Each of these members can be
accessed through their accessor member. For example, we can access the board for a game g
by (game-board g).
2. Macros: The x-coord and y-coord functions are implemented using Lisp’s macro capability.
This is an efficient way to do in-place expansion, giving a shorthand representation for fre-
quently used code fragments without the overhead of a function call. If you decide to use
macros in your own code, be sure you read up on them first.
3. Logging messages: If you use the format function with a second argument of *DEBUG*,
the message will be written to the global variable *DEBUG*. By default, this global variable
pf3
pf4

Partial preview of the text

Download Programming Assignment 1 for Introduction to Artificial Intelligence | CMSC 421 and more Assignments Computer Science in PDF only on Docsity!

CMSC 421 Programming Assignment 1 Fall 2006

Due Date: Thursday, September 28, 10PM

For this programming, you will write a program to play the game Rushhour. We’ve provided some code infrastructure in Lisp and test files. Please start early. While not a large amount of code is required for the final solution, you will need time for running and debugging, so plan accordingly!

Note: Code available from http://www.cs.umd.edu/class/fall2006/cmsc421/projects/pa1.tar.

RushHour

In this game, your goal is to drive a designated vehicle (represented by the letter R) out of the traffic jam and escape to freedom. To do so, you must maneuver all the blocking cars and trucks out of your way.

You can gain some intuition on how to solve this problem by playing web-based version of the game at: http://www.puzzles.com/products/RushHour/RHfromMarkRiedel/Jam.html http://gamerival.grab.com/index.cfm?game=6389B19F Note that the first url works properly using Internet Explorer, but not Firefox.

A 6x6 grid describes the traffic world. All vehicles occupy either 2 or 3 grid squares. A vehicle can only move along its axis of orientation (i.e. horizontal vehicles can only move left and right, verticle vehicles can only move up and down). The idea is to have the R car reach the state where it can exit the grid.

Example: Take a look at the test file test2. In this puzzle, there is one other vehicle in addition to your vehicle R. (Spaces marked NIL are empty.) In order to solve the puzzle, v1 needs to be moved down one step, and then R needs to be moved to the right one step. When R reaches the end of the row, the goal has been achieved.

Note: The red vehicle always starts somewhere in the 3rd row (i.e. x=2 in a 0-based array), and must move off the board to the right. Also, assume the car can only move one space at a time to any direction.

Vehicles (other than your red car) must stay on the board.

Algorithm

You will use depth-first (dfs), breadth-first search (bfs) and A* to solve the RushHour game. Your function should find a solution if one exist.

Code Infrastructure

Copy the code, rush-hour-basics.lisp, to your directory. Read the documentation in the file thor- oughly. A couple of notes on the code:

  1. Class - game: This class is used to define the game. It has four members: name, board, parent (i.e., parent game) and depth (depth in search tree). Each of these members can be accessed through their accessor member. For example, we can access the board for a game g by (game-board g).
  2. Macros: The x-coord and y-coord functions are implemented using Lisp’s macro capability. This is an efficient way to do in-place expansion, giving a shorthand representation for fre- quently used code fragments without the overhead of a function call. If you decide to use macros in your own code, be sure you read up on them first.
  3. Logging messages: If you use the format function with a second argument of DEBUG, the message will be written to the global variable DEBUG. By default, this global variable

is bound to T, which sends messages to standard output (i.e., the screen). You can send this output to a file by using open-debug with the name of the file. When you close the logging/debugging file with close-debug, it will revert to sending messages to standard output.

  1. Arrays: We’ve used an array to represent the game board. The main functions you need to know for manipulating arrays are make-array, array-dimensions, and aref, which derefer- ences an array location.
  2. I/O: We’ve written the load-game function, so for this assignment, you won’t need to do much of your own I/O.
  3. TIP: Because Lisp is an interpreted language, it’s very easy to test code fragments. We strongly recommend testing every function independently before moving on to the next one. The file test0 is the simplest possible case, so you may want to use this for your initial debug- ging.
  4. Debugging: Use trace, format and step functions.

The test cases are test1, test2, test3 and test4. Extra credit would be given if it works for test4. test0 is in the goal state.

Implementation

  1. [50 points] BFS and DFS Implement a generic uninformed search function, along the lines of Russell & Norvig, Figure 3.9 (page 72). Write wrapper functions that invoke this generic search function to perform breadth-first and depth-first search in the Rush Hour search space. Make sure the code is well documented.
  2. [50 points] A* Implement a heuristic function. Your heuristic is not required to be admissable. Implement A*, which uses the heuristic function to guide the search.
  3. [25 points] Results and Discussion Run your code on the test cases test1, test2, and test3 and generate the path to the goal state using the function, print-path-recursive. Find the number of nodes created and expanded, and also the length of solution. Submit one output file for each test case, containing the above information, for each of the three algorithms. Include a short writeup (2 pages max), describing the heuristic you used, whether it is admiss- able or not and your results with the three different search algorithms.
  4. [45 points] Bonus

(a) [15 points] Extra credit if you try more than one heuristic and compare results in your writeup. (b) [15 points] Extra credit if you also try greedy best-first search. (c) [15 points] Extra credit if you include results for test4.

Deliverables

The deliverables of this project is a single tar file, your-glue-id.tar, must contain the following files.

  1. The source code in rush-hour-basics.lisp. The code must run using ’alisp’ in the Grace servers and the following functions must be defined:
  1. BASIC-SEARCH: Write the BASIC-SEARCH function as given in Russell & Norvig’s Figure 3.9. Note that the queueing function is sent into this function – the best way to do this is to send in the function binding, e.g., use (BASIC-SEARCH ’Q-BFS file) to invoke the Q-BFS function. You should also keep track of the cumulative number of generated and expanded nodes, since you’ll need to report those in your final results. This function should return a list of four things: either the symbol ’SUCCESS or ’FAIL, the goal node found, the number of created nodes, and the number of expanded nodes. You can then pass this list to PRINT- SEARCH-INFO, which will print a summary of the search, including the path back to the node (assuming you’ve linked all of your parent nodes correctly!)
  2. Handling Repeated States: Detecting repeated states is very important in this domain, so you may want to have this function maintain a CLOSED list (list of nodes that have been expanded) as well as the usual OPEN list. You’ll probably want a separate function to detect repeated states and remove them from the list of new nodes, or you could do this check in the EXPAND function. (Note that I’ve given you ARRAY-EQUAL, which can be used to test whether two game boards are the same.) You’ll also want to check for the depth limit, and may want to have the option of setting the depth limit to NIL, meaning that there is no depth limit.
  3. EXPAND: Write the EXPAND function: given a node to expand, apply APPLY-MOVE to all of the moves listed by LEGAL-MOVES.
  4. Q-BFS and Q-DFS: Write Q-BFS (the function to be called by BASIC-SEARCH) and BFS (the wrapper that invokes BASIC-SEARCH with Q-BFS). Write Q-DFS (the function to be called by BASIC-SEARCH) and DFS (the wrapper that invokes BASIC-SEARCH with Q-DFS). These functions should just be a line or two each.
  5. Write a heuristic function, which given a board, gives an estimate of how good it is. Your heuristic is not required to be admissable (but it can be).
  6. A: Write Q-A (the function to be called by BASIC-SEARCH) and A* (the wrapper that invokes BASIC-SEARCH with Q-A*).

You’re done! :-) (modulo testing, gathering results, debugging, getting rid of the infinite loops...)