RTA_Astar3D.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # this is the three dimensional Real-time Adaptive LRTA* 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 import Astar3D
  14. from Search_3D.utils3D import getDist, getRay, g_Space, Heuristic, getNearest, isCollide, \
  15. cost, obstacleFree, children
  16. from Search_3D.plot_util3D import visualization
  17. import queue
  18. class RTA_A_star:
  19. def __init__(self, resolution=0.5, N=7):
  20. self.N = N # node to expand
  21. self.Astar = Astar3D.Weighted_A_star(resolution=resolution) # initialize A star
  22. self.path = [] # empty path
  23. self.st = []
  24. self.localhvals = []
  25. def updateHeuristic(self):
  26. # Initialize hvalues at infinity
  27. self.localhvals = []
  28. nodeset, vals = [], []
  29. for (_,_,xi) in self.Astar.OPEN.enumerate():
  30. nodeset.append(xi)
  31. vals.append(self.Astar.g[xi] + self.Astar.h[xi])
  32. j, fj = nodeset[np.argmin(vals)], min(vals)
  33. self.st = j
  34. # single pass update of hvals
  35. for xi in self.Astar.CLOSED:
  36. self.Astar.h[xi] = fj - self.Astar.g[xi]
  37. self.localhvals.append(self.Astar.h[xi])
  38. def move(self):
  39. st, localhvals = self.st, self.localhvals
  40. maxhval = max(localhvals)
  41. sthval = self.Astar.h[st]
  42. # find the lowest path up hill
  43. while sthval < maxhval:
  44. parentsvals , parents = [] , []
  45. # find the max child
  46. for xi in children(self.Astar,st):
  47. if xi in self.Astar.CLOSED:
  48. parents.append(xi)
  49. parentsvals.append(self.Astar.h[xi])
  50. lastst = st
  51. st = parents[np.argmax(parentsvals)]
  52. self.path.append([st,lastst]) # add to path
  53. sthval = self.Astar.h[st]
  54. self.Astar.reset(self.st)
  55. def run(self):
  56. while True:
  57. if self.Astar.run(N=self.N):
  58. self.Astar.Path = self.Astar.Path + self.path
  59. self.Astar.done = True
  60. visualization(self.Astar)
  61. plt.show()
  62. break
  63. self.updateHeuristic()
  64. self.move()
  65. if __name__ == '__main__':
  66. T = RTA_A_star(resolution=1, N=100)
  67. T.run()