You are on page 1of 14

COMSATS University, Islamabad

Department of Computer Science

Student Name: Ayesha Siddiqa Reg. SP22-BSE-009


Subject: Artificial Intelligence Class/Section: BSE-5A
Date: 3/25/2023 Lab # 7

Activity # 01:
Import math

class Node:
def __init__(self, state, parent, actions, totalCost, heuristic):
self.state = state
self.parent = parent
self.actions = actions
self.totalCost = totalCost
self.heuristic = heuristic

def findMin(frontier):
minV = math.inf
node = None
for i in frontier:
if minV > frontier[i][1]:
minV = frontier[i][1]
node = i
return node

def actionSequence(graph, initialState, goalState):


solution = [goalState]
currentParent = graph[goalState].parent
while currentParent is not None:
solution.append(currentParent)
currentParent = graph[currentParent].parent
solution.reverse()
return solution
def Astar():
initialState = 'A'
goalState = 'Y'

graph = {
'A': Node('A', None, [('F', 1)], 0, (0, 0)),
'B': Node('B', None, [('G', 1), ('C', 1)], 0, (2, 0)),
'C': Node('C', None, [('B', 1), ('D', 1)], 0, (3, 0)),
'D': Node('D', None, [('C', 1), ('E', 1)], 0, (4, 0)),
'E': Node('E', None, [('D', 1)], 0, (5, 0)),
'F': Node('F', None, [('A', 1), ('H', 1)], 0, (0, 1)),
'G': Node('G', None, [('B', 1), ('J', 1)], 0, (2, 1)),
'H': Node('H', None, [('F', 1), ('I', 1), ('M', 1)], 0, (0, 2)),
'I': Node('I', None, [('H', 1), ('J', 1), ('N', 1)], 0, (1, 2)),
'J': Node('J', None, [('G', 1), ('I', 1)], 0, (2, 2)),
'K': Node('K', None, [('L', 1), ('P', 1)], 0, (4, 2)),
'L': Node('L', None, [('K', 1), ('Q', 1)], 0, (5, 2)),
'M': Node('M', None, [('H', 1), ('N', 1), ('R', 1)], 0, (0, 3)),
'N': Node('N', None, [('I', 1), ('M', 1), ('S', 1)], 0, (1, 3)),
'O': Node('O', None, [('P', 1), ('U', 1)], 0, (3, 3)),
'P': Node('P', None, [('O', 1), ('Q', 1)], 0, (4, 3)),
'Q': Node('Q', None, [('L', 1), ('P', 1), ('V', 1)], 0, (5, 3)),
'R': Node('R', None, [('N', 1), ('S', 1)], 0, (0, 4)),
'S': Node('S', None, [('N', 1), ('R', 1), ('T', 1)], 0, (1, 4)),
'T': Node('T', None, [('S', 1), ('U', 1), ('W', 1)], 0, (2, 4)),
'U': Node('U', None, [('O', 1), ('T', 1)], 0, (3, 4)),
'V': Node('V', None, [('Q', 1), ('Y', 1)], 0, (5, 4)),
'W': Node('W', None, [('T', 1)], 0, (2, 5)),
'X': Node('X', None, [('Y', 1)], 0, (4, 5)),
'Y': Node('Y', None, [('V', 1), ('X', 1)], 0, (5, 5)),
}

frontier = {}
heuristicCost = math.sqrt((graph[goalState].heuristic[0] - graph[initialState].heuristic[0]) ** 2 +
(graph[goalState].heuristic[1] - graph[initialState].heuristic[1]) ** 2)
frontier[initialState] = (None, heuristicCost)
explored = {}

while len(frontier) != 0:
currentNode = findMin(frontier)
del frontier[currentNode]

if graph[currentNode].state == goalState:
return actionSequence(graph, initialState, goalState)

heuristicCost = math.sqrt((graph[goalState].heuristic[0] - graph[currentNode].heuristic[0]) ** 2 +


(graph[goalState].heuristic[1] - graph[currentNode].heuristic[1]) ** 2)
currentCost = graph[currentNode].totalCost
explored[currentNode] = (graph[currentNode].parent, heuristicCost + currentCost)

