Dstar3D.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. def initH(self):
  34. # h set, all initialzed h vals are 0 for all states.
  35. h = {}
  36. for xi in self.X:
  37. h[xi] = 0
  38. return h
  39. def initTag(self):
  40. # tag , New point (never been in the OPEN list)
  41. # Open point ( currently in OPEN )
  42. # Closed (currently in CLOSED)
  43. t = {}
  44. for xi in self.X:
  45. t[xi] = 'New'
  46. return t
  47. def get_kmin(self):
  48. # get the minimum of the k val in OPEN
  49. # -1 if it does not exist
  50. if self.OPEN:
  51. minv = np.inf
  52. for v, k in enumerate(self.OPEN):
  53. if v < minv: minv = v
  54. return minv
  55. return -1
  56. def min_state(self):
  57. # returns the state in OPEN with min k(.)
  58. # if empty, returns None and -1
  59. # it also removes this min value form the OPEN set.
  60. if self.OPEN:
  61. minv = np.inf
  62. for v, k in enumerate(self.OPEN):
  63. if v < minv: mink, minv = k, v
  64. return mink, self.OPEN.pop(mink)
  65. return None, -1
  66. def insert(self, x, h_new):
  67. # inserting a key and value into OPEN list (x, kx)
  68. # depending on following situations
  69. if self.tag[x] == 'New':
  70. kx = h_new
  71. if self.tag[x] == 'Open':
  72. kx = min(self.OPEN[x], h_new)
  73. if self.tag[x] == 'Closed':
  74. kx = min(self.h[x], h_new)
  75. self.OPEN[x] = kx
  76. self.h[x], self.tag[x] = h_new, 'Open'
  77. def process_state(self):
  78. x, kold = self.min_state()
  79. self.tag[x] = 'Closed'
  80. self.V.add(x)
  81. if x == None: return -1
  82. if kold < self.h[x]: # raised states
  83. for y in children(self, x):
  84. a = self.h[y] + cost(self, y, x)
  85. if self.h[y] <= kold and self.h[x] > a:
  86. self.b[x], self.h[x] = y, a
  87. elif kold == self.h[x]: # lower
  88. for y in children(self, x):
  89. bb = self.h[x] + cost(self, x, y)
  90. if self.tag[y] == 'New' or \
  91. (self.b[y] == x and self.h[y] != bb) or \
  92. (self.b[y] != x and self.h[y] > bb):
  93. self.b[y] = x
  94. self.insert(y, bb)
  95. else:
  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):
  100. self.b[y] = x
  101. self.insert(y, bb)
  102. else:
  103. if self.b[y] != x and self.h[y] > bb:
  104. self.insert(x, self.h[x])
  105. else:
  106. if self.b[y] != x and self.h[y] > bb and \
  107. self.tag[y] == 'Closed' and self.h[y] == kold:
  108. self.insert(y, self.h[y])
  109. return self.get_kmin()
  110. def modify_cost(self, x, y, cval):
  111. # TODO: implement own function
  112. # self.c[x][y] = cval
  113. # if self.tag[x] == 'Closed': self.insert(x,self.h[x])
  114. # return self.get_kmin()
  115. pass
  116. def modify(self, x):
  117. while True:
  118. kmin = self.process_state()
  119. if kmin >= self.h[x]: break
  120. def path(self, goal=None):
  121. path = []
  122. if not goal:
  123. x = self.x0
  124. else:
  125. x = goal
  126. start = self.xt
  127. while x != start:
  128. path.append([np.array(x), np.array(self.b[x])])
  129. x = self.b[x]
  130. return path
  131. def run(self):
  132. # put G (ending state) into the OPEN list
  133. self.OPEN[self.xt] = 0
  134. # first run
  135. while True:
  136. # TODO: self.x0 =
  137. self.process_state()
  138. visualization(self)
  139. if self.tag[self.x0] == "Closed":
  140. break
  141. self.ind += 1
  142. self.Path = self.path()
  143. self.done = True
  144. visualization(self)
  145. # plt.show()
  146. # when the environemnt changes over time
  147. s = tuple(self.env.start)
  148. while s != self.xt:
  149. if s == tuple(self.env.start):
  150. s = self.b[self.x0]
  151. else:
  152. s = self.b[s]
  153. # self.modify(s)
  154. self.env.move_block(a=[0, 0, -0.1], s=0.5, block_to_move=1, mode='translation')
  155. self.Path = self.path(s)
  156. visualization(self)
  157. self.ind += 1
  158. if __name__ == '__main__':
  159. D = D_star(1)
  160. D.run()