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

A Star Search Implementation

The document provides a Python implementation of the A* search algorithm for pathfinding on a grid. It defines the grid dimensions, start and goal positions, and uses the Manhattan distance as the heuristic function. The algorithm processes the grid to find the shortest path from the start to the goal, printing the result at the end.

Uploaded by

Sayan Neogy
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)
10 views3 pages

A Star Search Implementation

The document provides a Python implementation of the A* search algorithm for pathfinding on a grid. It defines the grid dimensions, start and goal positions, and uses the Manhattan distance as the heuristic function. The algorithm processes the grid to find the shortest path from the start to the goal, printing the result at the end.

Uploaded by

Sayan Neogy
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

A* Search Implementation.

Source Code:

import heapq

#Define the grid dimensions

grid_rows = 10

grid_cols = 10

#Define the grid

grid = [[0 for _ in range(grid_cols)] for _ in range(grid_rows)]

#Define the start and goal positions

start_pos = (0, 0)

goal_pos = (grid_rows - 1, grid_cols - 1)

#Define the heuristic function (Manhattan distance)

def heuristic(pos):

return abs(pos[0] - goal_pos[0]) + abs(pos[1] - goal_pos[1])

#Define the A* algorithm function

def astar(grid, start_pos, goal_pos):

# Define the open and closed lists

open_list = []

closed_list = set()

# Define the cost and previous position dictionaries

cost_dict = {start_pos: 0}

prev_pos_dict = {}

# Add the start position to the open list

heapq.heappush(open_list, (0, start_pos))


# Loop until the open list is empty

while open_list:

# Get the current position from the open list

current_cost, current_pos = heapq.heappop(open_list)

# Check if the current position is the goal position

if current_pos == goal_pos:

# Reconstruct the path

path = []

while current_pos in prev_pos_dict:

path.append(current_pos)

current_pos = prev_pos_dict[current_pos]

path.append(start_pos)

path.reverse()

return path

# Add the current position to the closed list

closed_list.add(current_pos)

# Get the neighbors of the current position

neighbors = [(current_pos[0] - 1, current_pos[1]), (current_pos[0] +


1, current_pos[1]),

(current_pos[0], current_pos[1] - 1), (current_pos[0],


current_pos[1] + 1)]

# Loop through the neighbors

for neighbor in neighbors:

# Check if the neighbor is within the grid boundaries

if 0 <= neighbor[0] < grid_rows and 0 <= neighbor[1] < grid_cols:

# Check if the neighbor is not in the closed list


if neighbor not in closed_list:

# Calculate the new cost

new_cost = cost_dict[current_pos] + 1

# Check if the neighbor is not in the open list or the


new cost is less than the previous cost

if neighbor not in cost_dict or new_cost <


cost_dict[neighbor]:

# Update the cost and previous position dictionaries

cost_dict[neighbor] = new_cost

prev_pos_dict[neighbor] = current_pos

# Calculate the priority

priority = new_cost + heuristic(neighbor)

# Add the neighbor to the open list

heapq.heappush(open_list, (priority, neighbor))

# Return None if no path is found

return None

# Run the A* algorithm

path = astar(grid, start_pos, goal_pos)

# Print the path

if path:

print("Path found:", path)

else:

print("No path found")

You might also like