CodexBloom - Programming Q&A Platform

Issues with Implementing A* Algorithm in Python - Inconsistent Pathfinding Results

πŸ‘€ Views: 1271 πŸ’¬ Answers: 1 πŸ“… Created: 2025-07-23
pathfinding algorithm numpy A* Python

I'm working on a project and hit a roadblock. I'm collaborating on a project where I'm upgrading from an older version and I've looked through the documentation and I'm still confused about I'm currently implementing the A* pathfinding algorithm in Python using the `numpy` library for efficient array manipulations, but I'm encountering inconsistent results when trying to find the shortest path in a grid..... Sometimes, the algorithm returns longer paths or fails to find a path altogether, even though a valid path exists. Here’s a simplified version of my code: ```python import numpy as np from queue import PriorityQueue class Node: def __init__(self, position, parent=None): self.position = position # (x, y) coordinates self.parent = parent self.g = 0 # Cost from start to current node self.h = 0 # Heuristic cost to end node self.f = 0 # Total cost def heuristic(a, b): return abs(a[0] - b[0]) + abs(a[1] - b[1]) # Manhattan distance def astar(start, end, grid): open_list = PriorityQueue() closed_list = set() start_node = Node(start) end_node = Node(end) open_list.put((start_node.f, start_node)) while not open_list.empty(): current_node = open_list.get()[1] closed_list.add(current_node.position) if current_node.position == end_node.position: path = [] while current_node: path.append(current_node.position) current_node = current_node.parent return path[::-1] # Return reversed path for new_position in [(0, 1), (1, 0), (0, -1), (-1, 0)]: node_position = (current_node.position[0] + new_position[0], current_node.position[1] + new_position[1]) if (0 <= node_position[0] < grid.shape[0]) and (0 <= node_position[1] < grid.shape[1]) and grid[node_position] != 1: if node_position in closed_list: continue neighbor_node = Node(node_position, current_node) neighbor_node.g = current_node.g + 1 neighbor_node.h = heuristic(node_position, end_node.position) neighbor_node.f = neighbor_node.g + neighbor_node.h if not any(neighbor_node.f < n.f for _, n in open_list.queue if n.position == node_position): open_list.put((neighbor_node.f, neighbor_node)) return [] # No path found # Example grid # 0 = walkable, 1 = wall grid = np.array([ [0, 0, 0, 0], [0, 1, 0, 1], [0, 0, 0, 0], ]) start = (0, 0) end = (2, 3) path = astar(start, end, grid) print('Path:', path) ``` I’ve ensured that the grid is initialized properly, and I’m checking for walls correctly, but I still get unexpected paths sometimes, especially if I change the `start` or `end` positions. Could this be due to how I’m managing the open and closed lists? Any suggestions on how to improve this implementation or identify the root cause of the issue would be greatly appreciated. I'm using Python 3.9 and have tested on multiple grids with varying complexities. I'm working on a API that needs to handle this. My development environment is Linux. What's the best practice here? My team is using Python for this application.