astar.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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_searching(self, e):
  46. path, visited = [], []
  47. while e >= 1:
  48. p_k, v_k = self.repeated_Astar(self.s_start, self.s_goal, e)
  49. path.append(p_k)
  50. visited.append(v_k)
  51. e -= 0.5
  52. return path, visited
  53. def repeated_Astar(self, s_start, s_goal, e):
  54. g = {s_start: 0, s_goal: float("inf")}
  55. OPEN = queue.QueuePrior()
  56. OPEN.put(s_start, g[s_start] + e * self.Heuristic(s_start))
  57. CLOSED = []
  58. PARENT = {s_start: s_start}
  59. while OPEN:
  60. s = OPEN.get()
  61. CLOSED.append(s)
  62. if s == s_goal:
  63. break
  64. for s_n in self.get_neighbor(s):
  65. if s_n not in CLOSED:
  66. new_cost = g[s] + self.cost(s, s_n)
  67. if s_n not in g:
  68. g[s_n] = float("inf")
  69. if new_cost < g[s_n]: # conditions for updating cost
  70. g[s_n] = new_cost
  71. PARENT[s_n] = s
  72. OPEN.put(s_n, g[s_n] + e * self.Heuristic(s_n))
  73. return self.extract_path(PARENT), CLOSED
  74. def get_neighbor(self, s):
  75. """
  76. find neighbors of state s that not in obstacles.
  77. :param s: state
  78. :return: neighbors
  79. """
  80. s_list = set()
  81. for u in self.u_set:
  82. s_next = tuple([s[i] + u[i] for i in range(2)])
  83. if s_next not in self.obs:
  84. s_list.add(s_next)
  85. return s_list
  86. def fvalue(self, x):
  87. """
  88. f = g + h. (g: cost to come, h: heuristic function)
  89. :param x: current state
  90. :return: f
  91. """
  92. return self.g[x] + self.Heuristic(x)
  93. def extract_path(self, PARENT):
  94. """
  95. Extract the path based on the PARENT set.
  96. :return: The planning path
  97. """
  98. path = [self.s_goal]
  99. s = self.s_goal
  100. while True:
  101. s = PARENT[s]
  102. path.append(s)
  103. if s == self.s_start:
  104. break
  105. return list(path)
  106. @staticmethod
  107. def cost(s_start, s_goal):
  108. """
  109. Calculate cost for this motion
  110. :param s_start: starting node
  111. :param s_goal: end node
  112. :return: cost for this motion
  113. :note: cost function could be more complicate!
  114. """
  115. return 1
  116. def Heuristic(self, s):
  117. """
  118. Calculate heuristic.
  119. :param s: current node (state)
  120. :return: heuristic function value
  121. """
  122. heuristic_type = self.heuristic_type # heuristic type
  123. goal = self.s_goal # goal node
  124. if heuristic_type == "manhattan":
  125. return abs(goal[0] - s[0]) + abs(goal[1] - s[1])
  126. else:
  127. return math.hypot(goal[0] - s[0], goal[1] - s[1])
  128. def main():
  129. s_start = (5, 5)
  130. s_goal = (45, 25)
  131. astar = Astar(s_start, s_goal, "euclidean")
  132. plot = plotting.Plotting(s_start, s_goal)
  133. path, visited = astar.searching()
  134. plot.animation(path, visited, "A*") # animation
  135. # path, visited = astar.repeated_searching(2.5) # initial weight e = 2.5
  136. # plot.animation_ara_star(path, visited, "Repeated A*")
  137. if __name__ == '__main__':
  138. main()