mgabilo / eightpuzzle-iterative-deepening

Eight puzzle solver using iterative deepening

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

eightpuzzle-iterative-deepening

This is an eight puzzle solver using iterative deepening depth-first search (IDDFS). Actually, it solves an n by m puzzle, not only an eight puzzle.

For example, the image below shows example start and goal states for a 3 x 4 puzzle instance:

alt text

In the input file, these states are described as follows.

(3 4)
((7 * 2) (9 6 4) (13 14 9) (0 11 10))
((7 6 2) (9 4 9) (13 14 10) (* 0 11))

The object of the game is to transform the start state into the goal state. The * symbol can be swapped with a number directly to its left, its right, or above or below it. In this instance, the player has 3 possible choices for the first move, because the * symbol is already at the top of the grid: it can only move to the left, right, or down. For example, if the * symbol moves left, then the 7 and the * will be swapped.

IDDFS is a state-space search algorithm which explores many possible paths from the starting state. It finds the shortest path to the goal if a path exists, since it discovers new states in the same order as a breadth-first search. However, it uses much less memory than breadth-first search.

A series of depth-first searches are initiated on the starting state, with increasing limits on the maximum depth each search can explore.

The first DFS can only explore a maximum depth of 1 (i.e., 1 move away from the start state), the next can explore a maximum depth of 2, the next a maximum depth of 3, and so on.

Each time the DFS reaches its maximum depth it compares the current state to the goal.

Algorithm walk-through

Below I'll demonstrate the first few steps the algorithm takes given the start state above.

Initially, it tries all possible sequences of one move that start from the starting state. That is, it moves left from the starting position, it moves right from the starting position, and it moves down from the starting position. These initial 3 generated states are shown below.

alt text

The program outputs the following to indicate it has generated these states.

(1 DOWN)
(CHECK)
(1 LEFT)
(CHECK)
(1 RIGHT)
(CHECK)

The "1" in each move indicates the distance (number of moves) away from the start state, i.e., the depth.

The "CHECK" indicates that the maximum depth of exploration has been reached, and the program will check if the current state is equal to the goal state.

Following this, IDDFS tries all possible sequences of two moves (depth of two) from the starting state. The program outputs the following.

(1 DOWN)
(2 UP)
(CHECK)
(2 DOWN)
(CHECK)
(2 LEFT)
(CHECK)
(2 RIGHT)
(CHECK)
(1 LEFT)
(2 DOWN)
(CHECK)
(2 RIGHT)
(CHECK)
(1 RIGHT)
(2 DOWN)
(CHECK)
(2 LEFT)
(CHECK)

The * moves down (state 1), then back up (state 2), where it has reached a depth of 2 and checks if this is a solution. Since the depth-first search cannot explore any other states from the current state, it backs up to the previous state (state 1). From here, it moves down again (state 3), where it has reached the maximum depth, and checks if this state is a solution. Once again it backs up to (state 2), at which point it goes left (state 4), reaches its maximum depth, and checks if the state is a solution.

Once the algorithm has exhausted all paths that are two moves away from the start state, it initiates a new depth first search with a maximum depth of 3.

The algorithm carries on like this until it has generated the goal state, or if there is no solution, it will carry on forever (until the maximum memory on the stack is exceeded).

Running the script

To run the script with the provided input file "in1" type:

./eightpuzzle.py in2

To run the script with the provided input file "in2" type:

./eightpuzzle.py in2

License

This project is licensed under the MIT License (see the LICENSE file for details).

Authors

About

Eight puzzle solver using iterative deepening

License:MIT License


Languages

Language:Python 100.0%