a_star.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import queue
  2. import plotting
  3. import env
  4. class Astar:
  5. def __init__(self, x_start, x_goal, e, heuristic_type):
  6. self.xI, self.xG = x_start, x_goal
  7. self.heuristic_type = heuristic_type
  8. self.Env = env.Env() # class Env
  9. self.e = e
  10. self.u_set = self.Env.motions # feasible input set
  11. self.obs = self.Env.obs # position of obstacles
  12. self.g = {self.xI: 0, self.xG: float("inf")}
  13. self.fig_name = "A* Algorithm"
  14. self.OPEN = queue.QueuePrior() # priority queue / OPEN
  15. self.OPEN.put(self.xI, self.fvalue(self.xI))
  16. self.parent = {self.xI: self.xI}
  17. def searching(self):
  18. """
  19. Searching using A_star.
  20. :return: planning path, action in each node, visited nodes in the planning process
  21. """
  22. visited = []
  23. while not self.OPEN.empty():
  24. s = self.OPEN.get()
  25. if s == self.xG: # stop condition
  26. break
  27. visited.append(s)
  28. for u_next in self.u_set: # explore neighborhoods of current node
  29. s_next = tuple([s[i] + u_next[i] for i in range(len(s))])
  30. if s_next not in self.obs:
  31. new_cost = self.g[s] + self.get_cost(s, u_next)
  32. if s_next not in self.g or new_cost < self.g[s_next]: # conditions for updating cost
  33. self.g[s_next] = new_cost
  34. self.parent[s_next] = s
  35. self.OPEN.put(s_next, self.fvalue(s_next))
  36. return self.extract_path(), visited
  37. def fvalue(self, x):
  38. h = self.e * self.Heuristic(x)
  39. return self.g[x] + h
  40. def extract_path(self):
  41. """
  42. Extract the path based on the relationship of nodes.
  43. :return: The planning path
  44. """
  45. path_back = [self.xG]
  46. x_current = self.xG
  47. while True:
  48. x_current = self.parent[x_current]
  49. path_back.append(x_current)
  50. if x_current == self.xI:
  51. break
  52. return list(path_back)
  53. @staticmethod
  54. def get_cost(x, u):
  55. """
  56. Calculate cost for this motion
  57. :param x: current node
  58. :param u: input
  59. :return: cost for this motion
  60. :note: cost function could be more complicate!
  61. """
  62. return 1
  63. def Heuristic(self, state):
  64. """
  65. Calculate heuristic.
  66. :param state: current node (state)
  67. :param goal: goal node (state)
  68. :param heuristic_type: choosing different heuristic functions
  69. :return: heuristic
  70. """
  71. heuristic_type = self.heuristic_type
  72. goal = self.xG
  73. if heuristic_type == "manhattan":
  74. return abs(goal[0] - state[0]) + abs(goal[1] - state[1])
  75. elif heuristic_type == "euclidean":
  76. return ((goal[0] - state[0]) ** 2 + (goal[1] - state[1]) ** 2) ** (1 / 2)
  77. else:
  78. print("Please choose right heuristic type!")
  79. def main():
  80. x_start = (5, 5) # Starting node
  81. x_goal = (49, 5) # Goal node
  82. astar = Astar(x_start, x_goal, 1, "manhattan")
  83. plot = plotting.Plotting(x_start, x_goal) # class Plotting
  84. fig_name = "A* Algorithm"
  85. path, visited = astar.searching()
  86. plot.animation(path, visited, fig_name) # animation generate
  87. if __name__ == '__main__':
  88. main()