D_star_Lite.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. """
  2. D_star_Lite 2D
  3. @author: huiming zhou
  4. """
  5. import os
  6. import sys
  7. import math
  8. import matplotlib.pyplot as plt
  9. sys.path.append(os.path.dirname(os.path.abspath(__file__)) +
  10. "/../../Search-based Planning/")
  11. from Search_2D import queue
  12. from Search_2D import plotting
  13. from Search_2D import env
  14. class DStarLite:
  15. def __init__(self, x_start, x_goal, heuristic_type):
  16. self.xI, self.xG = x_start, x_goal
  17. self.heuristic_type = heuristic_type
  18. self.Env = env.Env() # class Env
  19. self.u_set = self.Env.motions # feasible input set
  20. self.obs = self.Env.obs # position of obstacles
  21. self.U = queue.QueuePrior() # priority queue / U set
  22. self.g, self.rhs = {}, {}
  23. self.km = 0
  24. for i in range(self.Env.x_range):
  25. for j in range(self.Env.y_range):
  26. self.rhs[(i, j)] = float("inf")
  27. self.g[(i, j)] = float("inf")
  28. self.rhs[self.xG] = 0
  29. self.U.put(self.xG, self.CalculateKey(self.xG))
  30. def CalculateKey(self, s):
  31. return [min(self.g[s], self.rhs[s]) + self.h(self.xI, s) + self.km,
  32. min(self.g[s], self.rhs[s])]
  33. def h(self, s_start, s):
  34. heuristic_type = self.heuristic_type # heuristic type
  35. if heuristic_type == "manhattan":
  36. return abs(s[0] - s_start[0]) + abs(s[1] - s_start[1])
  37. else:
  38. return math.hypot(s[0] - s_start[0], s[1] - s_start[1])
  39. def UpdateVertex(self, s):
  40. if s != self.xG:
  41. def getNeighbor(self, s):
  42. v_list = set()
  43. for u in self.u_set:
  44. s_next = tuple([s[i] + u[i] for i in range(2)])
  45. if s_next not in self.obs:
  46. v_list.add(s_next)
  47. return v_list
  48. def getCost(self, s_start, s_end):