SP20-BCS-114 LAB - Assign - 2
SP20-BCS-114 LAB - Assign - 2
Course Title: Artificial Intelligence Lab Course Code: CSC462 Credit Hours: 3(2,1)
Course Instructor: Dr. Atifa Athar Programme Name: BS Computer Science
Semester: 6th Batch: SP20 Section: A Date: 09-10-2023
Due Date: 16-04-2023 Maximum Marks: 10
Student Name: Hassan Mahmood Registration No. SP20-BCS-114
Important Instructions / Guidelines:
# Iterate over frontier nodes and find the min out of them
and return it. Frontier are the neighbours node of current
node.
def findMin(frontier):
minNode = None
minCost = float("inf")
for node, (parent, cost) in frontier.items():
if cost < minCost:
minNode = node
minCost = cost
return minNode
Action Sequence:
currentNode = goalState
while currentNode != initialState:
parent = graph[currentNode].parent
for action, _ in graph[parent].actions:
if action == currentNode:
sequence.insert(0, action)
break
currentNode = parent
sequence.insert(0, initialState)
return sequence
A* Algorithm Code:
heuristicCost = math.sqrt(
(graph[goalState].heuristic[0] -
graph[initialState].heuristic[0]) ** 2
+ (graph[goalState].heuristic[1] -
graph[initialState].heuristic[1]) ** 2
)
# print(round(heuristicCost,2))
while frontier:
currentNode = findMin(frontier)
currentCost = frontier[currentNode][1]
del frontier[currentNode]
if currentNode == goalState:
return actionSequence(graph, initialState,
goalState)
explored[currentNode] = (graph[currentNode].parent,
currentCost)
if child in explored:
if (
graph[child].parent == currentNode
or child == initialState
or explored[child][1] <= childCost +
heuristicCost
):
continue
return None
initialState = "Arad"
goalState = "Bucharest"
Results: