LRT_Astar3D.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.Astar3D import Weighted_A_star
  14. from Search_3D.utils3D import getAABB, getDist, getRay, StateSpace, Heuristic, getNearest, isCollide, hash3D, dehash, cost
  15. from Search_3D.plot_util3D import visualization
  16. import queue
  17. # class LRT_A_star1(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],[-1,-1,-1],\
  21. # [1,-1,0],[-1,1,0],[1,0,-1],[-1,0, 1],[0,1, -1],[0, -1,1],\
  22. # [1,-1,-1],[-1,1,-1],[-1,-1,1],[1,1,-1],[1,-1,1],[-1,1,1]])
  23. # self.env = env(resolution = resolution)
  24. # self.Space = StateSpace(self)
  25. # self.start, self.goal = getNearest(self.Space,self.env.start), getNearest(self.Space,self.env.goal)
  26. # self.AABB = getAABB(self.env.blocks)
  27. # self.Space[hash3D(getNearest(self.Space,self.start))] = 0 # this is g
  28. # self.OPEN = queue.QueuePrior()
  29. # self.h = Heuristic(self.Space,self.goal) # 1. initialize heuristic h = h0
  30. # self.Child = {}
  31. # self.CLOSED = set()
  32. # self.V = []
  33. # self.done = False
  34. # self.Path = []
  35. # def children(self,x):
  36. # allchild = []
  37. # for j in self.Alldirec:
  38. # collide,child = isCollide(self,x,j)
  39. # if not collide:
  40. # allchild.append(child)
  41. # return allchild
  42. # def step(self, xi, strxi):
  43. # childs = self.children(xi) # 4. generate depth 1 neighborhood S(s,1) = {s' in S | norm(s,s') = 1}
  44. # fvals = [cost(xi,i) + self.h[hash3D(i)] for i in childs]
  45. # xj , fmin = childs[np.argmin(fvals)], min(fvals) # 5. compute h'(s) = min(dist(s,s') + h(s'))
  46. # strxj = hash3D(xj)
  47. # # add the child of xi
  48. # self.Child[strxi] = xj
  49. # if fmin >= self.h[strxi]: # 6. if h'(s) > h(s) then update h(s) = h'(s)
  50. # self.h[strxi] = fmin
  51. # # TODO: action to move to xj
  52. # self.OPEN.put(strxj, self.h[strxj]) # 7. update current state s = argmin (dist(s,s') + h(s'))
  53. # def run(self):
  54. # x0 = hash3D(self.start)
  55. # xt = hash3D(self.goal)
  56. # self.OPEN.put(x0, self.Space[x0] + self.h[x0]) # 2. reset the current state
  57. # self.ind = 0
  58. # while xt not in self.CLOSED and self.OPEN: # 3. while s not in Sg do
  59. # strxi = self.OPEN.get()
  60. # xi = dehash(strxi)
  61. # self.CLOSED.add(strxi)
  62. # self.V.append(xi)
  63. # visualization(self)
  64. # if self.ind % 100 == 0: print('iteration number = '+ str(self.ind))
  65. # self.ind += 1
  66. # self.done = True
  67. # self.Path = self.path()
  68. # visualization(self)
  69. # plt.show()
  70. # def path(self):
  71. # # this is a suboptimal path.
  72. # path = []
  73. # strgoal = hash3D(self.goal)
  74. # strx = hash3D(self.start)
  75. # ind = 0
  76. # while strx != strgoal:
  77. # path.append([dehash(strx),self.Child[strx]])
  78. # strx = hash3D(self.Child[strx])
  79. # ind += 1
  80. # if ind == 1000:
  81. # return np.flip(path,axis=0)
  82. # path = np.flip(path,axis=0)
  83. # return path
  84. class LRT_A_star2():
  85. def __init__(self,resolution=0.5, N=7):
  86. self.lookahead = N
  87. self.Astar = Weighted_A_star()
  88. self.Astar.env.resolution = resolution
  89. def expand(self):
  90. self.Astar.run(self.lookahead)
  91. def updateHeuristic(self):
  92. for strxi in self.Astar.CLOSED:
  93. self.Astar.h[strxi] = np.inf
  94. xi = dehash(strxi)
  95. self.Astar.h[strxi] = min([cost(xi,xj) + self.Astar.h[hash3D(xj)] for xj in self.Astar.children(xi)])
  96. #def move(self):
  97. # print(np.argmin([j[0] for j in self.Astar.OPEN.enumerate()]))
  98. def run(self):
  99. xt = hash3D(self.Astar.goal)
  100. while xt not in self.Astar.CLOSED:
  101. self.expand()
  102. #self.updateHeuristic()
  103. if __name__ == '__main__':
  104. T = LRT_A_star2(resolution = 1, N = 2)
  105. T.run()