Astar3D.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # this is the three dimensional A* algo
  2. # !/usr/bin/env python3
  3. # -*- coding: utf-8 -*-
  4. """
  5. @author: yue qi
  6. """
  7. import numpy as np
  8. import matplotlib.pyplot as plt
  9. import os
  10. import sys
  11. sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../../Search_based_Planning/")
  12. from Search_3D.env3D import env
  13. from Search_3D.utils3D import getDist, getRay, g_Space, Heuristic, getNearest, isCollide, \
  14. cost, children, StateSpace, heuristic_fun
  15. from Search_3D.plot_util3D import visualization
  16. import queue
  17. import time
  18. class Weighted_A_star(object):
  19. def __init__(self, resolution=0.5):
  20. self.Alldirec = {(1, 0, 0): 1, (0, 1, 0): 1, (0, 0, 1): 1, \
  21. (-1, 0, 0): 1, (0, -1, 0): 1, (0, 0, -1): 1, \
  22. (1, 1, 0): np.sqrt(2), (1, 0, 1): np.sqrt(2), (0, 1, 1): np.sqrt(2), \
  23. (-1, -1, 0): np.sqrt(2), (-1, 0, -1): np.sqrt(2), (0, -1, -1): np.sqrt(2), \
  24. (1, -1, 0): np.sqrt(2), (-1, 1, 0): np.sqrt(2), (1, 0, -1): np.sqrt(2), \
  25. (-1, 0, 1): np.sqrt(2), (0, 1, -1): np.sqrt(2), (0, -1, 1): np.sqrt(2), \
  26. (1, 1, 1): np.sqrt(3), (-1, -1, -1) : np.sqrt(3), \
  27. (1, -1, -1): np.sqrt(3), (-1, 1, -1): np.sqrt(3), (-1, -1, 1): np.sqrt(3), \
  28. (1, 1, -1): np.sqrt(3), (1, -1, 1): np.sqrt(3), (-1, 1, 1): np.sqrt(3)}
  29. self.settings = 'NonCollisionChecking' # 'NonCollisionChecking' or 'CollisionChecking'
  30. self.env = env(resolution=resolution)
  31. self.start, self.goal = tuple(self.env.start), tuple(self.env.goal)
  32. self.g = {self.start:0,self.goal:np.inf}
  33. self.Parent = {}
  34. self.CLOSED = set()
  35. self.V = []
  36. self.done = False
  37. self.Path = []
  38. self.ind = 0
  39. self.x0, self.xt = self.start, self.goal
  40. self.OPEN = queue.MinheapPQ() # store [point,priority]
  41. self.OPEN.put(self.x0, self.g[self.x0] + heuristic_fun(self,self.x0)) # item, priority = g + h
  42. self.lastpoint = self.x0
  43. def run(self, N=None):
  44. xt = self.xt
  45. xi = self.x0
  46. while self.OPEN: # while xt not reached and open is not empty
  47. xi = self.OPEN.get()
  48. if xi not in self.CLOSED:
  49. self.V.append(np.array(xi))
  50. self.CLOSED.add(xi) # add the point in CLOSED set
  51. if getDist(xi,xt) < self.env.resolution:
  52. break
  53. # visualization(self)
  54. for xj in children(self,xi):
  55. # if xj not in self.CLOSED:
  56. if xj not in self.g:
  57. self.g[xj] = np.inf
  58. else:
  59. pass
  60. a = self.g[xi] + cost(self, xi, xj)
  61. if a < self.g[xj]:
  62. self.g[xj] = a
  63. self.Parent[xj] = xi
  64. # assign or update the priority in the open
  65. self.OPEN.put(xj, a + 1 * heuristic_fun(self, xj))
  66. # For specified expanded nodes, used primarily in LRTA*
  67. if N:
  68. if len(self.CLOSED) % N == 0:
  69. break
  70. if self.ind % 100 == 0: print('number node expanded = ' + str(len(self.V)))
  71. self.ind += 1
  72. self.lastpoint = xi
  73. # if the path finding is finished
  74. if self.lastpoint in self.CLOSED:
  75. self.done = True
  76. self.Path = self.path()
  77. if N is None:
  78. visualization(self)
  79. plt.show()
  80. return True
  81. return False
  82. def path(self):
  83. path = []
  84. x = self.lastpoint
  85. start = self.x0
  86. while x != start:
  87. path.append([x, self.Parent[x]])
  88. x = self.Parent[x]
  89. # path = np.flip(path, axis=0)
  90. return path
  91. # utility used in LRTA*
  92. def reset(self, xj):
  93. self.g = g_Space(self) # key is the point, store g value
  94. self.start = xj
  95. self.g[getNearest(self.g, self.start)] = 0 # set g(x0) = 0
  96. self.x0 = xj
  97. self.OPEN.put(self.x0, self.g[self.x0] + heuristic_fun(self,self.x0)) # item, priority = g + h
  98. self.CLOSED = set()
  99. # self.h = h(self.Space, self.goal)
  100. if __name__ == '__main__':
  101. Astar = Weighted_A_star(0.5)
  102. sta = time.time()
  103. Astar.run()
  104. print(time.time() - sta)