Terna Engineering College
Computer Engineering Department
Program: Sem VI
Course: Artificial Intelligence
Faculty: Rohini Patil
LAB Manual
PART A
Experiment No.03
A.1 Aim: To Implement informed A* search methods using C /python/Java.
A.2 Prerequisite: Data Structure, Searching Techniques
A.3 Outcome:
After successful completion of this experiment students
will be able to
Ability to analyse the local and global impact of computing in searching techniques.
Understand, identify, analyse and design the problem, implement and validate the
solution for the A* search method.
• Ability to applying knowledge of computing in search technique areas.
Tools Required: C /Java
A.4 Theory:
An informed search strategy-one that uses problem-specific knowledge-can find solutions
more efficiently.A key component of these algorithms is a heuristic function h(n).
h(n) = estimated cost of the cheapest path from node n to a goal node.Admissible /heuristic
never over estimated i.e. h(n)≤ Actual cost.
For example, Distance between two nodes(cities)=> straight line distance and for 8-puzzel
problem- Admissible heuristic can be number of misplaced tiles h(n)= 8.
A* Search technique:
It is informed search technique. It uses additional information beyond problem formulation
and tree. Search is based on Evaluation function f(n). Evaluation function is based on both
heuristic function h(n) and g(n).
f(n)=g(n) + h(n)
It uses two queues for its implementation: open, close Queue. Open queue is a priority queue
which is arranged in ascending order of f(n)
Algorithm:
1. Create a single member queue comprising of Root node
2. If FIRST member of queue is goal then goto step 5
3. If first member of queue is not goal then remove it from queue and add to
close queue.
4. Consider its children if any, and add them to queue in ascending order of
evaluation function f(n).
5. If queue is not empty then goto step 2.
6. If queue is empty then goto step 6
7. Print ‘success’ and stop
8. Print ‘failure’ and stop.
Performance Comparison:
Completeness: yes
Optimality: yes
Time complexity :o(bd)
Limitation:
It generate same node again and again
Large Memory is required
Observation
Although A*generate many nodes it never uses those nodes for which f(n)> c* where c* is
optimum cost.
Consider an example as below
OPEN/FRINGE CLOSE
[A] [ ]
[C,B] [A]
[D,B,E,A] [A,C]
[F,E,B,C,A] [A,C,D]
[G,E,B,C,A,D] [A,C,D,F]
SUCCESS
Node A:
f(B)=g(B) + h(B)=3+5=8
f(C) =g(C) + h(C)=1 + 6=7
Node C:
f(A) = g(A) + h(A)=2+7=10
f(D) = g(D) + h(D)=3+4=7
f(E) = g(E) + h(E)=7+1=8
Node D:
f(F) = g(F) + h(F)=6+1=7
f(C) = g(C) + h(C)= 5+6=11
f(B) = g(B) + h(B)=4+5=9
Node F:
f(E) = g(E) + h(E)=7+1=8
f(D) = g(D) + h(D)= 9+4=13
f(G) = g(G) + h(G)=7+0=7
Final path: A C D F G Total
cost= 7
PART B
(PART B : TO BE COMPLETED BY
STUDENTS)
(Students must submit the soft copy as per following segments within
two hours of the practical. The soft copy must be uploaded on the
Blackboard or emailed to the concerned lab in charge faculties at the
end of the practical in case the there is no Black board access available)
Roll No. 60 Name: Shubhankar Vijay kanoje
Class : TE-A Comps Batch : A3
Date of Experiment: 28/01/2022 Date of Submission: 04/02/2022
Grade :
B.1 Document created by the student:
(Write the answers to the questions given in section 4 during the 2
hours of practical in the lab here)
from future import print_function
import matplotlib.pyplot as plt
class AStarGraph(object):
def init (self):
self.barriers = []
self.barriers.append([(2,4),(2,5),(2,6),(3,6),(4,6),(5,6),(5,5),(5,4),(5,3),(5,2),
(4,2),(3,2)])
def heuristic(self, start, goal):
D=1
D2 = 1
dx = abs(start[0] - goal[0])
dy = abs(start[1] - goal[1])
return D * (dx + dy) + (D2 - 2 * D) * min(dx, dy)
def get_vertex_neighbours(self, pos):
n = []
for dx, dy in [(1,0),(-1,0),(0,1),(0,-1),(1,1),(-1,1),(1,-1),(-1,-1)]:
x2 = pos[0] + dx
y2 = pos[1] + dy
if x2 < 0 or x2 > 7 or y2 < 0 or y2 > 7:
continue
n.append((x2, y2))
return n
def move_cost(self, a, b):
for barrier in self.barriers:
if b in barrier:
return 100
def AStarSearch(start, end, graph):
G = {}
F = {}
G[start] = 0
F[start] = graph.heuristic(start, end)
closedVertices = set()
openVertices = set([start])
cameFrom = {}
while len(openVertices) > 0:
current = None
currentFscore = None
for pos in openVertices:
if current is None or F[pos] < currentFscore:
currentFscore = F[pos]
current = pos
if current == end:
path = [current]
while current in cameFrom:
current = cameFrom[current]
path.append(current)
path.reverse()
return path, F[end]
openVertices.remove(current)
closedVertices.add(current)
for neighbour in graph.get_vertex_neighbours(current):
if neighbour in closedVertices:
continue
candidateG = G[current] + graph.move_cost(current, neighbour)
if neighbour not in openVertices:
openVertices.add(neighbour)
elif candidateG >= G[neighbour]:
continue
cameFrom[neighbour] = current
G[neighbour] = candidateG
H = graph.heuristic(neighbour, end)
F[neighbour] = G[neighbour] + H
raise RuntimeError("A* failed to find a solution")
if name ==" main ":
graph = AStarGraph()
result, cost = AStarSearch((0,0), (7,7), graph)
print ("route", result)
print ("cost", cost)
B.2 Observations and learning:
A-star (also referred to as A*) is a search algorithm to find the shortest path between nodes or
graphs. It is an informed search algorithm, as it uses information about path cost and also
uses heuristics to find the solution. A major drawback of the algorithm is its space and time
complexity. It takes a large amount of space to store all possible paths and a lot of time to
find them.
B.3 Conclusion:
In this experiment we Implement informed A* search methods using Python.
B.4 Question of Curiosity:
Q1) Apply A* algorithm in the following example and find the cost
Ans: f = accumulated path cost + heuristic
QUEUE = path containing root
QUEUE: <S>
QUEUE: <SB,SA>
QUEUE: <SA,SBC,SBG,SBA>
QUEUE: <SBC,SBG,SAB>
QUEUE: <SBCG,SBG>
QUEUE: <SBCG>
Q2) What is the other name of informed search strategy?
a. Simple search
b. heuristic search
c. online search
d. none of the above
Ans: b
Explanation: A key point of informed search strategy is heuristic function, So it is called as
heuristic function.