astar.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 # weighted A*: e >= 1
  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")} # cost to come
  21. self.OPEN = queue.QueuePrior() # priority queue / OPEN set
  22. self.OPEN.put(self.xI, self.fvalue(self.xI))
  23. self.CLOSED = [] # closed set & visited
  24. self.PARENT = {self.xI: self.xI} # relations
  25. def searching(self):
  26. """
  27. Searching using A_star.
  28. :return: path, order of visited nodes in the planning
  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 in self.u_set: # explore neighborhoods of current node
  36. s_next = tuple([s[i] + u[i] for i in range(2)])
  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)
  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. """
  48. f = g + h. (g: cost to come, h: heuristic function)
  49. :param x: current state
  50. :return: f
  51. """
  52. return self.g[x] + self.e * self.Heuristic(x)
  53. def extract_path(self):
  54. """
  55. Extract the path based on the relationship of nodes.
  56. :return: The planning path
  57. """
  58. path_back = [self.xG]
  59. x_current = self.xG
  60. while True:
  61. x_current = self.PARENT[x_current]
  62. path_back.append(x_current)
  63. if x_current == self.xI:
  64. break
  65. return list(path_back)
  66. @staticmethod
  67. def get_cost(x, u):
  68. """
  69. Calculate cost for this motion
  70. :param x: current node
  71. :param u: current input
  72. :return: cost for this motion
  73. :note: cost function could be more complicate!
  74. """
  75. return 1
  76. def Heuristic(self, state):
  77. """
  78. Calculate heuristic.
  79. :param state: current node (state)
  80. :return: heuristic function value
  81. """
  82. heuristic_type = self.heuristic_type # heuristic type
  83. goal = self.xG # goal node
  84. if heuristic_type == "manhattan":
  85. return abs(goal[0] - state[0]) + abs(goal[1] - state[1])
  86. elif heuristic_type == "euclidean":
  87. return ((goal[0] - state[0]) ** 2 + (goal[1] - state[1]) ** 2) ** (1 / 2)
  88. else:
  89. print("Please choose right heuristic type!")
  90. def main():
  91. x_start = (5, 5)
  92. x_goal = (45, 25)
  93. astar = Astar(x_start, x_goal, 1, "euclidean") # weight e = 1
  94. plot = plotting.Plotting(x_start, x_goal) # class Plotting
  95. fig_name = "A*"
  96. path, visited = astar.searching()
  97. plot.animation(path, visited, fig_name) # animation generate
  98. if __name__ == '__main__':
  99. main()