for child, cost in graph[currentNode].actions:


currentCost = cost + graph[currentNode].totalCost
heuristicCost = math.sqrt((graph[goalState].heuristic[0] - graph[child].heuristic[0]) ** 2 +
(graph[goalState].heuristic[1] - graph[child].heuristic[1]) ** 2)
if child in explored:
if graph[child].parent == currentNode or child == initialState or \
explored[child][1] <= currentCost + heuristicCost:
continue

if child not in frontier:


graph[child].parent = currentNode
graph[child].totalCost = currentCost
frontier[child] = (graph[child].parent, currentCost + heuristicCost)
else:
if frontier[child][1] < currentCost + heuristicCost:
graph[child].parent = frontier[child][0]
graph[child].totalCost = frontier[child][1] - heuristicCost
else:
frontier[child] = (currentNode, currentCost + heuristicCost)
graph[child].parent = frontier[child][0]
graph[child].totalCost = currentCost

solution = Astar()
print(solution)

Graded# 01:
 Using Euclidean Distance as Heuristic Function:
import math

class Node:
def __init__(self, state, parent, actions, totalCost, heuristic):
self.state = state
self.parent = parent
self.actions = actions
self.totalCost = totalCost
self.heuristic = heuristic

def findMin(frontier):
minV = math.inf
node = None
for key in frontier:
if minV > frontier[key][1]:
minV = frontier[key][1]
node = key
return node

def actionSequence(graph, initialState, goalState):


solution = [goalState]
currentParent = graph[goalState].parent
while currentParent is not None:
solution.append(currentParent)
currentParent = graph[currentParent].parent
solution.reverse()
return solution

def Astar():
initialState = '00'
goalState = '99'

