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