Dstar3D.py 6.9 KB

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