The Eight Puzzle: By: Robert Goldstein
The Eight Puzzle: By: Robert Goldstein
Table of Contents
0. Test System___________________________________________________ 4
_ 1. Introduction___________________________________________________ _ 2. Breadth First Search (BFS) ________________________________________ 3. A* Search _____________________________________________________ 3.1 A* Search using the Manhattan Distance Heuristic__________________ 3.2 A* Search using the Tiles Out of Place Heuristic__ _________________ 3.3 A* Search using Greedy Search________________________ _________ 4. Language of Choice for Project________________________________ _____ 5. How is the Eight Puzzle represented in the program? _____________________ 6. How was the BFS was implemented? _________________________________ 7. How was the A* Search implemented? ________________________________ 7.1 How was the Manhattan Distance Heuristic implemented?____________ 7.2 How was the Tiles Out of Place Heuristic implemented? _____________ 7.3 How was the A* Search using Greedy Search implemented?___________ 8. How Random is Randomize Tiles? ___________________________________ 9. Results from Searches_________________________________________ ____ 10. Known Bugs_________________________________________________ ___
Page 2 of 15
4 5 6 6 7 7 7 8 8 9 1 2 1 3 1 3 1 3 1 4 1 4 1 4 1 4 1 4 1 5 1 5
10.1 Display of Breadth First Search (BFS)_____________ _______________ 10.2 Depth Problem With Respect To Breath First Search (BFS)___________ 10.3 Depth Problem With Respect To A* Searches_____ _________________ 10.4 Applet Usage Problem With Respect To Microsoft Internet Explorer__ 10.5 Applet Usage Problem With Respect To Netscape Navigator________
Page 3 of 15
0. Test System
I used my home PC in order to do the testing for the project. The specifications of interest for it are: Pentium 150 MHz 64 Megabytes Ram 10.4 Gig HDD 4 Megabytes VRAM Video card
1. Introduction
The Eight puzzle is the largest puzzle of its type that can be completely solved. It is simple, and yet obeys a combinatorially large problem space of 9!/2 states. The N*N extension of the 8puzzle is NP-hard. It is a game in which there are eight 1 x 1 tiles arranged on a 3 x 3 square so that there is one 1 x 1 uncovered area on the square. The tiles are labeled 1 through 8, and initially they are arranged in some random order. A tile with a face adjacent to the uncovered area can be transferred to the uncovered area in a single move. The goal of the puzzle is to place them in order, as in
+---+---+---+ | 1 | 2 | 3 | +---+---+---+ | 4 | 5 | 6 | +---+---+---+ | 7 | 8 | | +---+---+---+ Figure 1. Goal State of Eight Puzzle
Therefore, the following formulation makes up the Eight-Puzzle problem: States: a state description specifies the location of each of the eight tiles in one of the nine squares. For efficiency, it is useful to include the location of the blank. Operators: blank moves left, right, up, or down. Goal test: state matches the goal configuration (as seen above in Figure 1). Path cost: each step costs 1, so the path cost is just the length of the path. For this problem, I will demonstration how to solve the eight puzzle using three methods: (1) Breadth First Search (BFS), (2) A* Search using the Manhattan Distance heuristic, and (3) A* Search using the number of tiles out of place heuristic.
Page 4 of 15
This is the maximum number, but the solution could be found at any point on level d, therefore in the best case, this number would be smaller. As a result, the time and memory complexity are both a ratio of O(bd).
Page 5 of 15
3. A* Search
The A* Search is a part of a class of algorithms called Best-First Search. This class of searching uses an evaluation function in order to determine the desirability (or lack thereof) of expanding a node. The name of this class of search algorithms is sort of misleading because if the search was able to directly pick the best node first, it would be a straight marsh to the Goal State instead of a search. However, what actually happens is that it chooses the node that appears to be the best node according to the evaluation function. The A* search makes its determination via the summation of two sub-functions in order to produce the evaluation function. There sub-functions provide the total path cost so far, g(n), and an admissible heuristic1, h(n), which is used to minimize the estimated path cost to the goal state. Therefore, this search method is optimal and complete. This means that it will in the end, always find a solution and it will be the best one.
3.1
Using the Manhattan distance heuristic with the A* search is good algorithm use if you would like to keep the keep the search from retreating in depth too much. This algorithm uses the sum of the distances of the tiles from their goal positions. Because tile cannot move along diagonals, the distance counted will be the sum of the horizontal and vertical distances. This heuristic is admissible, because any move can only move one tile one step closer to the goal. Below is the result of this heuristic when tiles 1 to 8 are in the following Start State:
An admissible heuristic never overestimates the cost to reach the Goal State. This optimism of this transfers through to the f function as well. If h is admissible, f(n) never overestimates the actual cost of the best solution through n. Page 6 of 15
+---+---+---+ | 5 | 4 | | +---+---+---+ | 6 | 1 | 8 | +---+---+---+ | 7 | 3 | 2 | +---+---+---+ Manhattan Distance = 2 + 3 + 3 + 2 + 4 + 2 + 0 + 2 = 18 Figure 4. Manhattan Distance Heuristic
3.2
The Tiles Out of Place heuristic is a fine algorithm to use with A* search. The heuristic involves summing the number of tiles that are not in their correct location for the Goal State. This heuristic is admissible, because it is clear that any tile that is out of place must be moved at least once. Below is the result of this heuristic when tiles 1 to 8 are in the following Start State:
+---+---+---+ | 5 | 4 | | +---+---+---+ | 6 | 1 | 8 | +---+---+---+ | 7 | 3 | 2 | +---+---+---+ Tiles Out of Place = 1 + 1 + 1 + 1 + 1 + 0 + 1 + 1 = 7 Figure 5. Tiles Out of Place Heuristic
3.3
The Greedy Search is one of the simplest algorithms to use. This is because there is no additional heuristic to aid you besides the total path cost so far, g(n). As a result, many nodes that should not be expanded are and additional time is needed in order to find the correct solution.
Page 7 of 15
Page 8 of 15
Let cCount = the number of nodes that possibly at current depth Let fCount = the number of nodes that possibly at current depth + 1, i.e. future Let nodes = the number of nodes evaluated Let depth = the expected depth for finding the current depth looking at
1. Remove a node state from queue. 2. If cCount = 0, then Increment depth by 1. Set cCount = fCount and fCount = 0. 3. If all tiles are in correct location, add 1 to nodes and Done! Otherwise continue to step 3. 4. Create all valid future nodes for that state and append to queue. Increment fCount by 1 for valid future nodes. 5. Reduce cCount by 1. 6. Increment nodes by 1. 7. Go to step 1. Figure 7. Procedures for Implemented BFS
Page 9 of 15
Let nodes = A vector that contains all open children nodes from the best node sorted by least total cost Let best = The first node in nodes vector. The node under consideration for future expansion. Let bestTotal = the best total of all nodes evaluated. Let open = A hash-table that contains all open nodes on the Search Tree. It is used to make sure that there is no duplicates saved and only keeping the best around. Let closed = A hash-table that contains the nodes that have already been expanded on the Search Tree. It is used insuring that the correct path is not over looked. Let theNode = A temporary variable that holds the node under examination. Let children = A vector that contains children of best, temporarily, until added to the nodes vector.
1. Set best = the first node in nodes vector. 2. If bests heuristic total != bestTotal, then set bestTotal = bests heuristic total 3. If best is the Goal State, then Done! Recurse through best back to the Start State and store all values in Vector (path through the Search Tree to reach the Goal State), so it can be shown graphically to user by manipulating the tiles. 4. Clean up past children from last run. 5. Generate bests children. 6. Produce bests childrens g(n), 1 + bests cost. 7. Increment expanded depth by 1. 8. Examine of bests children individually using the following steps: a. Obtain next child state, and clear out closedNode (temp var), openNode (temp var), theNode. b. If the child state is not in the closed table (store in closedNode), then try to obtain it from the open table (store in openNode). c. If openNode isnt null, then theNode = openNode, otherwise theNode = closedNode. d. Determine whether theNode isnt null. i. If theNode isnt null 1. Check to see if theNodes costs are > the bests childrens costs. If they are: a. If closedNode isnt null i. Put theNode into the open table with the child state as the key. ii. Remove the value in the closed table with the child state as the key. iii. Set theNodes costs = the childs costs iv. Set theNodes total = the theNodes new cost + theNodes distance. v. Set theNodes parent to the best node. vi. Add theNode to the children vector.
Page 10 of 15
If closedNode is null Set theNode = a new node for the child state, with the parent as the best node, costs as the childs costs, and the distance as theNodes distance. ii. Put theNode into the open table with the child state as the key. iii. Add theNode to the children vector. ii. If theNode is null 1. Get the estimate for the child state. 2. Create a new node for the child state, with the parent as the best node, costs as the childs costs, and the distance as the estimate. 3. Put the new node inter the open table with the child state as the key. 4. Increment the evaluated nodes 5. Add the new node to the children vector. e. Loop until all of bests children have been examined. 9. Remove from the open table the value where the best nodes state is the key. 10. Add to the closed table the best nodes state using the best node as a key. 11. Remove the first element in nodes vector. 12. Add children nodes to nodes vector in order of lowest cost 13. Go to step 1, until there are no more nodes in the nodes vector. 14. Return null, signifying a failure, i.e. no solution found. Figure 8. Procedures for Implemented A* Search
b. i.
Page 11 of 15
Page 12 of 15
7.2
The Tiles Out of Place Heuristic, talked about in section , is implemented via a for loop that checks each element in the array to determine whether or not the number in each element is in the correct position. Each time the number is incorrect the heuristic result is incremented by one, starting at zero. The code fragment for this heuristic is:
for(int offby=0; offby < dimensions of puzzle; offby++) { if( sequence to check[offby] != offby ) { increment heuristic result by one; } } return heuristic result; Figure 10. Pseudo code fragment for Tiles Out of Place Heuristic
Page 13 of 15
The problem with the BFS display is that the search doesnt graphically display each movement of the tiles from the Start State to the Goal State after completing the search, but rather redisplays the Goal State to the user. This is a result of not attaching the parent states to each child state throughout the search before putting the nodes on the queue to be later examined, something I did not forget to do in the A* searches. This bug will be corrected in the next version of the program.
10.2 (BFS)
While doing experimentation on the BFS algorithm in the program, I noticed that a depth greater 13 was beyond a reasonable amount of time to wait for an answer2. This is because the Eight Puzzle has on average a branching factor of around 2.7 3. Therefore at depth 14, the number of nodes that needs to be evaluated is approximately 1,094,190 compared to depth 13 which is approximately 405,256 nodes. This means that it would need significantly more time to complete the evaluation of these depths up to the point of never.
10.3
2
A reasonable amount of time is approximately of a hour. This was sufficient time for me to get bored playing other games or watching television, waiting for an answer. However, according to the ACM Regional Coding Contest, I recently attended, the reasonable amount of time was significantly lower. It actually was two minutes. 1 location with all four valid moves, 4 with three valid moves, and 4 with two valid moves = ((1*4)+(4*3)+(4*2))/ 9 spots = 2.6666 ~ 2.7 Page 14 of 15
The A* searches also have problems with large depths. However, when using the Randomize Tiles function, the main problem that was truly noticed was between depth 30 to depth 50. This problem was the ability to display the moved taken to trace back the path to reach the Goal State. The number that continued to pop up as 18,144 nodes. A conclusion drawn from this is that there is a limitation to amount to information that was able to be stored in the solution vector, which resulted in this problem. Further research into the matter is underway.
Page 15 of 15