Astar3D.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 os
  9. import sys
  10. sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../../Search-based Planning/")
  11. from Search_3D.env3D import env
  12. from Search_3D.utils3D import getAABB, getDist, getRay, StateSpace, Heuristic, getNearest, isCollide, hash3D, dehash, cost
  13. from Search_3D.plot_util3D import visualization
  14. import queue
  15. class Weighted_A_star(object):
  16. def __init__(self,resolution=0.2):
  17. 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],\
  18. [-1,0,0],[0,-1,0],[0,0,-1],[-1,-1,0],[-1,0,-1],[0,-1,-1],[-1,-1,-1],\
  19. [1,-1,0],[-1,1,0],[1,0,-1],[-1,0, 1],[0,1, -1],[0, -1,1],\
  20. [1,-1,-1],[-1,1,-1],[-1,-1,1],[1,1,-1],[1,-1,1],[-1,1,1]])
  21. self.env = env(resolution = resolution)
  22. self.Space = StateSpace(self) # key is the point, store g value
  23. self.start = getNearest(self.Space,self.env.start)
  24. self.goal = getNearest(self.Space,self.env.goal)
  25. self.Space[hash3D(getNearest(self.Space,self.start))] = 0 # set g(x0) = 0
  26. self.OPEN = queue.QueuePrior() # store [point,priority]
  27. self.h = Heuristic(self.Space,self.goal)
  28. self.Parent = {}
  29. self.CLOSED = {}
  30. self.V = []
  31. self.done = False
  32. self.Path = []
  33. def children(self,x):
  34. allchild = []
  35. for j in self.Alldirec:
  36. collide,child = isCollide(self,x,j)
  37. if not collide:
  38. allchild.append(child)
  39. return allchild
  40. def run(self):
  41. x0 = hash3D(self.start)
  42. xt = hash3D(self.goal)
  43. self.OPEN.put(x0,self.Space[x0] + self.h[x0]) # item, priority = g + h
  44. self.ind = 0
  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.V.append(xi)
  49. visualization(self)
  50. self.CLOSED[strxi] = [] # add the point in CLOSED set
  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 strxj in self.OPEN.enumerate():
  61. #TODO: update priority of xj
  62. # self.OPEN.put(strxj, a+1*self.h[strxj])
  63. pass
  64. else:
  65. #TODO: add xj in to OPEN set
  66. self.OPEN.put(strxj, a+1*self.h[strxj])
  67. if self.ind % 100 == 0: print('iteration number = '+ str(self.ind))
  68. self.ind += 1
  69. self.done = True
  70. #self.Path = self.path()
  71. #visualization(self)
  72. def path(self):
  73. path = [self.goal]
  74. strx = hash3D(self.goal)
  75. strstart = hash3D(self.start)
  76. while strx != strstart:
  77. path.append(self.Parent[strx])
  78. strx = hash3D(self.Parent[strx])
  79. path = np.array(np.flip(path,axis=0))
  80. return path
  81. if __name__ == '__main__':
  82. Astar = Weighted_A_star(1)
  83. Astar.run()
  84. PATH = Astar.path()
  85. print(PATH)