LRT_Astar3D.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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
  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.lookahead = N
  88. self.Astar = Astar3D.Weighted_A_star()
  89. while True:
  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. if __name__ == '__main__':
  99. T = LRT_A_star2(resolution=1, N=50)