Dstar3D.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 StateSpace, getDist, getNearest, getRay, isinbound, isinball, isCollide, children, cost, \
  10. initcost
  11. from Search_3D.plot_util3D import visualization
  12. class D_star(object):
  13. def __init__(self, resolution=1):
  14. self.Alldirec = {(1, 0, 0): 1, (0, 1, 0): 1, (0, 0, 1): 1, \
  15. (-1, 0, 0): 1, (0, -1, 0): 1, (0, 0, -1): 1, \
  16. (1, 1, 0): np.sqrt(2), (1, 0, 1): np.sqrt(2), (0, 1, 1): np.sqrt(2), \
  17. (-1, -1, 0): np.sqrt(2), (-1, 0, -1): np.sqrt(2), (0, -1, -1): np.sqrt(2), \
  18. (1, -1, 0): np.sqrt(2), (-1, 1, 0): np.sqrt(2), (1, 0, -1): np.sqrt(2), \
  19. (-1, 0, 1): np.sqrt(2), (0, 1, -1): np.sqrt(2), (0, -1, 1): np.sqrt(2), \
  20. (1, 1, 1): np.sqrt(3), (-1, -1, -1) : np.sqrt(3), \
  21. (1, -1, -1): np.sqrt(3), (-1, 1, -1): np.sqrt(3), (-1, -1, 1): np.sqrt(3), \
  22. (1, 1, -1): np.sqrt(3), (1, -1, 1): np.sqrt(3), (-1, 1, 1): np.sqrt(3)}
  23. self.env = env(resolution=resolution)
  24. self.X = StateSpace(self.env)
  25. self.x0, self.xt = getNearest(self.X, self.env.start), getNearest(self.X, self.env.goal)
  26. # self.x0, self.xt = tuple(self.env.start), tuple(self.env.goal)
  27. self.b = defaultdict(lambda: defaultdict(dict)) # back pointers every state has one except xt.
  28. self.OPEN = {} # OPEN list, here use a hashmap implementation. hash is point, key is value
  29. self.h = {} # estimate from a point to the end point
  30. self.tag = {} # set all states to new
  31. self.V = set() # vertice in closed
  32. # for visualization
  33. self.ind = 0
  34. self.Path = []
  35. self.done = False
  36. self.Obstaclemap = {}
  37. def checkState(self, y):
  38. if y not in self.h:
  39. self.h[y] = 0
  40. if y not in self.tag:
  41. self.tag[y] = 'New'
  42. def get_kmin(self):
  43. # get the minimum of the k val in OPEN
  44. # -1 if it does not exist
  45. if self.OPEN:
  46. return min(self.OPEN.values())
  47. return -1
  48. def min_state(self):
  49. # returns the state in OPEN with min k(.)
  50. # if empty, returns None and -1
  51. # it also removes this min value form the OPEN set.
  52. if self.OPEN:
  53. minvalue = min(self.OPEN.values())
  54. for k in self.OPEN.keys():
  55. if self.OPEN[k] == minvalue:
  56. return k, self.OPEN.pop(k)
  57. return None, -1
  58. def insert(self, x, h_new):
  59. # inserting a key and value into OPEN list (s, kx)
  60. # depending on following situations
  61. if self.tag[x] == 'New':
  62. kx = h_new
  63. if self.tag[x] == 'Open':
  64. kx = min(self.OPEN[x], h_new)
  65. if self.tag[x] == 'Closed':
  66. kx = min(self.h[x], h_new)
  67. self.OPEN[x] = kx
  68. self.h[x], self.tag[x] = h_new, 'Open'
  69. def process_state(self):
  70. # main function of the D star algorithm, perform the process state
  71. # around the old path when needed.
  72. x, kold = self.min_state()
  73. self.tag[x] = 'Closed'
  74. self.V.add(x)
  75. if x is None:
  76. return -1
  77. # check if 1st timer s
  78. self.checkState(x)
  79. if kold < self.h[x]: # raised states
  80. for y in children(self, x):
  81. # check y
  82. self.checkState(y)
  83. a = self.h[y] + cost(self, y, x)
  84. if self.h[y] <= kold and self.h[x] > a:
  85. self.b[x], self.h[x] = y, a
  86. if kold == self.h[x]: # lower
  87. for y in children(self, x):
  88. # check y
  89. self.checkState(y)
  90. bb = self.h[x] + cost(self, x, y)
  91. if self.tag[y] == 'New' or \
  92. (self.b[y] == x and self.h[y] != bb) or \
  93. (self.b[y] != x and self.h[y] > bb):
  94. self.b[y] = x
  95. self.insert(y, bb)
  96. else:
  97. for y in children(self, x):
  98. # check y
  99. self.checkState(y)
  100. bb = self.h[x] + cost(self, x, y)
  101. if self.tag[y] == 'New' or \
  102. (self.b[y] == x and self.h[y] != bb):
  103. self.b[y] = x
  104. self.insert(y, bb)
  105. else:
  106. if self.b[y] != x and self.h[y] > bb:
  107. self.insert(x, self.h[x])
  108. else:
  109. if self.b[y] != x and self.h[y] > bb and \
  110. self.tag[y] == 'Closed' and self.h[y] == kold:
  111. self.insert(y, self.h[y])
  112. return self.get_kmin()
  113. def modify_cost(self, x):
  114. xparent = self.b[x]
  115. if self.tag[x] == 'Closed':
  116. self.insert(x, self.h[xparent] + cost(self, x, xparent))
  117. def modify(self, x):
  118. self.modify_cost(x)
  119. while True:
  120. kmin = self.process_state()
  121. # visualization(self)
  122. if kmin >= self.h[x]:
  123. break
  124. def path(self, goal=None):
  125. path = []
  126. if not goal:
  127. x = self.x0
  128. else:
  129. x = goal
  130. start = self.xt
  131. while x != start:
  132. path.append([np.array(x), np.array(self.b[x])])
  133. x = self.b[x]
  134. return path
  135. def run(self):
  136. # put G (ending state) into the OPEN list
  137. self.OPEN[self.xt] = 0
  138. self.tag[self.x0] = 'New'
  139. # first run
  140. while True:
  141. # TODO: self.x0 =
  142. self.process_state()
  143. # visualization(self)
  144. if self.tag[self.x0] == "Closed":
  145. break
  146. self.ind += 1
  147. self.Path = self.path()
  148. self.done = True
  149. visualization(self)
  150. plt.pause(0.2)
  151. # plt.show()
  152. # when the environemnt changes over time
  153. for i in range(5):
  154. self.env.move_block(a=[0.1, 0, 0], s=0.5, block_to_move=1, mode='translation')
  155. self.env.move_block(a=[0, 0, -0.25], s=0.5, block_to_move=0, mode='translation')
  156. # travel from end to start
  157. s = tuple(self.env.start)
  158. # self.V = set()
  159. while s != self.xt:
  160. if s == tuple(self.env.start):
  161. sparent = self.b[self.x0]
  162. else:
  163. sparent = self.b[s]
  164. # if there is a change of Cost, or a collision.
  165. if cost(self, s, sparent) == np.inf:
  166. self.modify(s)
  167. continue
  168. self.ind += 1
  169. s = sparent
  170. self.Path = self.path()
  171. visualization(self)
  172. plt.show()
  173. if __name__ == '__main__':
  174. D = D_star(1)
  175. D.run()