Вы находитесь на странице: 1из 22

Group # 2

Group Members:

- Jose Vizueta

Manuel Sanchez

The 8-puzzle game consist of 8 differently numbered chips are fitted into 9
spaces on a grid. One space is left blank so that tiles can be moved around
to form different patterns.
The goal is to find a series of moves of tiles that places the board in a goal
configuration. This is a common game that most of us played as children.

This game has some interesting aspects that have made it an excellent
applicant in problem solving, the game states are easy to represent, it has
simple to define heuristics and a manageable state space.
The use of Heuristics is one of the best alternatives in problems where the
state space grows exponentially or factorially, and search techniques such as
depth-first or breadth-first may fail to find the solution or take too long time.
Guiding the search along the most promising path through the space and
avoiding unpromising states and their descendants from consideration, a
heuristic algorithm can find quickly an acceptable solution.

In this project we used a Heuristic Search algorithm like Best First Search to
reach the Goal State or solution from any Initial State of the board of the 8Puzzle game, using two different Heuristics: Manhattan Distances and
Misplaced Chips.

Game states
integer array
where each
in the same

can be represented using a simple 3 3
composed of numbers between 1 and 8,
zero as the empty space in the board,
element represents the corresponding chip

[ ]

0 1 2
state= 3 4 5
6 7 8

Details of concepts for developer the application

Initial State: any state configuration possible, with chips in any position.
Goal State: state that specify a particular configuration to be achieved.
State space: The state space is represented by a graph where each node

represent a board configuration and its children the resulting set of boards
after move the empty space on 4 possible directions ( up, down , left, right ).
Set of operators and Restrictions: To represent the legal moves in our

problem we define rules that transform our 3x3 matrix (representing a state)
in another 3x3.
In the physical puzzle moves are made by moving any of the (1-8) tiles, but
for our implementation its better to think in terms of moving the empty
space. This simplifies the definition of a legal move based only in this single
empty space.
In order to apply a move, we must make sure that it does not move the blank
tag off the board.
The legal moves are:






A'(i-1,j)=0; A'(i,j)= A(i1,j)

A'(i,j+1)=0; A'(i,j)=
A'(i,j-1)=0; A'(i,j)=
A'(i+1,j)=0; A'(i,j)=

Move chip to up
Move chip to
Move chip to left
Move chip to

Meta Check: This function checks if the state corresponds to the final state

given by the configuration of the chips in the order of the target node.
Cost function: it takes into account the cost of operations and the cost of the

path is the sum of the costs of the operations used to reach the goal.
Heuristic Function
The 8-puzzle game is rich enough to provide a number of interesting
In our implementation we used two heuristics:

Misplaced Chips: The simplest heuristic counts the chips out of place
in each state when compared with another state.

Manhattan Distance: This heuristic sum all the distances by which

each misplaced chip must be moved to reach its position in the goal

Search Algorithm
Best First Search
Best First is a Heuristic Search algorithm, it follows one path at a time, but it
can change the search path accordingly, based on the best promising path
ahead than the current path.
This algorithm uses two lists of nodes and a heuristic function that estimates
the merits of each node to be explored:
1. Open list it contains the generated nodes. The heuristic function has
been applied to them; but, they have not been tested yet; that is, their
successors have not been generated yet. Open can be considered as a
priority queue, in which the elements are ordered according to some
heuristic estimate of their closeness to a goal
2. Close list It record states already visited that were not the goal node.

The pseudocode for the function best_first_search appears below.

At each iteration, best_first_search removes the first element from the open
list. If it meets the goal conditions, the algorithm returns the solution path
that led to the goal.
If the first element on open is not a goal, the algorithm generates its
If a child state is not on open or closed best_first_search evaluates that state
and then is added to the open list, and the open list is sorted according to
the heuristic values of those states. This brings the best states to the front
of open.
If a child state is already on open or closed, the algorithm checks to make
sure that the state records the shorter of the two partial solution paths
avoiding duplicate paths. By updating the path history of nodes on open and
closed when they are rediscovered, the algorithm will find a shortest path to
a goal.

