Ai 2 3166
Ai 2 3166
Roll no :- 3166
EXPERIMENT No :- 2
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)
closed_list.append(current_node.position)
# 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
# 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)]