astar.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. """
  2. A_star 2D
  3. @author: huiming zhou
  4. """
  5. import os
  6. import sys
  7. import math
  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 Astar:
  14. def __init__(self, start, goal, heuristic_type):
  15. self.s_start, self.s_goal = start, 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.s_start: 0, self.s_goal: float("inf")} # cost to come
  21. self.OPEN = queue.QueuePrior() # priority queue / OPEN set
  22. self.OPEN.put(self.s_start, self.fvalue(self.s_start))
  23. self.CLOSED = [] # CLOSED set / VISITED order
  24. self.PARENT = {self.s_start: self.s_start}
  25. def searching(self):
  26. """
  27. A_star Searching.
  28. :return: path, order of visited nodes
  29. """
  30. while not self.OPEN.empty():
  31. s = self.OPEN.get()
  32. self.CLOSED.append(s)
  33. if s == self.s_goal: # stop condition
  34. break
  35. for s_n in self.get_neighbor(s):
  36. if s_n not in self.CLOSED:
  37. new_cost = self.g[s] + self.cost(s, s_n)
  38. if s_n not in self.g:
  39. self.g[s_n] = float("inf")
  40. if new_cost < self.g[s_n]: # conditions for updating cost
  41. self.g[s_n] = new_cost
  42. self.PARENT[s_n] = s
  43. self.OPEN.put(s_n, self.fvalue(s_n))
  44. return self.extract_path(self.PARENT), self.CLOSED
  45. def repeated_astar(self, e):
  46. """
  47. repeated a*.
  48. :param e: weight of a*
  49. :return: path and visited order
  50. """
  51. path, visited = [], []
  52. while e >= 1:
  53. p_k, v_k = self.repeated_searching(self.s_start, self.s_goal, e)
  54. path.append(p_k)
  55. visited.append(v_k)
  56. e -= 0.5
  57. return path, visited
  58. def repeated_searching(self, s_start, s_goal, e):
  59. """
  60. run a* with weight e.
  61. :param s_start: starting state
  62. :param s_goal: goal state
  63. :param e: weight of a*
  64. :return: path and visited order.
  65. """
  66. g = {s_start: 0, s_goal: float("inf")}
  67. OPEN = queue.QueuePrior()
  68. OPEN.put(s_start, g[s_start] + e * self.Heuristic(s_start))
  69. CLOSED = []
  70. PARENT = {s_start: s_start}
  71. while OPEN:
  72. s = OPEN.get()
  73. CLOSED.append(s)
  74. if s == s_goal:
  75. break
  76. for s_n in self.get_neighbor(s):
  77. if s_n not in CLOSED:
  78. new_cost = g[s] + self.cost(s, s_n)
  79. if s_n not in g:
  80. g[s_n] = float("inf")
  81. if new_cost < g[s_n]: # conditions for updating cost
  82. g[s_n] = new_cost
  83. PARENT[s_n] = s
  84. OPEN.put(s_n, g[s_n] + e * self.Heuristic(s_n))
  85. return self.extract_path(PARENT), CLOSED
  86. def get_neighbor(self, s):
  87. """
  88. find neighbors of state s that not in obstacles.
  89. :param s: state
  90. :return: neighbors
  91. """
  92. s_list = set()
  93. for u in self.u_set:
  94. s_next = tuple([s[i] + u[i] for i in range(2)])
  95. if s_next not in self.obs:
  96. s_list.add(s_next)
  97. return s_list
  98. def fvalue(self, x):
  99. """
  100. f = g + h. (g: cost to come, h: heuristic function)
  101. :param x: current state
  102. :return: f
  103. """
  104. return self.g[x] + self.Heuristic(x)
  105. def extract_path(self, PARENT):
  106. """
  107. Extract the path based on the PARENT set.
  108. :return: The planning path
  109. """
  110. path = [self.s_goal]
  111. s = self.s_goal
  112. while True:
  113. s = PARENT[s]
  114. path.append(s)
  115. if s == self.s_start:
  116. break
  117. return list(path)
  118. @staticmethod
  119. def cost(s_start, s_goal):
  120. """
  121. Calculate cost for this motion
  122. :param s_start: starting node
  123. :param s_goal: end node
  124. :return: cost for this motion
  125. :note: cost function could be more complicate!
  126. """
  127. return 1
  128. def Heuristic(self, s):
  129. """
  130. Calculate heuristic.
  131. :param s: current node (state)
  132. :return: heuristic function value
  133. """
  134. heuristic_type = self.heuristic_type # heuristic type
  135. goal = self.s_goal # goal node
  136. if heuristic_type == "manhattan":
  137. return abs(goal[0] - s[0]) + abs(goal[1] - s[1])
  138. else:
  139. return math.hypot(goal[0] - s[0], goal[1] - s[1])
  140. def main():
  141. s_start = (5, 5)
  142. s_goal = (45, 25)
  143. astar = Astar(s_start, s_goal, "euclidean")
  144. plot = plotting.Plotting(s_start, s_goal)
  145. path, visited = astar.searching()
  146. plot.animation(path, visited, "A*") # animation
  147. # path, visited = astar.repeated_astar(2.5) # initial weight e = 2.5
  148. # plot.animation_ara_star(path, visited, "Repeated A*")
  149. if __name__ == '__main__':
  150. main()