a_star.py 3.5 KB

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