Dstar3D.py 6.9 KB

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