graph = {
'00': Node('00', None, [('01', 1), ('10', 1)], 0, (0, 0)),
'10': Node('10', None, [('11', 1), ('20', 1), ('00', 1)], 0, (1, 0)),
'20': Node('20', None, [('21', 1), ('10', 1), ('30', 1)], 0, (2, 0)),
'30': Node('30', None, [('20', 1), ('40', 1)], 0, (3, 0)),
'40': Node('40', None, [('30', 1), ('50', 1)], 0, (4, 0)),
'50': Node('50', None, [('40', 1)], 0, (5, 0)),
'01': Node('01', None, [('00', 1), ('02', 1), ('11', 1)], 0, (0, 1)),
'11': Node('11', None, [('01', 1), ('21', 1), ('10', 1)], 0, (1, 1)),
'21': Node('21', None, [('11', 1), ('22', 1), ('20', 1)], 0, (2, 1)),
'61': Node('61', None, [('62', 1)], 0, (6, 1)),
'81': Node('81', None, [('82', 1), ('91', 1), ('80', 1)], 0, (8, 1)),
'91': Node('91', None, [('81', 1)], 0, (9, 1)),
'02': Node('02', None, [('03', 1), ('01', 1)], 0, (0, 2)),
'22': Node('22', None, [('21', 1), ('32', 1)], 0, (2, 2)),
'32': Node('32', None, [('22', 1), ('33', 1)],0, (3, 2)),
'52': Node('52', None, [('53', 1), ('62', 1)], 0, (5, 2)),
'62': Node('62', None, [('52', 1), ('61', 1)], 0, (6, 2)),
'82': Node('82', None, [('81', 1)], 0, (8, 2)),
'03': Node('03', None, [('02', 1), ('13', 1)], 0, (0, 3)),
'13': Node('13', None, [('03', 1), ('14', 1)], 0, (1, 3)),
'33': Node('33', None, [('32', 1),('43', 1)], 0, (3, 3)),
'43': Node('43', None, [('44', 1),('53', 1),('33', 1)], 0, (4, 3)),
'53': Node('53', None, [('52', 1), ('43', 1)], 0, (5, 3)),
'14': Node('14', None, [('13', 1), ('15', 1),('24', 1)], 0, (1, 4)),
'24': Node('24', None, [('14', 1),('25', 1)], 0, (2, 4)),
'44': Node('44', None, [('43', 1), ('45', 1)], 0, (4, 4)),
'74': Node('74', None, [('75', 1), ('84', 1)], 0, (7, 4)),
'84': Node('84', None, [('74', 1), ('85', 1)], 0, (8, 4)),
'05': Node('05', None, [('06', 1), ('15', 1)], 0, (0, 5)),
'15': Node('15', None, [('05', 1), ('14', 1),('25', 1)], 0, (1, 5)),
'25': Node('25', None, [('35', 1),('24', 1)], 0, (2, 5)),
'35': Node('35', None, [('25', 1), ('36', 1), ('45', 1)], 0, (3, 5)),
'45': Node('45', None, [('35', 1), ('44', 1), ('46', 1)], 0, (4, 5)),
'75': Node('75', None, [('74', 1), ('76', 1),('85', 1)], 0, (7, 5)),
'85': Node('85', None, [('84', 1), ('75', 1),('86', 1),('95', 1)], 0, (8, 5)),
'95': Node('95', None, [('96', 1),('85', 1)], 0, (9, 5)),
'06': Node('06', None, [('05', 1), ('07', 1)], 0, (0, 6)),
'36': Node('36', None, [('37', 1), ('46', 1),('35', 1)], 0, (3, 6)),
'46': Node('46', None, [('45', 1), ('36', 1),('47', 1)], 0, (4, 6)),
'66': Node('66', None, [('67', 1), ('76', 1)], 0, (6, 6)),
'76': Node('76', None, [('75', 1), ('77', 1),('66', 1),('86', 1)], 0, (7, 6)),
'86': Node('86', None, [('87', 1), ('76', 1),('85', 1),('96', 1)], 0, (8, 6)),
'96': Node('96', None, [('95', 1), ('97', 1),('86', 1)], 0, (9, 6)),
'07': Node('07', None, [('06', 1), ('08', 1)], 0, (0, 7)),
'37': Node('37', None, [('36', 1), ('47', 1)], 0, (3, 7)),
'47': Node('47', None, [('37', 1), ('46', 1),('57', 1)], 0, (4, 7)),
'57': Node('57', None, [('47', 1), ('67', 1)], 0, (5, 7)),
'67': Node('67', None, [('66', 1), ('57', 1),('77', 1),('68', 1)], 0, (6, 7)),
'77': Node('77', None, [('78', 1), ('76', 1),('67', 1),('87', 1)], 0, (7, 7)),
'87': Node('87', None, [('86', 1), ('77', 1),('88', 1),('97', 1)], 0, (8, 7)),
'97': Node('97', None, [('96', 1), ('87', 1)], 0, (9, 7)),
'08': Node('08', None, [('07', 1), ('09', 1),('18', 1)], 0, (0, 8)),
'18': Node('18', None, [('08', 1), ('19', 1),('28', 1)], 0, (1, 8)),
'28': Node('28', None, [('18', 1)], 0, (2, 8)),
'48': Node('48', None, [('47', 1), ('49', 1)], 0, (4, 8)),
'68': Node('68', None, [('67', 1), ('69', 1),('78', 1)], 0, (6, 8)),
'78': Node('78', None, [('77', 1), ('79', 1),('68', 1),('88', 1)], 0, (7, 8)),
'88': Node('88', None, [('87', 1), ('78', 1),('89', 1)], 0, (8, 8)),
'09': Node('09', None, [('08', 1), ('19', 1)], 0, (0, 9)),
'19': Node('19', None, [('09', 1), ('18', 1)], 0, (1, 9)),
'49': Node('49', None, [('48', 1)], 0, (4, 9)),
'69': Node('69', None, [('79', 1), ('68', 1)], 0, (6, 9)),
'79': Node('79', None, [('89', 1), ('69', 1),('78', 1)], 0, (7, 9)),
'89': Node('89', None, [('79', 1), ('88', 1),('99',1)], 0, (8, 9)),
'99': Node('99', None, [('89', 1)], 0, (9, 9))
}

