Astar3D.py 4.6 KB

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