bidirectional_a_star.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. """
  2. Bidirectional_a_star 2D
  3. @author: huiming zhou
  4. """
  5. import os
  6. import sys
  7. sys.path.append(os.path.dirname(os.path.abspath(__file__)) +
  8. "/../../Search-based Planning/")
  9. from Search_2D import queue
  10. from Search_2D import plotting
  11. from Search_2D import env
  12. class BidirectionalAstar:
  13. def __init__(self, x_start, x_goal, heuristic_type):
  14. self.xI, self.xG = x_start, x_goal
  15. self.heuristic_type = heuristic_type
  16. self.Env = env.Env() # class Env
  17. self.u_set = self.Env.motions # feasible input set
  18. self.obs = self.Env.obs # position of obstacles
  19. self.g_fore = {self.xI: 0, self.xG: float("inf")} # cost to come: from x_start
  20. self.g_back = {self.xG: 0, self.xI: float("inf")} # cost to come: form x_goal
  21. self.OPEN_fore = queue.QueuePrior() # OPEN set for foreward searching
  22. self.OPEN_fore.put(self.xI, self.g_fore[self.xI] + self.h(self.xI, self.xG))
  23. self.OPEN_back = queue.QueuePrior() # OPEN set for backward searching
  24. self.OPEN_back.put(self.xG, self.g_back[self.xG] + self.h(self.xG, self.xI))
  25. self.CLOSED_fore = [] # CLOSED set for foreward
  26. self.CLOSED_back = [] # CLOSED set for backward
  27. self.PARENT_fore = {self.xI: self.xI}
  28. self.PARENT_back = {self.xG: self.xG}
  29. def searching(self):
  30. s_meet = self.xI
  31. while not self.OPEN_fore.empty() and not self.OPEN_back.empty():
  32. # solve foreward-search
  33. s_fore = self.OPEN_fore.get()
  34. if s_fore in self.PARENT_back:
  35. s_meet = s_fore
  36. break
  37. self.CLOSED_fore.append(s_fore)
  38. for u in self.u_set:
  39. s_next = tuple([s_fore[i] + u[i] for i in range(2)])
  40. if s_next not in self.obs:
  41. new_cost = self.g_fore[s_fore] + self.get_cost(s_fore, u)
  42. if s_next not in self.g_fore:
  43. self.g_fore[s_next] = float("inf")
  44. if new_cost < self.g_fore[s_next]:
  45. self.g_fore[s_next] = new_cost
  46. self.PARENT_fore[s_next] = s_fore
  47. self.OPEN_fore.put(s_next, new_cost + self.h(s_next, self.xG))
  48. # solve backward-search
  49. s_back = self.OPEN_back.get()
  50. if s_back in self.PARENT_fore:
  51. s_meet = s_back
  52. break
  53. self.CLOSED_back.append(s_back)
  54. for u in self.u_set:
  55. s_next = tuple([s_back[i] + u[i] for i in range(len(s_back))])
  56. if s_next not in self.obs:
  57. new_cost = self.g_back[s_back] + self.get_cost(s_back, u)
  58. if s_next not in self.g_back:
  59. self.g_back[s_next] = float("inf")
  60. if new_cost < self.g_back[s_next]:
  61. self.g_back[s_next] = new_cost
  62. self.PARENT_back[s_next] = s_back
  63. self.OPEN_back.put(s_next, new_cost + self.h(s_next, self.xI))
  64. return self.extract_path(s_meet), self.CLOSED_fore, self.CLOSED_back
  65. def extract_path(self, s_meet):
  66. # extract path for foreward part
  67. path_fore = [s_meet]
  68. s = s_meet
  69. while True:
  70. s = self.PARENT_fore[s]
  71. path_fore.append(s)
  72. if s == self.xI:
  73. break
  74. # extract path for backward part
  75. path_back = []
  76. s = s_meet
  77. while True:
  78. s = self.PARENT_back[s]
  79. path_back.append(s)
  80. if s == self.xG:
  81. break
  82. return list(reversed(path_fore)) + list(path_back)
  83. def h(self, s, goal):
  84. """
  85. Calculate heuristic value.
  86. :param s: current node (state)
  87. :param goal: goal node (state)
  88. :return: heuristic value
  89. """
  90. heuristic_type = self.heuristic_type
  91. if heuristic_type == "manhattan":
  92. return abs(goal[0] - s[0]) + abs(goal[1] - s[1])
  93. elif heuristic_type == "euclidean":
  94. return ((goal[0] - s[0]) ** 2 + (goal[1] - s[1]) ** 2) ** (1 / 2)
  95. else:
  96. print("Please choose right heuristic type!")
  97. @staticmethod
  98. def get_cost(x, u):
  99. """
  100. Calculate cost for this motion
  101. :param x: current node
  102. :param u: input
  103. :return: cost for this motion
  104. :note: cost function could be more complicate!
  105. """
  106. return 1
  107. def main():
  108. x_start = (5, 5)
  109. x_goal = (45, 25)
  110. bastar = BidirectionalAstar(x_start, x_goal, "euclidean")
  111. plot = plotting.Plotting(x_start, x_goal)
  112. fig_name = "Bidirectional-A*"
  113. path, visited_fore, visited_back = bastar.searching()
  114. plot.animation_bi_astar(path, visited_fore, visited_back, fig_name) # animation
  115. if __name__ == '__main__':
  116. main()