LPAstar.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. """
  2. LPA_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 LpaStar:
  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.U = queue.QueuePrior() # priority queue / OPEN set
  21. self.g, self.rhs = {}, {}
  22. for i in range(self.Env.x_range):
  23. for j in range(self.Env.y_range):
  24. self.rhs[(i, j)] = float("inf")
  25. self.g[(i, j)] = float("inf")
  26. self.rhs[self.xI] = 0
  27. self.U.put(self.xI, self.CalculateKey(self.xI))
  28. def searching(self):
  29. self.computePath()
  30. path = [self.extract_path()]
  31. obs_change = set()
  32. for j in range(14, 15):
  33. self.obs.add((30, j))
  34. obs_change.add((30, j))
  35. for s in obs_change:
  36. self.rhs[s] = float("inf")
  37. self.g[s] = float("inf")
  38. for x in self.get_neighbor(s):
  39. self.UpdateVertex(x)
  40. # for x in obs_change:
  41. # self.obs.remove(x)
  42. # for x in obs_change:
  43. # self.UpdateVertex(x)
  44. print(self.g[(29, 15)])
  45. print(self.g[(29, 14)])
  46. print(self.g[(29, 13)])
  47. print(self.g[(30, 13)])
  48. print(self.g[(31, 13)])
  49. print(self.g[(32, 13)])
  50. print(self.g[(33, 13)])
  51. print(self.g[(34, 13)])
  52. self.computePath()
  53. path.append(self.extract_path_test())
  54. return path, obs_change
  55. def computePath(self):
  56. while self.U.top_key() < self.CalculateKey(self.xG) \
  57. or self.rhs[self.xG] != self.g[self.xG]:
  58. s = self.U.get()
  59. if self.g[s] > self.rhs[s]:
  60. self.g[s] = self.rhs[s]
  61. else:
  62. self.g[s] = float("inf")
  63. self.UpdateVertex(s)
  64. for x in self.get_neighbor(s):
  65. self.UpdateVertex(x)
  66. # return self.extract_path()
  67. def extract_path(self):
  68. path = []
  69. s = self.xG
  70. while True:
  71. g_list = {}
  72. for x in self.get_neighbor(s):
  73. g_list[x] = self.g[x]
  74. s = min(g_list, key=g_list.get)
  75. if s == self.xI:
  76. return list(reversed(path))
  77. path.append(s)
  78. def extract_path_test(self):
  79. path = []
  80. s = self.xG
  81. for k in range(30):
  82. g_list = {}
  83. for x in self.get_neighbor(s):
  84. g_list[x] = self.g[x]
  85. s = min(g_list, key=g_list.get)
  86. path.append(s)
  87. return list(reversed(path))
  88. def get_neighbor(self, s):
  89. nei_list = set()
  90. for u in self.u_set:
  91. s_next = tuple([s[i] + u[i] for i in range(2)])
  92. if s_next not in self.obs:
  93. nei_list.add(s_next)
  94. return nei_list
  95. def CalculateKey(self, s):
  96. return [min(self.g[s], self.rhs[s]) + self.h(s),
  97. min(self.g[s], self.rhs[s])]
  98. def UpdateVertex(self, u):
  99. if u != self.xI:
  100. u_min = float("inf")
  101. for x in self.get_neighbor(u):
  102. u_min = min(u_min, self.g[x] + self.get_cost(u, x))
  103. self.rhs[u] = u_min
  104. self.U.check_remove(u)
  105. if self.g[u] != self.rhs[u]:
  106. self.U.put(u, self.CalculateKey(u))
  107. def h(self, s):
  108. heuristic_type = self.heuristic_type # heuristic type
  109. goal = self.xG # goal node
  110. if heuristic_type == "manhattan":
  111. return abs(goal[0] - s[0]) + abs(goal[1] - s[1])
  112. elif heuristic_type == "euclidean":
  113. return ((goal[0] - s[0]) ** 2 + (goal[1] - s[1]) ** 2) ** (1 / 2)
  114. else:
  115. print("Please choose right heuristic type!")
  116. def get_cost(self, s_start, s_end):
  117. """
  118. Calculate cost for this motion
  119. :param s_start:
  120. :param s_end:
  121. :return: cost for this motion
  122. :note: cost function could be more complicate!
  123. """
  124. if s_start not in self.obs:
  125. if s_end not in self.obs:
  126. return 1
  127. else:
  128. return float("inf")
  129. return float("inf")
  130. def main():
  131. x_start = (5, 5)
  132. x_goal = (45, 25)
  133. lpastar = LpaStar(x_start, x_goal, "euclidean")
  134. plot = plotting.Plotting(x_start, x_goal)
  135. path, obs = lpastar.searching()
  136. plot.plot_grid("Lifelong Planning A*")
  137. p = path[0]
  138. px = [x[0] for x in p]
  139. py = [x[1] for x in p]
  140. plt.plot(px, py, marker='o')
  141. plt.pause(0.5)
  142. p = path[1]
  143. px = [x[0] for x in p]
  144. py = [x[1] for x in p]
  145. plt.plot(px, py, marker='o')
  146. plt.pause(0.01)
  147. plt.show()
  148. if __name__ == '__main__':
  149. main()