frontier = {}
heuristicCost = math.sqrt((graph[goalState].heuristic[0] -
graph[initialState].heuristic[0]) ** 2 +
(graph[goalState].heuristic[1] -
graph[initialState].heuristic[1]) ** 2)

frontier[initialState] = (None, heuristicCost)


explored = {}

while len(frontier) != 0:
currentNode = findMin(frontier)
del frontier[currentNode]

if graph[currentNode].state == goalState:
return actionSequence(graph, initialState, goalState)

heuristicCost = math.sqrt((graph[goalState].heuristic[0] -
graph[currentNode].heuristic[0]) ** 2 +
(graph[goalState].heuristic[1] -
graph[currentNode].heuristic[1]) ** 2)
currentCost = graph[currentNode].totalCost
explored[currentNode] = (graph[currentNode].parent, heuristicCost +
currentCost)

for child, cost in graph[currentNode].actions:


currentCost = cost + graph[currentNode].totalCost
heuristicCost = math.sqrt((graph[goalState].heuristic[0] -
graph[child].heuristic[0]) ** 2 + (graph[goalState].heuristic[1] -
graph[child].heuristic[1]) ** 2)
if child in explored:
if graph[child].parent == currentNode or child == initialState or \
explored[child][1] <= currentCost + heuristicCost:
continue

if child not in frontier:


graph[child].parent = currentNode
graph[child].totalCost = currentCost
frontier[child] = (graph[child].parent, currentCost + heuristicCost)
else:
if frontier[child][1] < currentCost + heuristicCost:
graph[child].parent = frontier[child][0]
graph[child].totalCost = frontier[child][1] - heuristicCost
else:
frontier[child] = (currentNode, currentCost + heuristicCost)
graph[child].parent = frontier[child][0]
graph[child].totalCost = currentCost

solution = Astar()
print(solution)

 Using Diagonal Distance as Heuristic Function:

import math

class Node:
def __init__(self, state, parent, actions, totalCost, heuristic):
self.state = state
self.parent = parent
self.actions = actions
self.totalCost = totalCost
self.heuristic = heuristic

def findMin(frontier):
minV = math.inf
node = None
for key in frontier:
if minV > frontier[key][1]:
minV = frontier[key][1]
node = key
return node

def actionSequence(graph, initialState, goalState):


solution = [goalState]
currentParent = graph[goalState].parent
while currentParent is not None:
solution.append(currentParent)
currentParent = graph[currentParent].parent
solution.reverse()
return solution
def Astar():
initialState = '00'
goalState = '99'

