LRT_Astar3D.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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_star1(object):
  19. # def __init__(self,resolution=0.5):
  20. # 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],\
  21. # [-1,0,0],[0,-1,0],[0,0,-1],[-1,-1,0],[-1,0,-1],[0,-1,-1],[-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)
  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 # this is g
  29. # self.OPEN = queue.QueuePrior()
  30. # self.h = Heuristic(self.Space,self.goal) # 1. initialize heuristic h = h0
  31. # self.Child = {}
  32. # self.CLOSED = set()
  33. # self.V = []
  34. # self.done = False
  35. # self.Path = []
  36. # def children(self,x):
  37. # allchild = []
  38. # for j in self.Alldirec:
  39. # collide,child = isCollide(self,x,j)
  40. # if not collide:
  41. # allchild.append(child)
  42. # return allchild
  43. # def step(self, xi, strxi):
  44. # childs = self.children(xi) # 4. generate depth 1 neighborhood S(s,1) = {s' in S | norm(s,s') = 1}
  45. # fvals = [cost(xi,i) + self.h[hash3D(i)] for i in childs]
  46. # xj , fmin = childs[np.argmin(fvals)], min(fvals) # 5. compute h'(s) = min(dist(s,s') + h(s'))
  47. # strxj = hash3D(xj)
  48. # # add the child of xi
  49. # self.Child[strxi] = xj
  50. # if fmin >= self.h[strxi]: # 6. if h'(s) > h(s) then update h(s) = h'(s)
  51. # self.h[strxi] = fmin
  52. # # TODO: action to move to xj
  53. # self.OPEN.put(strxj, self.h[strxj]) # 7. update current state s = argmin (dist(s,s') + h(s'))
  54. # def run(self):
  55. # x0 = hash3D(self.start)
  56. # xt = hash3D(self.goal)
  57. # self.OPEN.put(x0, self.Space[x0] + self.h[x0]) # 2. reset the current state
  58. # self.ind = 0
  59. # while xt not in self.CLOSED and self.OPEN: # 3. while s not in Sg do
  60. # strxi = self.OPEN.get()
  61. # xi = dehash(strxi)
  62. # self.CLOSED.add(strxi)
  63. # self.V.append(xi)
  64. # visualization(self)
  65. # if self.ind % 100 == 0: print('iteration number = '+ str(self.ind))
  66. # self.ind += 1
  67. # self.done = True
  68. # self.Path = self.path()
  69. # visualization(self)
  70. # plt.show()
  71. # def path(self):
  72. # # this is a suboptimal path.
  73. # path = []
  74. # strgoal = hash3D(self.goal)
  75. # strx = hash3D(self.start)
  76. # ind = 0
  77. # while strx != strgoal:
  78. # path.append([dehash(strx),self.Child[strx]])
  79. # strx = hash3D(self.Child[strx])
  80. # ind += 1
  81. # if ind == 1000:
  82. # return np.flip(path,axis=0)
  83. # path = np.flip(path,axis=0)
  84. # return path
  85. class LRT_A_star2:
  86. def __init__(self, resolution=0.5, N=7):
  87. self.N = N
  88. self.Astar = Astar3D.Weighted_A_star(resolution=resolution)
  89. self.path = []
  90. def children(self, x):
  91. allchild = []
  92. resolution = self.Astar.env.resolution
  93. for direc in self.Astar.Alldirec:
  94. child = np.array(list(map(np.add,x,np.multiply(direc,resolution))))
  95. allchild.append(hash3D(child))
  96. return allchild
  97. def updateHeuristic(self):
  98. for strxi in self.Astar.CLOSED:
  99. self.Astar.h[strxi] = np.inf
  100. xi = dehash(strxi)
  101. minfval = min([cost(xi, xj, settings=1) + self.Astar.h[hash3D(xj)] for xj in self.Astar.children(xi)])
  102. if self.Astar.h[strxi] >= minfval:
  103. self.Astar.h[strxi] = minfval
  104. def move(self):
  105. #self.Astar.Parent[xj] = dehash(self.Astar.lastpoint)
  106. strst = self.Astar.x0
  107. st = self.Astar.start
  108. ind = 0
  109. while strst in self.Astar.CLOSED: # when minchild in CLOSED then continue, when minchild in OPEN, stop
  110. # strChildren = self.children(st)
  111. strChildren = [hash3D(i) for i in self.Astar.children(st)]
  112. minh , minchild = np.inf , None
  113. for child in strChildren:
  114. h = self.Astar.h[child]
  115. if h <= minh:
  116. minh , minchild = h , dehash(child)
  117. self.path.append([st,minchild])
  118. strst, st = hash3D(minchild), minchild
  119. for (_,strp) in self.Astar.OPEN.enumerate():
  120. if strp == strst:
  121. break
  122. ind += 1
  123. if ind > 1000:
  124. break
  125. self.Astar.reset(st)
  126. def run(self):
  127. while self.Astar.lastpoint != hash3D(self.Astar.goal):
  128. self.Astar.run(N=self.N)
  129. #print(path)
  130. visualization(self.Astar)
  131. self.updateHeuristic()
  132. self.move()
  133. self.Astar.Path = self.path
  134. self.Astar.done = True
  135. visualization(self.Astar)
  136. plt.show()
  137. if __name__ == '__main__':
  138. T = LRT_A_star2(resolution=1, N=100)
  139. T.run()