suraj0152

A* in Python in 3D env

Jan 14th, 2024
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.73 KB | Software | 0 0
  1. ## A star Algorithm
  2.  
  3. import heapq
  4. import numpy as np
  5. import matplotlib.pyplot as plt
  6. from mpl_toolkits.mplot3d import Axes3D
  7.  
  8. class Node:
  9.     def __init__(self, position, cost=0, parent=None):
  10.         self.position = position
  11.         self.cost = cost
  12.         self.parent = parent
  13.  
  14.     def __lt__(self, other):
  15.         return self.cost < other.cost
  16.  
  17. def heuristic(current, goal):
  18.     return abs(current[0] - goal[0]) + abs(current[1] - goal[1]) + abs(current[2] - goal[2])
  19.  
  20. def is_valid_position(position, grid_shape):
  21.     return 0 <= position[0] < grid_shape[0] and 0 <= position[1] < grid_shape[1] and 0 <= position[2] < grid_shape[2]
  22.  
  23. def get_neighbors(current, grid_shape):
  24.     x, y, z = current
  25.     neighbors = []
  26.  
  27.     for dx in [-1, 0, 1]:
  28.         for dy in [-1, 0, 1]:
  29.             for dz in [-1, 0, 1]:
  30.                 if dx == 0 and dy == 0 and dz == 0:
  31.                     continue
  32.  
  33.                 neighbor = (x + dx, y + dy, z + dz)
  34.                 if is_valid_position(neighbor, grid_shape):
  35.                     neighbors.append(neighbor)
  36.  
  37.     return neighbors
  38.  
  39. def visualize_path(voxel_grid, path):
  40.     fig = plt.figure()
  41.     ax = fig.add_subplot(111, projection='3d')
  42.  
  43.     x, y, z = np.where(voxel_grid == 1)
  44.     ax.scatter(x, y, z, c='black', marker='s', label='Obstacle')
  45.  
  46.     x, y, z = zip(*path)
  47.     ax.plot(x, y, z, marker='o', linestyle='-', color='r', label='Path')
  48.  
  49.     ax.set_xlabel('X-axis')
  50.     ax.set_ylabel('Y-axis')
  51.     ax.set_zlabel('Z-axis')
  52.  
  53.     plt.legend()
  54.     plt.show()
  55.  
  56. def astar():
  57.     #3D voxel
  58.     voxel_grid = np.zeros((10, 10, 10))
  59.     voxel_grid[2:8, 2:8, 2:8] = 1  # Obstacle in voxel
  60.  
  61.     start = (0, 0, 0)
  62.     goal = (9, 9, 9)
  63.     heap = [Node(start)]
  64.     visited = set()
  65.  
  66.     while heap:
  67.         current_node = heapq.heappop(heap)
  68.         current_position = current_node.position
  69.  
  70.         if current_position == goal:
  71.             path = []
  72.             while current_node:
  73.                 path.append(current_node.position)
  74.                 current_node = current_node.parent
  75.  
  76.             path.reverse()
  77.             return path
  78.  
  79.         if current_position in visited:
  80.             continue
  81.  
  82.         visited.add(current_position)
  83.  
  84.         for neighbor_position in get_neighbors(current_position, voxel_grid.shape):
  85.             if voxel_grid[neighbor_position] == 1:
  86.                 continue
  87.  
  88.             neighbor_node = Node(neighbor_position)
  89.             #f(n)=g(n)+h(n)
  90.             neighbor_node.cost = current_node.cost + heuristic(neighbor_position, goal)
  91.             neighbor_node.parent = current_node
  92.  
  93.             heapq.heappush(heap, neighbor_node)
  94.  
  95.     return None
  96.  
  97. path = astar()
  98.  
  99. print("Simplest Path:", path)
  100. visualize_path(voxel_grid, path)
Advertisement
Add Comment
Please, Sign In to add comment