graph = {
'00': Node('00', None, [('01', 1), ('10', 1)], 0, (0, 0)),
'10': Node('10', None, [('11', 1), ('20', 1), ('00', 1)], 0, (1, 0)),
'20': Node('20', None, [('21', 1), ('10', 1), ('30', 1)], 0, (2, 0)),
'30': Node('30', None, [('20', 1), ('40', 1)], 0, (3, 0)),
'40': Node('40', None, [('30', 1), ('50', 1)], 0, (4, 0)),
'50': Node('50', None, [('40', 1)], 0, (5, 0)),
'01': Node('01', None, [('00', 1), ('02', 1), ('11', 1)], 0, (0, 1)),
'11': Node('11', None, [('01', 1), ('21', 1), ('10', 1)], 0, (1, 1)),
'21': Node('21', None, [('11', 1), ('22', 1), ('20', 1)], 0, (2, 1)),
'61': Node('61', None, [('62', 1)], 0, (6, 1)),
'81': Node('81', None, [('82', 1), ('91', 1), ('80', 1)], 0, (8, 1)),
'91': Node('91', None, [('81', 1)], 0, (9, 1)),
'02': Node('02', None, [('03', 1), ('01', 1)], 0, (0, 2)),
'22': Node('22', None, [('21', 1), ('32', 1)], 0, (2, 2)),
'32': Node('32', None, [('22', 1), ('33', 1)],0, (3, 2)),
'52': Node('52', None, [('53', 1), ('62', 1)], 0, (5, 2)),
'62': Node('62', None, [('52', 1), ('61', 1)], 0, (6, 2)),
'82': Node('82', None, [('81', 1)], 0, (8, 2)),
'03': Node('03', None, [('02', 1), ('13', 1)], 0, (0, 3)),
'13': Node('13', None, [('03', 1), ('14', 1)], 0, (1, 3)),
'33': Node('33', None, [('32', 1),('43', 1)], 0, (3, 3)),
'43': Node('43', None, [('44', 1),('53', 1),('33', 1)], 0, (4, 3)),
'53': Node('53', None, [('52', 1), ('43', 1)], 0, (5, 3)),
'14': Node('14', None, [('13', 1), ('15', 1),('24', 1)], 0, (1, 4)),
'24': Node('24', None, [('14', 1),('25', 1)], 0, (2, 4)),
'44': Node('44', None, [('43', 1), ('45', 1)], 0, (4, 4)),
'74': Node('74', None, [('75', 1), ('84', 1)], 0, (7, 4)),
'84': Node('84', None, [('74', 1), ('85', 1)], 0, (8, 4)),
'05': Node('05', None, [('06', 1), ('15', 1)], 0, (0, 5)),
'15': Node('15', None, [('05', 1), ('14', 1),('25', 1)], 0, (1, 5)),
'25': Node('25', None, [('35', 1),('24', 1)], 0, (2, 5)),
'35': Node('35', None, [('25', 1), ('36', 1), ('45', 1)], 0, (3, 5)),
'45': Node('45', None, [('35', 1), ('44', 1), ('46', 1)], 0, (4, 5)),
'75': Node('75', None, [('74', 1), ('76', 1),('85', 1)], 0, (7, 5)),
'85': Node('85', None, [('84', 1), ('75', 1),('86', 1),('95', 1)], 0, (8, 5)),
'95': Node('95', None, [('96', 1),('85', 1)], 0, (9, 5)),
'06': Node('06', None, [('05', 1), ('07', 1)], 0, (0, 6)),
'36': Node('36', None, [('37', 1), ('46', 1),('35', 1)], 0, (3, 6)),
'46': Node('46', None, [('45', 1), ('36', 1),('47', 1)], 0, (4, 6)),
'66': Node('66', None, [('67', 1), ('76', 1)], 0, (6, 6)),
'76': Node('76', None, [('75', 1), ('77', 1),('66', 1),('86', 1)], 0, (7, 6)),
'86': Node('86', None, [('87', 1), ('76', 1),('85', 1),('96', 1)], 0, (8, 6)),
'96': Node('96', None, [('95', 1), ('97', 1),('86', 1)], 0, (9, 6)),
'07': Node('07', None, [('06', 1), ('08', 1)], 0, (0, 7)),
'37': Node('37', None, [('36', 1), ('47', 1)], 0, (3, 7)),
'47': Node('47', None, [('37', 1), ('46', 1),('57', 1)], 0, (4, 7)),
'57': Node('57', None, [('47', 1), ('67', 1)], 0, (5, 7)),
'67': Node('67', None, [('66', 1), ('57', 1),('77', 1),('68', 1)], 0, (6, 7)),
'77': Node('77', None, [('78', 1), ('76', 1),('67', 1),('87', 1)], 0, (7, 7)),
'87': Node('87', None, [('86', 1), ('77', 1),('88', 1),('97', 1)], 0, (8, 7)),
'97': Node('97', None, [('96', 1), ('87', 1)], 0, (9, 7)),
'08': Node('08', None, [('07', 1), ('09', 1),('18', 1)], 0, (0, 8)),
'18': Node('18', None, [('08', 1), ('19', 1),('28', 1)], 0, (1, 8)),
'28': Node('28', None, [('18', 1)], 0, (2, 8)),
'48': Node('48', None, [('47', 1), ('49', 1)], 0, (4, 8)),
'68': Node('68', None, [('67', 1), ('69', 1),('78', 1)], 0, (6, 8)),
'78': Node('78', None, [('77', 1), ('79', 1),('68', 1),('88', 1)], 0, (7, 8)),
'88': Node('88', None, [('87', 1), ('78', 1),('89', 1)], 0, (8, 8)),
'09': Node('09', None, [('08', 1), ('19', 1)], 0, (0, 9)),
'19': Node('19', None, [('09', 1), ('18', 1)], 0, (1, 9)),
'49': Node('49', None, [('48', 1)], 0, (4, 9)),
'69': Node('69', None, [('79', 1), ('68', 1)], 0, (6, 9)),
'79': Node('79', None, [('89', 1), ('69', 1),('78', 1)], 0, (7, 9)),
'89': Node('89', None, [('79', 1), ('88', 1),('99',1)], 0, (8, 9)),
'99': Node('99', None, [('89', 1)], 0, (9, 9))
}

