Astar3D.py 3.7 KB

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