Anytime_Dstar3D.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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.g = {} # all g initialized at inf
  31. self.h = {}
  32. self.rhs = {self.xt:0} # rhs(x0) = 0
  33. self.OPEN.put(self.xt, self.key(self.xt))
  34. self.INCONS = set()
  35. self.CLOSED = set()
  36. # init children set:
  37. self.CHILDREN = {}
  38. # init cost set
  39. self.COST = defaultdict(lambda: defaultdict(dict))
  40. # for visualization
  41. self.V = set() # vertice in closed
  42. self.ind = 0
  43. self.Path = []
  44. self.done = False
  45. # epsilon in the key caculation
  46. self.epsilon = 1
  47. self.increment = 0.1
  48. def getcost(self, xi, xj):
  49. # use a LUT for getting the costd
  50. if xi not in self.COST:
  51. for (xj,xjcost) in children(self, xi, settings=1):
  52. self.COST[xi][xj] = cost(self, xi, xj, xjcost)
  53. # this might happen when there is a node changed.
  54. if xj not in self.COST[xi]:
  55. self.COST[xi][xj] = cost(self, xi, xj)
  56. return self.COST[xi][xj]
  57. def getchildren(self, xi):
  58. if xi not in self.CHILDREN:
  59. allchild = children(self, xi)
  60. self.CHILDREN[xi] = set(allchild)
  61. return self.CHILDREN[xi]
  62. def geth(self, xi):
  63. # when the heurisitic is first calculated
  64. if xi not in self.h:
  65. self.h[xi] = heuristic_fun(self, xi, self.x0)
  66. return self.h[xi]
  67. def getg(self, xi):
  68. if xi not in self.g:
  69. self.g[xi] = np.inf
  70. return self.g[xi]
  71. def getrhs(self, xi):
  72. if xi not in self.rhs:
  73. self.rhs[xi] = np.inf
  74. return self.rhs[xi]
  75. def updatecost(self,range_changed=None, new=None, old=None, mode=False):
  76. # scan graph for changed cost, if cost is changed update it
  77. CHANGED = set()
  78. for xi in self.CLOSED:
  79. if xi in self.CHILDREN:
  80. oldchildren = self.CHILDREN[xi]# A
  81. if isinbound(old, xi, mode) or isinbound(new, xi, mode):
  82. newchildren = set(children(self,xi))# B
  83. removed = oldchildren.difference(newchildren)
  84. intersection = oldchildren.intersection(newchildren)
  85. added = newchildren.difference(oldchildren)
  86. for xj in removed:
  87. self.COST[xi][xj] = cost(self, xi, xj)
  88. for xj in intersection.union(added):
  89. self.COST[xi][xj] = cost(self, xi, xj)
  90. CHANGED.add(xi)
  91. else:
  92. if isinbound(old, xi, mode) or isinbound(new, xi, mode):
  93. CHANGED.add(xi)
  94. children_added = set(children(self,xi))
  95. self.CHILDREN[xi] = children_added
  96. for xj in children_added:
  97. self.COST[xi][xj] = cost(self, xi, xj)
  98. return CHANGED
  99. #--------------main functions for Anytime D star
  100. def key(self, s, epsilon=1):
  101. if self.getg(s) > self.getrhs(s):
  102. return [self.rhs[s] + epsilon * heuristic_fun(self, s, self.x0), self.rhs[s]]
  103. else:
  104. return [self.getg(s) + heuristic_fun(self, s, self.x0), self.getg(s)]
  105. def UpdateState(self, s):
  106. if s not in self.CLOSED:
  107. # TODO if s is not visited before
  108. self.g[s] = np.inf
  109. if s != self.xt:
  110. self.rhs[s] = min([self.getcost(s, s_p) + self.getg(s_p) for s_p in self.getchildren(s)])
  111. self.OPEN.check_remove(s)
  112. if self.getg(s) != self.getrhs(s):
  113. if s not in self.CLOSED:
  114. self.OPEN.put(s, self.key(s))
  115. else:
  116. self.INCONS.add(s)
  117. def ComputeorImprovePath(self):
  118. while self.OPEN.top_key() < self.key(self.x0) or self.rhs[self.x0] != self.g[self.x0]:
  119. s = self.OPEN.get()
  120. visualization(self)
  121. if self.g[s] > self.rhs[s]:
  122. self.g[s] = self.rhs[s]
  123. self.CLOSED.add(s)
  124. self.V.add(s)
  125. for s_p in self.getchildren(s):
  126. self.UpdateState(s_p)
  127. else:
  128. self.g[s] = np.inf
  129. self.UpdateState(s)
  130. for s_p in self.getchildren(s):
  131. self.UpdateState(s_p)
  132. self.ind += 1
  133. def Main(self):
  134. epsilon = self.epsilon
  135. increment = self.increment
  136. ischanged = False
  137. islargelychanged = False
  138. self.ComputeorImprovePath()
  139. #TODO publish current epsilon sub-optimal solution
  140. while True:
  141. # change environment
  142. new2,old2 = self.env.move_block(theta = [0,0,0.1*t], mode='rotation')
  143. ischanged = True
  144. # islargelychanged = True
  145. self.Path = []
  146. # update cost with changed environment
  147. if ischanged:
  148. CHANGED = self.updatecost(True, new2, old2, mode='obb')
  149. for u in CHANGED:
  150. self.UpdateState(u)
  151. self.ComputeorImprovePath()
  152. ischanged = False
  153. if islargelychanged:
  154. epsilon += increment # or replan from scratch
  155. elif epsilon > 1:
  156. epsilon -= increment
  157. # move states from the INCONS to OPEN
  158. # update priorities in OPEN
  159. Allnodes = self.INCONS.union(set(self.OPEN.enumerate()))
  160. for node in Allnodes:
  161. self.OPEN.put(node, self.key(node, epsilon))
  162. self.INCONS = set()
  163. self.CLOSED = set()
  164. self.ComputeorImprovePath()
  165. # publish current epsilon sub optimal solution
  166. # if epsilon == 1:
  167. # wait for change to occur
  168. pass
  169. if __name__ == '__main__':
  170. AD = Anytime_Dstar(resolution = 0.5)
  171. AD.Main()