Astar3D.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. def children(self, x):
  40. allchild = []
  41. for j in self.Alldirec:
  42. collide, child = isCollide(self, x, j)
  43. if not collide:
  44. allchild.append(child)
  45. return allchild
  46. def run(self, N=None):
  47. xt = self.xt
  48. while xt not in self.CLOSED and self.OPEN: # while xt not reached and open is not empty
  49. strxi = self.OPEN.get()
  50. xi = dehash(strxi)
  51. self.CLOSED.add(strxi) # add the point in CLOSED set
  52. self.V.append(xi)
  53. visualization(self)
  54. allchild = self.children(xi)
  55. for xj in allchild:
  56. strxj = hash3D(xj)
  57. if strxj not in self.CLOSED:
  58. gi, gj = self.Space[strxi], self.Space[strxj]
  59. a = gi + cost(xi, xj)
  60. if a < gj:
  61. self.Space[strxj] = a
  62. self.Parent[strxj] = xi
  63. if (a, strxj) in self.OPEN.enumerate():
  64. # update priority of xj
  65. self.OPEN.put(strxj, a + 1 * self.h[strxj])
  66. else:
  67. # add xj in to OPEN set
  68. self.OPEN.put(strxj, a + 1 * self.h[strxj])
  69. # For specified expanded nodes, used primarily in LRTA*
  70. if N:
  71. if len(self.CLOSED) % N == 0:
  72. break
  73. if self.ind % 100 == 0: print('iteration number = ' + str(self.ind))
  74. self.ind += 1
  75. # if the path finding is finished
  76. if xt in self.CLOSED:
  77. self.done = True
  78. self.Path = self.path()
  79. visualization(self)
  80. plt.show()
  81. def path(self):
  82. path = []
  83. strx = hash3D(self.goal)
  84. strstart = hash3D(self.start)
  85. while strx != strstart:
  86. path.append([dehash(strx), self.Parent[strx]])
  87. strx = hash3D(self.Parent[strx])
  88. path = np.flip(path, axis=0)
  89. return path
  90. if __name__ == '__main__':
  91. Astar = Weighted_A_star(1)
  92. Astar.run()