Dstar3D.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import os
  4. import sys
  5. sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../../Search-based Planning/")
  6. from Search_3D.env3D import env
  7. from Search_3D import Astar3D
  8. from Search_3D.utils3D import StateSpace, getDist, getNearest, getRay, isinbound, isinball, isCollide, children, cost, initcost
  9. import pyrr
  10. class D_star(object):
  11. def __init__(self,resolution = 1):
  12. 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],
  13. [-1, 0, 0], [0, -1, 0], [0, 0, -1], [-1, -1, 0], [-1, 0, -1], [0, -1, -1],
  14. [-1, -1, -1],
  15. [1, -1, 0], [-1, 1, 0], [1, 0, -1], [-1, 0, 1], [0, 1, -1], [0, -1, 1],
  16. [1, -1, -1], [-1, 1, -1], [-1, -1, 1], [1, 1, -1], [1, -1, 1], [-1, 1, 1]])
  17. self.env = env(resolution = resolution)
  18. self.X = StateSpace(self.env)
  19. self.x0, self.xt = getNearest(self.X, self.env.start), getNearest(self.X, self.env.goal)
  20. self.b = {} # back pointers every state has one except xt.
  21. self.OPEN = {} # OPEN list, here use a hashmap implementation. hash is point, key is value
  22. self.h = self.initH() # estimate from a point to the end point
  23. self.tag = self.initTag() # set all states to new
  24. # initialize cost set
  25. self.c = initcost(self)
  26. # put G (ending state) into the OPEN list
  27. self.OPEN[self.xt] = 0
  28. def initH(self):
  29. # h set, all initialzed h vals are 0 for all states.
  30. h = {}
  31. for xi in self.X:
  32. h[xi] = 0
  33. return h
  34. def initTag(self):
  35. # tag , New point (never been in the OPEN list)
  36. # Open point ( currently in OPEN )
  37. # Closed (currently in CLOSED)
  38. t = {}
  39. for xi in self.X:
  40. t[xi] = 'New'
  41. return t
  42. def get_kmin(self):
  43. # get the minimum of the k val in OPEN
  44. # -1 if it does not exist
  45. if self.OPEN:
  46. minv = np.inf
  47. for k,v in enumerate(self.OPEN):
  48. if v < minv: minv = v
  49. return minv
  50. return -1
  51. def min_state(self):
  52. # returns the state in OPEN with min k(.)
  53. # if empty, returns None and -1
  54. # it also removes this min value form the OPEN set.
  55. if self.OPEN:
  56. minv = np.inf
  57. for k,v in enumerate(self.OPEN):
  58. if v < minv: mink, minv = k, v
  59. return mink, self.OPEN.pop(mink)
  60. return None, -1
  61. def insert(self, x, h_new):
  62. # inserting a key and value into OPEN list (x, kx)
  63. # depending on following situations
  64. if self.tag[x] == 'New':
  65. kx = h_new
  66. if self.tag[x] == 'Open':
  67. kx = min(self.OPEN[x],h_new)
  68. if self.tag[x] == 'Closed':
  69. kx = min(self.h[x], h_new)
  70. self.OPEN[x] = kx
  71. self.h[x],self.tag[x] = h_new, 'Open'
  72. def process_state(self):
  73. x, kold = self.min_state()
  74. self.tag[x] = 'Closed'
  75. if x == None: return -1
  76. if kold < self.h[x]: # raised states
  77. for y in children(self,x):
  78. a = self.h[y] + self.c[y][x]
  79. if self.h[y] <= kold and self.h[x] > a:
  80. self.b[x], self.h[x] = y , a
  81. elif kold == self.h[x]:# lower
  82. for y in children(self,x):
  83. bb = self.h[x] + self.c[x][y]
  84. if self.tag[y] == 'New' or \
  85. (self.b[y] == x and self.h[y] != bb) or \
  86. (self.b[y] != x and self.h[y] > bb):
  87. self.b[y] = x
  88. self.insert(y, bb)
  89. else:
  90. for y in children(self,x):
  91. bb = self.h[x] + self.c[x][y]
  92. if self.tag[y] == 'New' or \
  93. (self.b[y] == x and self.h[y] != bb):
  94. self.b[y] = x
  95. self.insert(y, bb)
  96. else:
  97. if self.b[y] != x and self.h[y] > bb:
  98. self.insert(x, self.h[x])
  99. else:
  100. if self.b[y] != x and self.h[y] > bb and \
  101. self.tag[y] == 'Closed' and self.h[y] == kold:
  102. self.insert(y, self.h[y])
  103. return self.get_kmin()
  104. def modify_cost(self,x,y,cval):
  105. self.c[x][y] = cval # set the new cost to the cval
  106. if self.tag[x] == 'Closed': self.insert(x,self.h[x])
  107. return self.get_kmin()
  108. def run(self):
  109. # TODO: implementation of changing obstable in process
  110. pass
  111. if __name__ == '__main__':
  112. D = D_star(1)