Dstar3D.py 7.2 KB

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