Anytime_Dstar3D.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # check paper of
  2. # [Likhachev2005]
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. import os
  6. import sys
  7. from collections import defaultdict
  8. sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../../Search-based Planning/")
  9. from Search_3D.env3D import env
  10. from Search_3D.utils3D import getDist, heuristic_fun, getNearest, isinbound, \
  11. cost, children, StateSpace
  12. from Search_3D.plot_util3D import visualization
  13. from Search_3D import queue
  14. import time
  15. class Anytime_Dstar(object):
  16. def __init__(self, resolution=1):
  17. self.Alldirec = {(1, 0, 0): 1, (0, 1, 0): 1, (0, 0, 1): 1, \
  18. (-1, 0, 0): 1, (0, -1, 0): 1, (0, 0, -1): 1, \
  19. (1, 1, 0): np.sqrt(2), (1, 0, 1): np.sqrt(2), (0, 1, 1): np.sqrt(2), \
  20. (-1, -1, 0): np.sqrt(2), (-1, 0, -1): np.sqrt(2), (0, -1, -1): np.sqrt(2), \
  21. (1, -1, 0): np.sqrt(2), (-1, 1, 0): np.sqrt(2), (1, 0, -1): np.sqrt(2), \
  22. (-1, 0, 1): np.sqrt(2), (0, 1, -1): np.sqrt(2), (0, -1, 1): np.sqrt(2), \
  23. (1, 1, 1): np.sqrt(3), (-1, -1, -1) : np.sqrt(3), \
  24. (1, -1, -1): np.sqrt(3), (-1, 1, -1): np.sqrt(3), (-1, -1, 1): np.sqrt(3), \
  25. (1, 1, -1): np.sqrt(3), (1, -1, 1): np.sqrt(3), (-1, 1, 1): np.sqrt(3)}
  26. self.env = env(resolution=resolution)
  27. self.settings = 'CollisionChecking' # for collision checking
  28. self.x0, self.xt = tuple(self.env.start), tuple(self.env.goal)
  29. self.OPEN = queue.MinheapPQ()
  30. self.km = 0
  31. self.g = {} # all g initialized at inf
  32. self.rhs = {self.xt:0} # rhs(x0) = 0
  33. self.h = {}
  34. self.OPEN.put(self.xt, self.key(self.xt))
  35. self.INCONS = set()
  36. self.CLOSED = set()
  37. # init children set:
  38. self.CHILDREN = {}
  39. # init cost set
  40. self.COST = defaultdict(lambda: defaultdict(dict))
  41. # for visualization
  42. self.V = set() # vertice in closed
  43. self.ind = 0
  44. self.Path = []
  45. self.done = False
  46. def getcost(self, xi, xj):
  47. # use a LUT for getting the costd
  48. if xi not in self.COST:
  49. for (xj,xjcost) in children(self, xi, settings=1):
  50. self.COST[xi][xj] = cost(self, xi, xj, xjcost)
  51. # this might happen when there is a node changed.
  52. if xj not in self.COST[xi]:
  53. self.COST[xi][xj] = cost(self, xi, xj)
  54. return self.COST[xi][xj]
  55. def getchildren(self, xi):
  56. if xi not in self.CHILDREN:
  57. allchild = children(self, xi)
  58. self.CHILDREN[xi] = set(allchild)
  59. return self.CHILDREN[xi]
  60. def geth(self, xi):
  61. # when the heurisitic is first calculated
  62. if xi not in self.h:
  63. self.h[xi] = heuristic_fun(self, xi, self.x0)
  64. return self.h[xi]
  65. def getg(self, xi):
  66. if xi not in self.g:
  67. self.g[xi] = np.inf
  68. return self.g[xi]
  69. def getrhs(self, xi):
  70. if xi not in self.rhs:
  71. self.rhs[xi] = np.inf
  72. return self.rhs[xi]
  73. #--------------main functions for Anytime D star
  74. def key(self, s, epsilon=1):
  75. if self.getg(s) > self.getrhs(s):
  76. return [self.rhs[s] + epsilon * heuristic_fun(self, s, self.x0), self.rhs[s]]
  77. else:
  78. return [self.getg(s) + heuristic_fun(self, s, self.x0), self.getg(s)]
  79. def UpdateState(self, s):
  80. if s not in self.CLOSED:
  81. # TODO if s is not visited before
  82. self.g[s] = np.inf
  83. if getDist(s, self.xt) <= self.env.resolution:
  84. self.rhs[s] = min([self.getcost(s, s_p) + self.getg(s_p) for s_p in self.getchildren(s)])
  85. self.OPEN.check_remove(s)
  86. if self.getg(s) != self.getrhs(s):
  87. if s not in self.CLOSED:
  88. self.OPEN.put(s, self.key(s))
  89. else:
  90. self.INCONS.add(s)
  91. def ComputeorImprovePath(self):
  92. pass
  93. def Main(self):
  94. pass
  95. if __name__ == '__main__':
  96. AD = Anytime_Dstar(resolution = 1)
  97. AD.Main()