Dstar3D.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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([x for x in 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. mink = min(self.OPEN, key=self.OPEN.get)
  69. minv = self.OPEN[mink]
  70. _ = self.OPEN.pop(mink)
  71. # #
  72. # mink = -1
  73. # minv = np.inf
  74. # for v, k in enumerate(self.OPEN):
  75. # if v < minv:
  76. # mink, minv = k, v
  77. # return mink, self.OPEN.pop(mink)
  78. return mink, minv
  79. return None, -1
  80. def insert(self, x, h_new):
  81. # inserting a key and value into OPEN list (x, kx)
  82. # depending on following situations
  83. if self.tag[x] == 'New':
  84. kx = h_new
  85. if self.tag[x] == 'Open':
  86. kx = min(self.OPEN[x], h_new)
  87. if self.tag[x] == 'Closed':
  88. kx = min(self.h[x], h_new)
  89. self.OPEN[x] = kx
  90. self.h[x], self.tag[x] = h_new, 'Open'
  91. def process_state(self):
  92. x, kold = self.min_state()
  93. self.tag[x] = 'Closed'
  94. self.V.add(x)
  95. if x is None:
  96. return -1
  97. if kold < self.h[x]: # raised states
  98. for y in children(self, x):
  99. a = self.h[y] + cost(self, y, x)
  100. if self.h[y] <= kold and self.h[x] > a:
  101. self.b[x], self.h[x] = y, a
  102. if kold == self.h[x]: # lower
  103. for y in children(self, x):
  104. bb = self.h[x] + cost(self, x, y)
  105. if self.tag[y] == 'New' or \
  106. (self.b[y] == x and self.h[y] != bb) or \
  107. (self.b[y] != x and self.h[y] > bb):
  108. self.b[y] = x
  109. self.insert(y, bb)
  110. else:
  111. for y in children(self, x):
  112. bb = self.h[x] + cost(self, x, y)
  113. if self.tag[y] == 'New' or \
  114. (self.b[y] == x and self.h[y] != bb):
  115. self.b[y] = x
  116. self.insert(y, bb)
  117. else:
  118. if self.b[y] != x and self.h[y] > bb:
  119. self.insert(x, self.h[x])
  120. else:
  121. if self.b[y] != x and self.h[y] > bb and \
  122. self.tag[y] == 'Closed' and self.h[y] == kold:
  123. self.insert(y, self.h[y])
  124. return self.get_kmin()
  125. def modify_cost(self, x):
  126. # TODO: implement own function
  127. # self.c[x][y] = cval
  128. xparent = self.b[x]
  129. if self.tag[x] == 'Closed':
  130. self.insert(x, self.h[xparent] + cost(self, x, xparent))
  131. def modify(self, x):
  132. self.modify_cost(x)
  133. while True:
  134. kmin = self.process_state()
  135. if kmin >= self.h[x]:
  136. break
  137. def path(self, goal=None):
  138. path = []
  139. if not goal:
  140. x = self.x0
  141. else:
  142. x = goal
  143. start = self.xt
  144. while x != start:
  145. path.append([np.array(x), np.array(self.b[x])])
  146. x = self.b[x]
  147. return path
  148. def run(self):
  149. # put G (ending state) into the OPEN list
  150. self.OPEN[self.xt] = 0
  151. # first run
  152. while True:
  153. # TODO: self.x0 =
  154. self.process_state()
  155. visualization(self)
  156. if self.tag[self.x0] == "Closed":
  157. break
  158. self.ind += 1
  159. self.Path = self.path()
  160. self.done = True
  161. visualization(self)
  162. plt.pause(0.2)
  163. # plt.show()
  164. # when the environemnt changes over time
  165. for i in range(2):
  166. self.env.move_block(a=[0, 0, -1], s=0.5, block_to_move=1, mode='translation')
  167. visualization(self)
  168. s = tuple(self.env.start)
  169. while s != self.xt:
  170. if s == tuple(self.env.start):
  171. sparent = self.b[self.x0]
  172. else:
  173. sparent = self.b[s]
  174. # self.update_obs()
  175. if cost(self, s, sparent) == np.inf:
  176. # print(s, " ", sparent)
  177. self.modify(s)
  178. continue
  179. self.ind += 1
  180. s = sparent
  181. self.Path = self.path()
  182. visualization(self)
  183. plt.show()
  184. if __name__ == '__main__':
  185. D = D_star(1)
  186. D.run()