LRTA_star.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. """
  2. LRTA_star 2D
  3. @author: huiming zhou
  4. """
  5. import os
  6. import sys
  7. import matplotlib.pyplot as plt
  8. sys.path.append(os.path.dirname(os.path.abspath(__file__)) +
  9. "/../../Search-based Planning/")
  10. from Search_2D import queue
  11. from Search_2D import plotting
  12. from Search_2D import env
  13. class LrtAstar:
  14. def __init__(self, x_start, x_goal, heuristic_type):
  15. self.xI, self.xG = x_start, x_goal
  16. self.heuristic_type = heuristic_type
  17. self.Env = env.Env() # class Env
  18. self.u_set = self.Env.motions # feasible input set
  19. self.obs = self.Env.obs # position of obstacles
  20. self.g = {self.xI: 0, self.xG: float("inf")}
  21. self.OPEN = queue.QueuePrior() # priority queue / OPEN
  22. self.OPEN.put(self.xI, self.h(self.xI))
  23. self.CLOSED = set()
  24. self.Parent = {self.xI: self.xI}
  25. def searching(self):
  26. h = {self.xI: self.h(self.xI)}
  27. s = self.xI
  28. parent = {self.xI: self.xI}
  29. visited = []
  30. count = 0
  31. while s != self.xG:
  32. count += 1
  33. print(count)
  34. visited.append(s)
  35. h_list = {}
  36. for u in self.u_set:
  37. s_next = tuple([s[i] + u[i] for i in range(len(s))])
  38. if s_next not in self.obs:
  39. if s_next not in h:
  40. h[s_next] = self.h(s_next)
  41. h_list[s_next] = self.get_cost(s, s_next) + h[s_next]
  42. h_new = min(h_list.values())
  43. if h_new > h[s]:
  44. h[s] = h_new
  45. s_child = min(h_list, key=h_list.get)
  46. parent[s_child] = s
  47. s = s_child
  48. # path_get = self.extract_path(parent)
  49. return [], visited
  50. def extract_path(self, parent):
  51. path = [self.xG]
  52. s = self.xG
  53. while True:
  54. s = parent[s]
  55. path.append(s)
  56. if s == self.xI:
  57. break
  58. return path
  59. def h(self, s):
  60. heuristic_type = self.heuristic_type
  61. goal = self.xG
  62. if heuristic_type == "manhattan":
  63. return abs(goal[0] - s[0]) + abs(goal[1] - s[1])
  64. elif heuristic_type == "euclidean":
  65. return ((goal[0] - s[0]) ** 2 + (goal[1] - s[1]) ** 2) ** (1 / 2)
  66. else:
  67. print("Please choose right heuristic type!")
  68. @staticmethod
  69. def get_cost(x, u):
  70. """
  71. Calculate cost for this motion
  72. :param x: current node
  73. :param u: input
  74. :return: cost for this motion
  75. :note: cost function could be more complicate!
  76. """
  77. return 1
  78. def main():
  79. x_start = (10, 5) # Starting node
  80. x_goal = (45, 25) # Goal node
  81. lrtastar = LrtAstar(x_start, x_goal, "manhattan")
  82. plot = plotting.Plotting(x_start, x_goal) # class Plotting
  83. path, visited = lrtastar.searching()
  84. pathx = [x[0] for x in path]
  85. pathy = [x[1] for x in path]
  86. vx = [x[0] for x in visited]
  87. vy = [x[1] for x in visited]
  88. plot.plot_grid("test")
  89. plt.plot(pathx, pathy, 'r')
  90. plt.plot(vx, vy, 'gray')
  91. plt.show()
  92. if __name__ == '__main__':
  93. main()