0% found this document useful (0 votes)
4 views5 pages

AI Exp - 6

The document outlines an experiment on implementing the A* Search algorithm, a popular informed search technique used for path-finding and graph traversal. It explains the algorithm's mechanics, including the parameters 'g' and 'h' used to determine the best path through a grid with obstacles. The document also includes a code implementation of the algorithm and concludes with a summary of the study and implementation of A*.

Uploaded by

krutikapandya23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

AI Exp - 6

The document outlines an experiment on implementing the A* Search algorithm, a popular informed search technique used for path-finding and graph traversal. It explains the algorithm's mechanics, including the parameters 'g' and 'h' used to determine the best path through a grid with obstacles. The document also includes a code implementation of the algorithm and concludes with a summary of the study and implementation of A*.

Uploaded by

krutikapandya23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Universal College of Engineering, Kaman

Department of Computer Engineering


Subject: Artificial Intelligence
Experiment No: 6
Roll No: 55 Name: Krutika Pandya Div: B Batch: B1

Aim:- Implement Informed search Technique (A*)

Theory:-

A* Search algorithm is one of the best and popular technique used in path-finding and graph
traversals.Informally speaking, A* Search algorithms, unlike other traversal techniques, it has
“brains”. What it means is that it is really a smart algorithm which separates it from the other
conventional algorithms. This fact is cleared in detail in below sections. And it is also worth
mentioning that many games and web-based maps use this algorithm to find the shortest path very
efficiently (approximation).

Explanation :-Consider a square grid having many obstacles and we are given a starting cell and
a target cell. We want to reach the target cell (if possible) from the starting cell as quickly as
possible. Here A* Search Algorithm comes to the rescue. What A* Search Algorithm does is that
at each step it picks the node according to a value-‘f’ which is a parameter equal to the sum of two
other parameters – ‘g’ and ‘h’. At each step it picks the node/cell having the lowest ‘f’, and
process that node/cell. We define ‘g’ and ‘h’ as simply as possible below g = the movement cost
to move from the starting point to a given square on the grid, following the path generated to get
there. h = the estimated movement cost to move from that given square on the grid to the final
destination. This is often referred to as the heuristic, which is nothing but a kind of smart guess.
We really don’t know the actual distance until we find the path, because all sorts of things can be
in the way (walls, water, etc.). There can be many ways to calculate this ‘h’ which are discussed in
the later sections.

Code:-

def get_neighbors(node): return Graph_nodes.get(node, []) # Fetch neighbors or return empty list
def aStarAlgo(start_node, stop_node):

open_set = set([start_node]) # Correct initialization

closed_set = set()

g = {} # Cost from start node

parents = {} # Store parent nodes

g[start_node] = 0 parents[start_node]

= start_node while len(open_set) >

0:

n = None

# Find node with lowest g(n)

for v in open_set:

if n is None or g[v] < g[n]:

n=v if n is None:

print("Path does not exist!")

return None

# If the goal node is reached

if n == stop_node:

path = []

while parents[n] != n:

path.append(n)

n = parents[n]

path.append(start_node)
path.reverse()

print("Path found:", path)

return path

# Check if n is not in Graph_nodes

if Graph_nodes.get(n) is None:

Continue # Explore neighbors

for (m, weight) in get_neighbors(n): if m not in open_set and m not in closed_set:

open_set.add(m)

parents[m] = n

g[m] = g[n] + weight

else: if g[m] > g[n] + weight:

g[m] = g[n] + weight

parents[m] = n

if m in closed_set:

closed_set.remove(m)

open_set.add(m)

open_set.remove(n) closed_set.add(n)

print("Path does not exist!") return None

# Define Graph Nodes (Dictionary)

Graph_nodes = {

'A': [('B', 1), ('C', 3)],

'B': [('D', 1), ('E', 5)],


'C': [('E', 2)],

'D': [('G', 9)],

'E': [('G', 4)],

'G': []

# Call the function with correct arguments

aStarAlgo('A', 'G')

Output:-

Conclusion:-Thus we have studied and implemented A* algorithm.

You might also like