function best_first_search;
open := [Start];
closed := [ ];
while open [ ] do
% states
remove the leftmost state from open, call it X;
if X = goal then return the path from Start to X
else begin
generate children of X;
for each child of X do
the child is not on open or closed:
assign the child a heuristic value;
add the child to open
the child is already on open:
if the child was reached by a shorter path
then give the state on open the shorter
the child is already on closed:
if the child was reached by a shorter path then
remove the state from closed;
add the child to open
% case
put X on closed;
re-order states on open by heuristic merit (best leftmost)
return FAIL % open is empty

The Application that will be developed as a visual representation of the
procedure followed by the Best First Search with an of the heuristic selected
(Misplaced Chips and Manhattan distances ) , this show a tree that represent
the solution path from the goal state to the final state defined by the user .
We used the Java programming language because its an object-oriented
language and it facilitates the representation of the board, tree, nodes and
its relationships.

We used the JUNG library to graph trees, JUNG the Java Universal
Network/Graph Framework--is a software library that provides a common and
extendible language for the modeling, analysis, and visualization of data that
can be represented as a graph or network. It is written in Java, which allows
JUNG-based applications to make use of the extensive built-in capabilities of
the Java API, as well as those of other existing third-party Java libraries.

The user will have access to a simple interface where he can select the initial
and final state boards of a few predefined configurations or manually by user
input, and then select the heuristic utilized in the search.

Initial State

Goal State

In each interaction 2, 3 or 4 below possible states are always generated,

depending of the moves the empty space could do: up, down, left and right.

The Best First Search use an evaluation function f(), where:

f ()=g ()+ h()

Function g is the cost determined to reach the current state from the
initial state. In our case the step cost: one movement of the chip.
Function h is the estimated cost to reach the objective state from the
current state. This is determined by the heuristic function.

The result path obtained should be as below:

Use Case Diagram

Class Diagrams

Sequence Diagrams
Best First Search - Search Function

Heuristic Function Get Manhattan Distance Value

Heuristic Function Get Misplaced Chips Value

TreeFrame draw Tree

EightPuzzleFrame Generate Solution Path

Board Can Move Gap

Tests were realized with three different initial configurations of the board,
representing 3 difficulties according to the degree to which the chips are
misplaced and the same Goal state:




2 3

easy=[ 5 8 ]

2 4 3
1 6 8
7 5

Goal State:

1 2 3
4 5 6
7 8

2 4

hard=[ 15 ]

The tests were realized with each of the two heuristics (Manhattan,
Misplaced) a nd the evaluation was performed based on the time in which
the solution is found and the number of states were reached and
evaluated with the heuristics.

Test 1 - Easy

Manhattan Distance
3.70 secs

Misplaced Chips
3.85 secs

States evaluated

Search Path

Manhattan Distance

Misplaced Chips



Test 2 - Medium

States evaluated

Manhattan Distance
4.01 secs

Search Path

Manhattan Distance

Misplaced Chips
4.11 secs



Test 3 - Hard

States evaluated

Manhattan Distance
5.63 secs

Misplaced Chips
11.91 secs

The Best First Search proved to be a remarkable heuristic search
method, more than follow the most promising path, it keeps a record of
its history, allowing it to step back in case its path begins to be

However the efficiency of this depends mostly on the heuristic used,

and how representative it is based on reaching the goal state.
As observed in Test 1, the Heuristics of Manhattan and Misplaced Chips
showed similar results as the solution was easily achievable, however
with the increasing difficulty, the Misplaced Heuristic showed quite
inferior and not as efficient as the Manhattan Heuristic, especially in
Test 3 having to evaluate 10 times more states than the other heuristic
and taking almost twice as long time.

Content of Archives of Artificial Intelligence by Enrique Pelez Ph.D.

George F. Luger, Artificial Intelligence Structures and Strategies for

Complex Problem Solving, Sixth Edition, 2010, Addison Wesley.

Russel Stuart, Norbig Peter, Artificial Intelligence A Modern Approach,

Third Edition

N. Nillson. Inteligencia Articial: una nueva sntesis. McGraw-Hill, 2001.

www.fic.udc.es/files/asignaturas/18IA/MemoriaPractica.pdf - Cabrero &