LRT_Astar3D.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # this is the three dimensional near-sighted 1 neighborhood 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 getAABB, getDist, getRay, StateSpace, Heuristic, getNearest, isCollide, hash3D, dehash, \
  15. cost, obstacleFree
  16. from Search_3D.plot_util3D import visualization
  17. import queue
  18. class LRT_A_star2:
  19. def __init__(self, resolution=0.5, N=7):
  20. self.N = N
  21. self.Astar = Astar3D.Weighted_A_star(resolution=resolution)
  22. self.path = []
  23. def children(self, x):
  24. allchild = []
  25. resolution = self.Astar.env.resolution
  26. for direc in self.Astar.Alldirec:
  27. child = np.array(list(map(np.add, x, np.multiply(direc, resolution))))
  28. allchild.append(hash3D(child))
  29. return allchild
  30. def updateHeuristic(self):
  31. # Initialize at infinity
  32. for strxi in self.Astar.CLOSED:
  33. self.Astar.h[strxi] = np.inf
  34. # initialize difference
  35. Diff = True
  36. while Diff: # repeat until converge
  37. hvals, lasthvals = [], []
  38. for strxi in self.Astar.CLOSED:
  39. xi = dehash(strxi)
  40. lasthvals.append(self.Astar.h[strxi])
  41. # update h values if they are smaller
  42. minfval = min([cost(xi, xj, settings=0) + self.Astar.h[hash3D(xj)] for xj in self.Astar.children(xi)])
  43. if self.Astar.h[strxi] >= minfval:
  44. self.Astar.h[strxi] = minfval
  45. hvals.append(self.Astar.h[strxi])
  46. if lasthvals == hvals: Diff = False
  47. def move(self):
  48. strst = self.Astar.x0
  49. st = self.Astar.start
  50. ind = 0
  51. while strst in self.Astar.CLOSED: # when minchild in CLOSED then continue, when minchild in OPEN, stop
  52. # strChildren = self.children(st)
  53. strChildren = [hash3D(i) for i in self.Astar.children(st)]
  54. minh, minchild = np.inf, None
  55. for child in strChildren:
  56. h = self.Astar.h[child]
  57. if h <= minh:
  58. minh, minchild = h, dehash(child)
  59. self.path.append([st, minchild])
  60. strst, st = hash3D(minchild), minchild
  61. for (_, strp) in self.Astar.OPEN.enumerate():
  62. if strp == strst:
  63. break
  64. ind += 1
  65. if ind > 1000:
  66. break
  67. self.Astar.reset(st)
  68. def run(self):
  69. while True:
  70. if self.Astar.run(N=self.N):
  71. self.Astar.Path = self.Astar.Path + self.path
  72. self.Astar.done = True
  73. visualization(self.Astar)
  74. plt.show()
  75. break
  76. self.updateHeuristic()
  77. self.move()
  78. if __name__ == '__main__':
  79. T = LRT_A_star2(resolution=0.5, N=1500)
  80. T.run()