Dstar3D.py 6.8 KB

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