frontier = {}
# diagonal distance heuristics
dx = abs(graph[goalState].heuristic[0] - graph[initialState].heuristic[0] ** 2)
dy = abs((graph[goalState].heuristic[1] - graph[initialState].heuristic[1]) ** 2)
D = 1

if (dx < dy):


min = dx
elif (dx > dy):
min = dy

heuristicCost = D * (dx + dy) + (math.sqrt(2) - 2 * D) * min

frontier[initialState] = (None, heuristicCost)


explored = {}

while len(frontier) != 0:
currentNode = findMin(frontier)
del frontier[currentNode]

if graph[currentNode].state == goalState:
return actionSequence(graph, initialState, goalState)

heuristicCost = math.sqrt((graph[goalState].heuristic[0] -
graph[currentNode].heuristic[0]) ** 2 +
(graph[goalState].heuristic[1] - graph[currentNode].heuristic[1]) ** 2)
currentCost = graph[currentNode].totalCost
explored[currentNode] = (graph[currentNode].parent, heuristicCost + currentCost)

for child, cost in graph[currentNode].actions:


currentCost = cost + graph[currentNode].totalCost
heuristicCost = math.sqrt((graph[goalState].heuristic[0] - graph[child].heuristic[0])
** 2 + (graph[goalState].heuristic[1] - graph[child].heuristic[1]) ** 2)
if child in explored:
if graph[child].parent == currentNode or child == initialState or \
explored[child][1] <= currentCost + heuristicCost:
continue

if child not in frontier:


graph[child].parent = currentNode
graph[child].totalCost = currentCost
frontier[child] = (graph[child].parent, currentCost + heuristicCost)
else:
if frontier[child][1] < currentCost + heuristicCost:
graph[child].parent = frontier[child][0]
graph[child].totalCost = frontier[child][1] - heuristicCost
else:
frontier[child] = (currentNode, currentCost + heuristicCost)
graph[child].parent = frontier[child][0]
graph[child].totalCost = currentCost

solution = Astar()
print(solution)

 Using Manhattan Distance as Heuristic Function:


import math
class Node:
def __init__(self, state, parent, actions, totalCost, heuristic):
self.state = state
self.parent = parent
self.actions = actions
self.totalCost = totalCost
self.heuristic = heuristic

def findMin(frontier):
minV = math.inf
node = None
for key in frontier:
if minV > frontier[key][1]:
minV = frontier[key][1]
node = key
return node

def actionSequence(graph, initialState, goalState):


solution = [goalState]
currentParent = graph[goalState].parent
while currentParent is not None:
solution.append(currentParent)
currentParent = graph[currentParent].parent
solution.reverse()
return solution

def Astar():
initialState = '00'
goalState = '99'

