You are on page 1of 6

60003180058 AI VARUN VORA

LAB

Experiment No: 7
Aim: To implement 8 –puzzle problem using A* search technique.

Theory:

A* uses a best-first search and finds the least-cost path from a given initial node to one goal node
(out of one or more possible goals).

It uses a distance-plus-cost heuristic function (usually denoted f(x)) to determine the order in which
the search visits nodes in the tree. The distance-plus-cost heuristic is a sum of two functions:

The path-cost function, which is the cost from the starting node to the current node (usually
denoted g(x)) and an admissible "heuristic estimate" of the distance to the goal (usually denoted
h(x)).

The h(x) part of the f(x) function must be an admissible heuristic; that is, it must not overestimate
the distance to the goal. Thus, for an application like routing, h(x) might represent the straight- line
distance to the goal, since that is physically the smallest possible distance between any two points
or nodes.

Algorithm:
Pseudo code:
function A*(start,goal)
closedset := the empty set // The set of nodes already evaluated.
openset := {start} // The set of tentative nodes to be evaluated, initially containing the start
node
came_from := the empty map // The map of navigated nodes.

g_score[start] := 0 // Cost from start along best known path.


h_score[start] := heuristic_cost_estimate(start, goal)
f_score[start] := h_score[start] // Estimated total cost from start to goal through y.

while openset is not empty


x := the node in openset having the lowest f_score[] value
if x = goal
return reconstruct_path(came_from, came_from[goal])

remove x from openset


add x to closedset
foreach y in neighbor_nodes(x)
if y in closedset
continue
tentative_g_score := g_score[x] + dist_between(x,y)

1
60003180058 AI VARUN VORA
LAB

if y not in openset
add y to openset
tentative_is_better := true
else if tentative_g_score < g_score[y]
tentative_is_better := true
else
tentative_is_better := false

if tentative_is_better = true
came_from[y] := x
g_score[y] := tentative_g_score
h_score[y] := heuristic_cost_estimate(y, goal)
f_score[y] := g_score[y] + h_score[y]

return failure

function reconstruct_path(came_from, current_node)


if came_from[current_node] is set
p = reconstruct_path(came_from, came_from[current_node])
return (p + current_node)
else
return current_node

Problem Definition:

Code:

import numpy as np

from queue import PriorityQueue

goal_state = [[7, 2, 4], [5, 0, 6], [8, 3, 1]]

2
60003180058 AI VARUN VORA
LAB
start_state = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]

class State:

def init (self, data, level, fval):

self.data = data

self.level = level

self.fval = fval

def copy(self, root):

temp = []

for i in root:

t = []

for j in i:

t.append(j)

temp.append(t)

return temp

def generate_child(self):

x, y = self.find(self.data, 0)

val_list = [[x, y-1], [x, y+1], [x-1, y], [x+1, y]]

children = []

for i in val_list:

child = self.shuffle(self.data, x, y, i[0], i[1])

3
60003180058 AI VARUN VORA
LAB
if child is not None:

child_node = State(child, self.level+1, 0)

children.append(child_node)

return children

def shuffle(self, puz, x1, y1, x2, y2):

if x2 >= 0 and x2 < len(self.data) and y2 >= 0 and y2 < len(self.data):

temp_puz = []

temp_puz = self.copy(puz)

temp = temp_puz[x2][y2]

temp_puz[x2][y2] = temp_puz[x1][y1]

temp_puz[x1][y1] = temp

return temp_puz

else:

return None

def find(self, puz, x):

for i in range(0, len(self.data)):

for j in range(0, len(self.data)):

if puz[i][j] == x:

return i, j

class A_Star:

4
60003180058 AI VARUN VORA
LAB

def init (self):

self.open = []

self.closed = []

def get_h_cost(self, mat_1, mat_2):

state_1 = np.asmatrix(mat_1)

state_2 = np.asmatrix(mat_2)

return np.sqrt(np.sum(abs((state_1 - state_2)**2)))

def find_goal(self, start_state, goal_state):

curr_level = 0

start = State(start_state, 0, 0)

start.fval = self.get_h_cost(start_state, goal_state)+curr_level

curr_level += 1

self.open.append(start)

print("\n\n")

while True:

cur = self.open[0]

print("")

print(" | ")

print(" | ")

print("\n")

for i in cur.data:

for j in i:

print(j, end=" ")

5
60003180058 AI VARUN VORA
LAB
print("")

if(self.get_h_cost(cur.data, goal_state) == 0):

break

for i in cur.generate_child():

i.fval = self.get_h_cost(i.data, goal_state)+curr_level

curr_level += 1

self.open.append(i)

self.closed.append(cur)

del self.open[0]

self.open.sort(key=lambda x: x.fval, reverse=False)

obj = A_Star()

obj.find_goal(start_state, goal_state)

Output:

You might also like