DstarLite3D.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import os
  4. import sys
  5. from collections import defaultdict
  6. sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../../Search-based Planning/")
  7. from Search_3D.env3D import env
  8. from Search_3D import Astar3D
  9. from Search_3D.utils3D import getDist, getRay, g_Space, Heuristic, heuristic_fun, getNearest, isinbound, isinball, \
  10. cost, obstacleFree, children, StateSpace
  11. from Search_3D.plot_util3D import visualization
  12. import queue
  13. import pyrr
  14. import time
  15. class D_star_Lite(object):
  16. # Original version of the D*lite
  17. def __init__(self, resolution = 1):
  18. self.Alldirec = {(1, 0, 0): 1, (0, 1, 0): 1, (0, 0, 1): 1, \
  19. (-1, 0, 0): 1, (0, -1, 0): 1, (0, 0, -1): 1, \
  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, 0, -1): np.sqrt(2), (0, -1, -1): np.sqrt(2), \
  22. (1, -1, 0): np.sqrt(2), (-1, 1, 0): np.sqrt(2), (1, 0, -1): np.sqrt(2), \
  23. (-1, 0, 1): np.sqrt(2), (0, 1, -1): np.sqrt(2), (0, -1, 1): np.sqrt(2), \
  24. (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. (1, 1, -1): np.sqrt(3), (1, -1, 1): np.sqrt(3), (-1, 1, 1): np.sqrt(3)}
  27. self.env = env(resolution=resolution)
  28. # self.X = StateSpace(self.env)
  29. # self.x0, self.xt = getNearest(self.X, self.env.start), getNearest(self.X, self.env.goal)
  30. self.x0, self.xt = tuple(self.env.start), tuple(self.env.goal)
  31. self.OPEN = queue.QueuePrior()
  32. self.km = 0
  33. self.g = {} # all g initialized at inf
  34. self.rhs = {self.xt:0} # rhs(x0) = 0
  35. self.h = {}
  36. self.OPEN.put(self.xt, self.CalculateKey(self.xt))
  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 updatecost(self):
  56. # TODO: update cost when the environment is changed
  57. pass
  58. def getchildren(self, xi):
  59. if xi not in self.CHILDREN:
  60. allchild = children(self, xi)
  61. self.CHILDREN[xi] = set(allchild)
  62. return self.CHILDREN[xi]
  63. def updatechildren(self):
  64. # TODO: update children set when the environment is changed
  65. pass
  66. def geth(self, xi):
  67. # when the heurisitic is first calculated
  68. if xi not in self.h:
  69. self.h[xi] = heuristic_fun(self, xi, self.x0)
  70. return self.h[xi]
  71. def getg(self, xi):
  72. if xi not in self.g:
  73. self.g[xi] = np.inf
  74. return self.g[xi]
  75. def getrhs(self, xi):
  76. if xi not in self.rhs:
  77. self.rhs[xi] = np.inf
  78. return self.rhs[xi]
  79. #-------------main functions for D*Lite-------------
  80. def CalculateKey(self, s, epsilion = 1):
  81. return [min(self.getg(s), self.getrhs(s)) + epsilion * self.geth(s) + self.km, min(self.getg(s), self.getrhs(s))]
  82. def UpdateVertex(self, u):
  83. # if still in the hunt
  84. if not getDist(self.xt, u) <= self.env.resolution: # originally: u != s_goal
  85. self.rhs[u] = min([self.getcost(s, u) + self.getg(s) for s in self.getchildren(u)])
  86. # if u is in OPEN, remove it
  87. self.OPEN.check_remove(u)
  88. # if rhs(u) not equal to g(u)
  89. if self.getg(u) != self.getrhs(u):
  90. self.OPEN.put(u, self.CalculateKey(u))
  91. def ComputeShortestPath(self):
  92. while self.OPEN.top_key() < self.CalculateKey(self.x0) or self.getrhs(self.x0) != self.getg(self.x0) :
  93. kold = self.OPEN.top_key()
  94. u = self.OPEN.get()
  95. self.V.add(u)
  96. if getDist(self.x0, u) <= self.env.resolution:
  97. break
  98. # visualization(self)
  99. if kold < self.CalculateKey(u):
  100. self.OPEN.put(u, self.CalculateKey(u))
  101. if self.getg(u) > self.getrhs(u):
  102. self.g[u] = self.rhs[u]
  103. else:
  104. self.g[u] = np.inf
  105. self.UpdateVertex(u)
  106. for s in self.getchildren(u):
  107. self.UpdateVertex(s)
  108. self.ind += 1
  109. def main(self):
  110. s_last = self.x0
  111. s_start = self.x0
  112. self.ComputeShortestPath()
  113. # while s_start != self.xt:
  114. # while getDist(s_start, self.xt) > self.env.resolution:
  115. # newcost, allchild = [], []
  116. # for i in children(self, s_start):
  117. # newcost.append(cost(self, i, s_start) + self.g[s_start])
  118. # allchild.append(i)
  119. # s_start = allchild[np.argmin(newcost)]
  120. # #TODO: move to s_start
  121. # #TODO: scan graph or costs changes
  122. # # self.km = self.km + heuristic_fun(self, s_start, s_last)
  123. # # for all directed edges (u,v) with changed edge costs
  124. # # update edge cost c(u,v)
  125. # # updatevertex(u)
  126. # self.ComputeShortestPath()
  127. if __name__ == '__main__':
  128. a = time.time()
  129. D_lite = D_star_Lite(1)
  130. # D_lite.UpdateVertex(D_lite.x0)
  131. D_lite.main()
  132. print('used time (s) is ' + str(time.time() - a))