graph = {
'00': Node('00', None, [('01', 1), ('10', 1)], 0, (0, 0)),
'10': Node('10', None, [('11', 1), ('20', 1), ('00', 1)], 0, (1, 0)),
'20': Node('20', None, [('21', 1), ('10', 1), ('30', 1)], 0, (2, 0)),
'30': Node('30', None, [('20', 1), ('40', 1)], 0, (3, 0)),
'40': Node('40', None, [('30', 1), ('50', 1)], 0, (4, 0)),
'50': Node('50', None, [('40', 1)], 0, (5, 0)),
'01': Node('01', None, [('00', 1), ('02', 1), ('11', 1)], 0, (0, 1)),
'11': Node('11', None, [('01', 1), ('21', 1), ('10', 1)], 0, (1, 1)),
'21': Node('21', None, [('11', 1), ('22', 1), ('20', 1)], 0, (2, 1)),
'61': Node('61', None, [('62', 1)], 0, (6, 1)),
'81': Node('81', None, [('82', 1), ('91', 1), ('80', 1)], 0, (8, 1)),
'91': Node('91', None, [('81', 1)], 0, (9, 1)),
'02': Node('02', None, [('03', 1), ('01', 1)], 0, (0, 2)),
'22': Node('22', None, [('21', 1), ('32', 1)], 0, (2, 2)),
'32': Node('32', None, [('22', 1), ('33', 1)],0, (3, 2)),
'52': Node('52', None, [('53', 1), ('62', 1)], 0, (5, 2)),
'62': Node('62', None, [('52', 1), ('61', 1)], 0, (6, 2)),
'82': Node('82', None, [('81', 1)], 0, (8, 2)),
'03': Node('03', None, [('02', 1), ('13', 1)], 0, (0, 3)),
'13': Node('13', None, [('03', 1), ('14', 1)], 0, (1, 3)),
'33': Node('33', None, [('32', 1),('43', 1)], 0, (3, 3)),
'43': Node('43', None, [('44', 1),('53', 1),('33', 1)], 0, (4, 3)),
'53': Node('53', None, [('52', 1), ('43', 1)], 0, (5, 3)),
'14': Node('14', None, [('13', 1), ('15', 1),('24', 1)], 0, (1, 4)),
'24': Node('24', None, [('14', 1),('25', 1)], 0, (2, 4)),
'44': Node('44', None, [('43', 1), ('45', 1)], 0, (4, 4)),
'74': Node('74', None, [('75', 1), ('84', 1)], 0, (7, 4)),
'84': Node('84', None, [('74', 1), ('85', 1)], 0, (8, 4)),
'05': Node('05', None, [('06', 1), ('15', 1)], 0, (0, 5)),
'15': Node('15', None, [('05', 1), ('14', 1),('25', 1)], 0, (1, 5)),
'25': Node('25', None, [('35', 1),('24', 1)], 0, (2, 5)),
'35': Node('35', None, [('25', 1), ('36', 1), ('45', 1)], 0, (3, 5)),
'45': Node('45', None, [('35', 1), ('44', 1), ('46', 1)], 0, (4, 5)),
'75': Node('75', None, [('74', 1), ('76', 1),('85', 1)], 0, (7, 5)),
'85': Node('85', None, [('84', 1), ('75', 1),('86', 1),('95', 1)], 0, (8, 5)),
'95': Node('95', None, [('96', 1),('85', 1)], 0, (9, 5)),
'06': Node('06', None, [('05', 1), ('07', 1)], 0, (0, 6)),
'36': Node('36', None, [('37', 1), ('46', 1),('35', 1)], 0, (3, 6)),
'46': Node('46', None, [('45', 1), ('36', 1),('47', 1)], 0, (4, 6)),
'66': Node('66', None, [('67', 1), ('76', 1)], 0, (6, 6)),
'76': Node('76', None, [('75', 1), ('77', 1),('66', 1),('86', 1)], 0, (7, 6)),
'86': Node('86', None, [('87', 1), ('76', 1),('85', 1),('96', 1)], 0, (8, 6)),
'96': Node('96', None, [('95', 1), ('97', 1),('86', 1)], 0, (9, 6)),
'07': Node('07', None, [('06', 1), ('08', 1)], 0, (0, 7)),
'37': Node('37', None, [('36', 1), ('47', 1)], 0, (3, 7)),
'47': Node('47', None, [('37', 1), ('46', 1),('57', 1)], 0, (4, 7)),
'57': Node('57', None, [('47', 1), ('67', 1)], 0, (5, 7)),
'67': Node('67', None, [('66', 1), ('57', 1),('77', 1),('68', 1)], 0, (6, 7)),
'77': Node('77', None, [('78', 1), ('76', 1),('67', 1),('87', 1)], 0, (7, 7)),
'87': Node('87', None, [('86', 1), ('77', 1),('88', 1),('97', 1)], 0, (8, 7)),
'97': Node('97', None, [('96', 1), ('87', 1)], 0, (9, 7)),
'08': Node('08', None, [('07', 1), ('09', 1),('18', 1)], 0, (0, 8)),
'18': Node('18', None, [('08', 1), ('19', 1),('28', 1)], 0, (1, 8)),
'28': Node('28', None, [('18', 1)], 0, (2, 8)),
'48': Node('48', None, [('47', 1), ('49', 1)], 0, (4, 8)),
'68': Node('68', None, [('67', 1), ('69', 1),('78', 1)], 0, (6, 8)),
'78': Node('78', None, [('77', 1), ('79', 1),('68', 1),('88', 1)], 0, (7, 8)),
'88': Node('88', None, [('87', 1), ('78', 1),('89', 1)], 0, (8, 8)),
'09': Node('09', None, [('08', 1), ('19', 1)], 0, (0, 9)),
'19': Node('19', None, [('09', 1), ('18', 1)], 0, (1, 9)),
'49': Node('49', None, [('48', 1)], 0, (4, 9)),
'69': Node('69', None, [('79', 1), ('68', 1)], 0, (6, 9)),
'79': Node('79', None, [('89', 1), ('69', 1),('78', 1)], 0, (7, 9)),
'89': Node('89', None, [('79', 1), ('88', 1),('99',1)], 0, (8, 9)),
'99': Node('99', None, [('89', 1)], 0, (9, 9))
}

