Astar3D.py 4.4 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 getAABB, getDist, getRay, StateSpace, Heuristic, getNearest, isCollide, hash3D, dehash, \
  14. cost
  15. from Search_3D.plot_util3D import visualization
  16. import queue
  17. class Weighted_A_star(object):
  18. def __init__(self, resolution=0.5):
  19. 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],
  20. [-1, 0, 0], [0, -1, 0], [0, 0, -1], [-1, -1, 0], [-1, 0, -1], [0, -1, -1],
  21. [-1, -1, -1],
  22. [1, -1, 0], [-1, 1, 0], [1, 0, -1], [-1, 0, 1], [0, 1, -1], [0, -1, 1],
  23. [1, -1, -1], [-1, 1, -1], [-1, -1, 1], [1, 1, -1], [1, -1, 1], [-1, 1, 1]])
  24. self.env = env(resolution=resolution)
  25. self.Space = StateSpace(self) # key is the point, store g value
  26. self.start, self.goal = getNearest(self.Space, self.env.start), getNearest(self.Space, self.env.goal)
  27. self.AABB = getAABB(self.env.blocks)
  28. self.Space[hash3D(getNearest(self.Space, self.start))] = 0 # set g(x0) = 0
  29. self.h = Heuristic(self.Space, self.goal)
  30. self.Parent = {}
  31. self.CLOSED = set()
  32. self.V = []
  33. self.done = False
  34. self.Path = []
  35. self.ind = 0
  36. self.x0, self.xt = hash3D(self.start), hash3D(self.goal)
  37. self.OPEN = queue.QueuePrior() # store [point,priority]
  38. self.OPEN.put(self.x0, self.Space[self.x0] + self.h[self.x0]) # item, priority = g + h
  39. self.lastpoint = self.x0
  40. def children(self, x):
  41. allchild = []
  42. for j in self.Alldirec:
  43. collide, child = isCollide(self, x, j)
  44. if not collide:
  45. allchild.append(child)
  46. return allchild
  47. def run(self, N=None):
  48. xt = self.xt
  49. while xt not in self.CLOSED and self.OPEN: # while xt not reached and open is not empty
  50. strxi = self.OPEN.get()
  51. xi = dehash(strxi)
  52. if strxi not in self.CLOSED:
  53. self.V.append(xi)
  54. self.CLOSED.add(strxi) # add the point in CLOSED set
  55. visualization(self)
  56. allchild = self.children(xi)
  57. for xj in allchild:
  58. strxj = hash3D(xj)
  59. if strxj not in self.CLOSED:
  60. gi, gj = self.Space[strxi], self.Space[strxj]
  61. a = gi + cost(xi, xj)
  62. if a < gj:
  63. self.Space[strxj] = a
  64. self.Parent[strxj] = xi
  65. if (a, strxj) in self.OPEN.enumerate():
  66. # update priority of xj
  67. self.OPEN.put(strxj, a + 1 * self.h[strxj])
  68. else:
  69. # add xj in to OPEN set
  70. self.OPEN.put(strxj, a + 1 * self.h[strxj])
  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 = strxi
  78. # if the path finding is finished
  79. if xt in self.CLOSED and N is None:
  80. self.done = True
  81. self.Path = self.path()
  82. visualization(self)
  83. plt.show()
  84. def path(self):
  85. path = []
  86. strx = self.lastpoint
  87. #strstart = hash3D(getNearest(self.Space, self.env.start))
  88. strstart = self.x0
  89. while strx != strstart:
  90. path.append([dehash(strx), self.Parent[strx]])
  91. strx = hash3D(self.Parent[strx])
  92. path = np.flip(path, axis=0)
  93. return path
  94. # utility used in LRTA*
  95. def reset(self,xj):
  96. self.Space = StateSpace(self) # key is the point, store g value
  97. self.start = xj
  98. self.Space[hash3D(getNearest(self.Space, self.start))] = 0 # set g(x0) = 0
  99. self.x0 = hash3D(xj)
  100. self.OPEN.put(self.x0, self.Space[self.x0] + self.h[self.x0]) # item, priority = g + h
  101. self.CLOSED = set()
  102. if __name__ == '__main__':
  103. Astar = Weighted_A_star(0.5)
  104. Astar.run()