LRT_Astar3D.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 hvalues at infinity
  32. for strxi in self.Astar.CLOSED:
  33. self.Astar.h[strxi] = np.inf
  34. Diff = True
  35. while Diff: # repeat DP until converge
  36. hvals, lasthvals = [], []
  37. for strxi in self.Astar.CLOSED:
  38. xi = dehash(strxi)
  39. lasthvals.append(self.Astar.h[strxi])
  40. # update h values if they are smaller
  41. Children = self.Astar.children(xi)
  42. minfval = min([cost(xi, xj, settings=0) + self.Astar.h[hash3D(xj)] for xj in Children])
  43. # h(s) = h(s') if h(s) > c(s,s') + h(s')
  44. if self.Astar.h[strxi] >= minfval:
  45. self.Astar.h[strxi] = minfval
  46. hvals.append(self.Astar.h[strxi])
  47. if lasthvals == hvals: Diff = False
  48. def move(self):
  49. strst = self.Astar.x0
  50. st = self.Astar.start
  51. ind = 0
  52. # find the lowest path down hill
  53. while strst in self.Astar.CLOSED: # when minchild in CLOSED then continue, when minchild in U, stop
  54. # strChildren = self.children(st)
  55. strChildren = [hash3D(i) for i in self.Astar.children(st)]
  56. minh, minchild = np.inf, None
  57. for child in strChildren:
  58. h = self.Astar.h[child]
  59. if h <= minh:
  60. minh, minchild = h, dehash(child)
  61. self.path.append([st, minchild])
  62. strst, st = hash3D(minchild), minchild
  63. for (_, strp) in self.Astar.OPEN.enumerate():
  64. if strp == strst:
  65. break
  66. ind += 1
  67. if ind > 1000:
  68. break
  69. self.Astar.reset(st)
  70. def run(self):
  71. while True:
  72. if self.Astar.run(N=self.N):
  73. self.Astar.Path = self.Astar.Path + self.path
  74. self.Astar.done = True
  75. visualization(self.Astar)
  76. plt.show()
  77. break
  78. self.updateHeuristic()
  79. self.move()
  80. if __name__ == '__main__':
  81. T = LRT_A_star2(resolution=1, N=150)
  82. T.run()