frontier = {}

# Manhattan distance
dx = abs(graph[goalState].heuristic[0] - graph[initialState].heuristic[0] )
dy = abs((graph[goalState].heuristic[1] - graph[initialState].heuristic[1]))

heuristicCost = dx + dy

frontier[initialState] = (None, heuristicCost)


explored = {}

while len(frontier) != 0:
currentNode = findMin(frontier)
del frontier[currentNode]

if graph[currentNode].state == goalState:
return actionSequence(graph, initialState, goalState)

heuristicCost = math.sqrt((graph[goalState].heuristic[0] -
graph[currentNode].heuristic[0]) ** 2 +
(graph[goalState].heuristic[1] -
graph[currentNode].heuristic[1]) ** 2)
currentCost = graph[currentNode].totalCost
explored[currentNode] = (graph[currentNode].parent, heuristicCost +
currentCost)

for child, cost in graph[currentNode].actions:


currentCost = cost + graph[currentNode].totalCost
heuristicCost = math.sqrt((graph[goalState].heuristic[0] -
graph[child].heuristic[0]) ** 2 + (graph[goalState].heuristic[1] -
graph[child].heuristic[1]) ** 2)
if child in explored:
if graph[child].parent == currentNode or child == initialState or \
explored[child][1] <= currentCost + heuristicCost:
continue
if child not in frontier:
graph[child].parent = currentNode
graph[child].totalCost = currentCost
frontier[child] = (graph[child].parent, currentCost + heuristicCost)
else:
if frontier[child][1] < currentCost + heuristicCost:
graph[child].parent = frontier[child][0]
graph[child].totalCost = frontier[child][1] - heuristicCost
else:
frontier[child] = (currentNode, currentCost + heuristicCost)
graph[child].parent = frontier[child][0]
graph[child].totalCost = currentCost

solution = Astar()
print(solution)

You might also like