Solving the 8-puzzle
Due: Jan 13th 11:59AM
The Game: The 8 puzzle is a simple game played on a 3x3 board with 8 tiles numbered from 1 to 8 and a blank space. The following is a depiction of how one instance of a board might look.
1 3 5
6 7 X
8 2 4
Here X represents the blank space on the board. The goal of the game is to rearrange the tiles such that the tiles appear in the following order.
1 2 3
4 5 6
7 8 X
Any numbered tile may slide into an adjacent position if the adjacent position is the blank. The following is one of 4 valid operations.
1 2 3 1 2 3
4 X 5 => 4 5 X
6 8 7 6 8 7
In this example, the tile numbered 5 has moved left.
For uniformity, you are required accept the following representation of the board as input. Of course, you can immediately transform it to another representation if you wish. This example board also represents the goal state of the game.
((1 2 3)
(4 5 6)
(7 8 X))
Each sub-list is a row of the board and 'X represents the blank space.
Write a general function called search that accepts the following information.
This function should return a list of moves in the form '((move tile direction)) necessary to go from the start state to the goal state.
For example:
(search '((1 2 3) (X 5 6) (4 7 8)) bfs #f -1)
would return
((move 4 up)
(move 7 left)
(move 8 left)
Note: Use "right" and "down" for the other two directions.
Searching:
Your general search function should be similar to the one written in class. This requires that you implement certain functions that are independent of the type of search that you are performing.
In addition to these two functions, you will also write the search specific function for each of the three types of searches that you will be testing. Each search specific function should accept a list of new states, a list of current states and an evaluation function (if none is needed you can pass #f or '(). This function will return the new queue of states to continue searching with.
For testing purposes (I will not use it), you may want to implement an additional wrapper function so that you will be able to track certain statistics that I will ask for in your final report. You may also want to add functionality to your search to not expand nodes that you have already expanded.
When each of these 3 searches completes a search, you should return a list of the moves required to solve the puzzle. In addition, a final report (written in html) should also include for each search performed:
Heuristic Evaluation Function:
For your A* search, you should write the following two heuristic evaluation functions. Each function accepts a board as an argument and returns a number greater than or equal to zero. In both of these cases the better state is the one with the smallest value (the goal state has a value of 0).
For example: ((1 2 3) (4 5 6) (8 7 X)) would score 2 since both 7 and 8 are not in their proper locations.
For example (with Manhattan Distance): ((1 2 3) (5 6 X) (8 4 7)) would score an 8. 4 is one horizontal unit and one vertical unit away from its proper location thus receiving a score of 2. Each tile is scored likewise and the total is summed.
Testing:
Test each search method and heuristic with the following states and create a table of the performance of each strategy. Include this table as a part of the comments at the top of your program. Construct at least one additional medium problem with a solution of over 20 steps and include that with your results as well. Keep in mind, some of the searches may never solve the puzzle and thus if you don't have a solution in under an hour (probably a lot less time for most). Then you are not likely to find a solution with that search method. Also note, there are possible states for which there is no solution.
State1 '((1 2 3) (7 X 5) (8 4 6)) - Easy
State2 '((2 1 3) (5 4 X) (7 8 6)) - Harder
Challenge Problems:
You should turn in file called 8puzzle.ss and 8puzzle.html to your turnin directory.