Astar3D.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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
  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 = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 1, 0], [1, 0, 1], [0, 1, 1], [1, 1, 1],
  21. [-1, 0, 0], [0, -1, 0], [0, 0, -1], [-1, -1, 0], [-1, 0, -1], [0, -1, -1],
  22. [-1, -1, -1],
  23. [1, -1, 0], [-1, 1, 0], [1, 0, -1], [-1, 0, 1], [0, 1, -1], [0, -1, 1],
  24. [1, -1, -1], [-1, 1, -1], [-1, -1, 1], [1, 1, -1], [1, -1, 1], [-1, 1, 1]])
  25. self.env = env(resolution=resolution)
  26. self.g = g_Space(self) # key is the point, store g value
  27. self.start, self.goal = getNearest(self.g, self.env.start), getNearest(self.g, self.env.goal)
  28. # self.AABB = getAABB(self.env.blocks)
  29. self.g[getNearest(self.g, self.start)] = 0 # set g(x0) = 0
  30. self.h = Heuristic(self.g, self.goal)
  31. self.Parent = {}
  32. self.CLOSED = set()
  33. self.V = []
  34. self.done = False
  35. self.Path = []
  36. self.ind = 0
  37. self.x0, self.xt = self.start, self.goal
  38. self.OPEN = queue.QueuePrior() # store [point,priority]
  39. self.OPEN.put(self.x0, self.g[self.x0] + self.h[self.x0]) # item, priority = g + h
  40. self.lastpoint = self.x0
  41. def children(self, x):
  42. allchild = []
  43. for j in self.Alldirec:
  44. collide, child = isCollide(self, x, j)
  45. if not collide:
  46. allchild.append(child)
  47. return allchild
  48. def run(self, N=None):
  49. xt = self.xt
  50. xi = self.x0
  51. while xt not in self.CLOSED and self.OPEN: # while xt not reached and open is not empty
  52. xi = self.OPEN.get()
  53. if xi not in self.CLOSED:
  54. self.V.append(np.array(xi))
  55. self.CLOSED.add(xi) # add the point in CLOSED set
  56. visualization(self)
  57. allchild = self.children(xi)
  58. for xj in allchild:
  59. if xj not in self.CLOSED:
  60. gi, gj = self.g[xi], self.g[xj]
  61. a = gi + cost(xi, xj)
  62. if a < gj:
  63. self.g[xj] = a
  64. self.Parent[xj] = xi
  65. if (a, xj) in self.OPEN.enumerate():
  66. # update priority of xj
  67. self.OPEN.put(xj, a + 1 * self.h[xj])
  68. else:
  69. # add xj in to OPEN set
  70. self.OPEN.put(xj, a + 1 * self.h[xj])
  71. # For specified expanded nodes, used primarily in LRTA*
  72. if N:
  73. if len(self.CLOSED) % N == 0:
  74. break
  75. if self.ind % 100 == 0: print('number node expanded = ' + str(len(self.V)))
  76. self.ind += 1
  77. self.lastpoint = xi
  78. # if the path finding is finished
  79. if xt in self.CLOSED:
  80. self.done = True
  81. self.Path = self.path()
  82. if N is None:
  83. visualization(self)
  84. plt.show()
  85. return True
  86. return False
  87. def path(self):
  88. path = []
  89. x = self.lastpoint
  90. start = self.x0
  91. while x != start:
  92. path.append([x, self.Parent[x]])
  93. x = self.Parent[x]
  94. # path = np.flip(path, axis=0)
  95. return path
  96. # utility used in LRTA*
  97. def reset(self, xj):
  98. self.g = g_Space(self) # key is the point, store g value
  99. self.start = xj
  100. self.g[getNearest(self.g, self.start)] = 0 # set g(x0) = 0
  101. self.x0 = xj
  102. self.OPEN.put(self.x0, self.g[self.x0] + self.h[self.x0]) # item, priority = g + h
  103. self.CLOSED = set()
  104. # self.h = h(self.Space, self.goal)
  105. if __name__ == '__main__':
  106. sta = time.time()
  107. Astar = Weighted_A_star(1)
  108. Astar.run()
  109. print(time.time() - sta)