LPAstar.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. """
  2. LPA_star 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 LpaStar:
  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()
  19. self.Plot = plotting.Plotting(x_start, x_goal)
  20. self.u_set = self.Env.motions
  21. self.obs = self.Env.obs
  22. self.x = self.Env.x_range
  23. self.y = self.Env.y_range
  24. self.U = queue.QueuePrior()
  25. self.g, self.rhs = {}, {}
  26. for i in range(self.Env.x_range):
  27. for j in range(self.Env.y_range):
  28. self.rhs[(i, j)] = float("inf")
  29. self.g[(i, j)] = float("inf")
  30. self.rhs[self.xI] = 0
  31. self.U.put(self.xI, self.Key(self.xI))
  32. self.fig = plt.figure()
  33. def run(self):
  34. self.Plot.plot_grid("Lifelong Planning A*")
  35. self.ComputePath()
  36. self.plot_path(self.extract_path_test())
  37. self.fig.canvas.mpl_connect('button_press_event', self.on_press)
  38. print("hahha")
  39. plt.show()
  40. def on_press(self, event):
  41. x, y = event.xdata, event.ydata
  42. if x < 0 or x > self.x - 1 or y < 0 or y > self.y - 1:
  43. print("Please choose right area!")
  44. else:
  45. x, y = int(x), int(y)
  46. print("Change position: x =", x, ",", "y =", y)
  47. if (x, y) not in self.obs:
  48. self.obs.add((x, y))
  49. plt.plot(x, y, 'sk')
  50. plt.pause(0.001)
  51. self.rhs[(x, y)] = float("inf")
  52. self.g[(x, y)] = float("inf")
  53. for node in self.get_neighbor((x, y)):
  54. self.UpdateVertex(node)
  55. else:
  56. self.obs.remove((x, y))
  57. plt.plot(x, y, marker='s', color='white')
  58. self.UpdateVertex((x, y))
  59. self.ComputePath()
  60. self.plot_path(self.extract_path_test())
  61. self.fig.canvas.draw_idle()
  62. @staticmethod
  63. def plot_path(path):
  64. px = [x[0] for x in path]
  65. py = [x[1] for x in path]
  66. plt.plot(px, py, marker='o')
  67. def ComputePath(self):
  68. while self.U.top_key() < self.Key(self.xG) or \
  69. self.rhs[self.xG] != self.g[self.xG]:
  70. s = self.U.get()
  71. if self.g[s] > self.rhs[s]:
  72. self.g[s] = self.rhs[s]
  73. else:
  74. self.g[s] = float("inf")
  75. self.UpdateVertex(s)
  76. for x in self.get_neighbor(s):
  77. self.UpdateVertex(x)
  78. def UpdateVertex(self, s):
  79. if s != self.xI:
  80. u_min = float("inf")
  81. for x in self.get_neighbor(s):
  82. u_min = min(u_min, self.g[x] + self.cost(x, s))
  83. self.rhs[s] = u_min
  84. self.U.remove(s)
  85. if self.g[s] != self.rhs[s]:
  86. self.U.put(s, self.Key(s))
  87. def get_neighbor(self, s):
  88. nei_list = set()
  89. for u in self.u_set:
  90. s_next = tuple([s[i] + u[i] for i in range(2)])
  91. if s_next not in self.obs:
  92. nei_list.add(s_next)
  93. return nei_list
  94. def Key(self, s):
  95. return [min(self.g[s], self.rhs[s]) + self.h(s),
  96. min(self.g[s], self.rhs[s])]
  97. def h(self, s):
  98. heuristic_type = self.heuristic_type # heuristic type
  99. goal = self.xG # goal node
  100. if heuristic_type == "manhattan":
  101. return abs(goal[0] - s[0]) + abs(goal[1] - s[1])
  102. else:
  103. return math.hypot(goal[0] - s[0], goal[1] - s[1])
  104. def cost(self, s_start, s_end):
  105. if s_start in self.obs or s_end in self.obs:
  106. return float("inf")
  107. return 1
  108. def extract_path(self):
  109. path = []
  110. s = self.xG
  111. while True:
  112. g_list = {}
  113. for x in self.get_neighbor(s):
  114. g_list[x] = self.g[x]
  115. s = min(g_list, key=g_list.get)
  116. if s == self.xI:
  117. return list(reversed(path))
  118. path.append(s)
  119. def extract_path_test(self):
  120. path = []
  121. s = self.xG
  122. for k in range(100):
  123. g_list = {}
  124. for x in self.get_neighbor(s):
  125. g_list[x] = self.g[x]
  126. s = min(g_list, key=g_list.get)
  127. if s == self.xI:
  128. return list(reversed(path))
  129. path.append(s)
  130. return list(reversed(path))
  131. def print_g(self):
  132. print("he")
  133. for k in range(self.Env.y_range):
  134. j = self.Env.y_range - k - 1
  135. string = ""
  136. for i in range(self.Env.x_range):
  137. if self.g[(i, j)] == float("inf"):
  138. string += ("00" + ', ')
  139. else:
  140. if self.g[(i, j)] // 10 == 0:
  141. string += ("0" + str(self.g[(i, j)]) + ', ')
  142. else:
  143. string += (str(self.g[(i, j)]) + ', ')
  144. print(string)
  145. def main():
  146. x_start = (5, 5)
  147. x_goal = (45, 25)
  148. lpastar = LpaStar(x_start, x_goal, "manhattan")
  149. lpastar.run()
  150. if __name__ == '__main__':
  151. main()