Astar3D.py 4.4 KB

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