Astar3D.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 = '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. # if (a, xj) in self.OPEN.enumerate():
  65. # update priority of xj
  66. self.OPEN.put(xj, a + 1 * heuristic_fun(self, xj))
  67. # else:
  68. # add xj in to OPEN set
  69. # self.OPEN.put(xj, a + 1 * heuristic_fun(self, xj))
  70. # For specified expanded nodes, used primarily in LRTA*
  71. if N:
  72. if len(self.CLOSED) % N == 0:
  73. break
  74. if self.ind % 100 == 0: print('number node expanded = ' + str(len(self.V)))
  75. self.ind += 1
  76. self.lastpoint = xi
  77. # if the path finding is finished
  78. if self.lastpoint in self.CLOSED:
  79. self.done = True
  80. self.Path = self.path()
  81. if N is None:
  82. #visualization(self)
  83. plt.show()
  84. return True
  85. return False
  86. def path(self):
  87. path = []
  88. x = self.lastpoint
  89. start = self.x0
  90. while x != start:
  91. path.append([x, self.Parent[x]])
  92. x = self.Parent[x]
  93. # path = np.flip(path, axis=0)
  94. return path
  95. # utility used in LRTA*
  96. def reset(self, xj):
  97. self.g = g_Space(self) # key is the point, store g value
  98. self.start = xj
  99. self.g[getNearest(self.g, self.start)] = 0 # set g(x0) = 0
  100. self.x0 = xj
  101. self.OPEN.put(self.x0, self.g[self.x0] + heuristic_fun(self,self.x0)) # item, priority = g + h
  102. self.CLOSED = set()
  103. # self.h = h(self.Space, self.goal)
  104. if __name__ == '__main__':
  105. Astar = Weighted_A_star(0.5)
  106. sta = time.time()
  107. Astar.run()
  108. print(time.time() - sta)