0% found this document useful (0 votes)
12 views3 pages

Ai 2 3166

The document outlines an implementation of the A* algorithm for pathfinding in a grid. It defines a Node class to represent each position, calculates costs, and reconstructs the path to the goal using a heuristic function (Manhattan distance). An example usage is provided, demonstrating the algorithm's functionality with a specific grid and start/goal positions.
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)
12 views3 pages

Ai 2 3166

The document outlines an implementation of the A* algorithm for pathfinding in a grid. It defines a Node class to represent each position, calculates costs, and reconstructs the path to the goal using a heuristic function (Manhattan distance). An example usage is provided, demonstrating the algorithm's functionality with a specific grid and start/goal positions.
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/ 3

Name :- Pragati Bhujang Jagtap

Roll no :- 3166

EXPERIMENT No :- 2

# Define the Node class class Node: def __init__(self , position ,


g_cost=0 , h_cost=0 , parent=None):
self.position=position # (x, y) position of the node self.g_cost=g_cost
# Cost to reach this node from start self.h_cost=h_cost # Heuristic
estimate to the goal
self.parent=parent # Parent node (to reconstruct the path)

# f_cost is the sum of g_cost and h_cost


def f_cost(self):
return self.g_cost+self.h_cost

# A* Algorithm Implementation using lists def


a_star(start , goal , grid , heuristic):
open_list=[] # List of nodes to be evaluated
closed_list=[] # List of nodes that have been evaluated

# Start node initialization


start_node=Node(start , g_cost=0 , h_cost=heuristic(start , goal))
open_list.append(start_node)

while open_list:
# Get the node with the lowest f_cost (A* logic)
current_node=min(open_list , key=lambda node: node.f_cost())
open_list.remove(current_node)

# If we've reached the goal, reconstruct the path


if current_node.position==goal: path=[]
while current_node:
path.append(current_node.position)
current_node=current_node.parent
return path[::-1] # Return reversed path

closed_list.append(current_node.position)

# Check all adjacent nodes (4 directions: up, down, left, right)


for neighbor_pos in get_neighbors(current_node.position , grid):
if neighbor_pos in closed_list:
continue # Skip already visited nodes
# g_cost from start node to this neighbor
g_cost=current_node.g_cost+1 # Assume uniform cost of 1
h_cost=heuristic(neighbor_pos , goal)
neighbor_node=Node(neighbor_pos , g_cost=g_cost , h_cost=h_cost , parent=current_node)

# If this neighbor is not in open_list or has a lower f_cost


if not any(node.position==neighbor_pos and node.f_cost()<=neighbor_node.f_cost() for node
in open_list):
open_list.append(neighbor_node)

return None # No path found

# Get the valid neighboring positions (up, down, left, right) within the grid def
get_neighbors(position , grid):
x , y=position
neighbors=[]
directions=[(-1 , 0) , (1 , 0) , (0 , -1) , (0 , 1)] # Up, down, left, right
for dx , dy in directions: nx , ny=x+dx , y+dy
# Check if within bounds and not an obstacle if
0<=nx<len(grid) and 0<=ny<len(grid[0]) and grid[nx][ny]!=1:
neighbors.append((nx , ny))
return neighbors

# Heuristic function: Manhattan distance def


manhattan_heuristic(position , goal):
x1 , y1=position
x2 , y2=goal
return abs(x1-x2)+abs(y1-y2)

# Example usage: if
__name__=="__main__":
grid=[
[0 , 0 , 0 , 0 , 0] ,
[0 , 1 , 1 , 0 , 0] ,
[0 , 0 , 0 , 1 , 0] ,
[0 , 1 , 0 , 0 , 0] ,
[0 , 0 , 0 , 0 , 0]
]
start=(0 , 0) # Start position (top-left corner)
goal=(4 , 4) # Goal position (bottom-right corner)
path=a_star(start , goal , grid , manhattan_heuristic)
if path:
print("Path found:" , path)
else: print("No path
found.")

OUTPUT :-
C:\Users\bhumi\PycharmProjects\pythonProject9\venv\Scripts\python
.exe C:\Users\bhumi\PycharmProjects\pythonProject9\p2.py Path
found: [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (4, 1), (4, 2), (4, 3), (4,4)]

Process finished with exit code 0

You might also like