ara_star.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. """
  2. ARA_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 AraStar:
  13. def __init__(self, x_start, x_goal, 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.u_set = self.Env.motions # feasible input set
  18. self.obs = self.Env.obs # position of obstacles
  19. self.e = 2.5
  20. self.g = {self.xI: 0, self.xG: float("inf")}
  21. self.fig_name = "ARA_Star Algorithm"
  22. self.OPEN = queue.QueuePrior() # priority queue / OPEN
  23. self.CLOSED = []
  24. self.INCONS = []
  25. self.parent = {self.xI: self.xI}
  26. self.path = []
  27. self.visited = []
  28. def searching(self):
  29. self.OPEN.put(self.xI, self.fvalue(self.xI))
  30. self.ImprovePath()
  31. self.path.append(self.extract_path())
  32. while self.update_e() > 1:
  33. self.e -= 0.5
  34. print(self.e)
  35. OPEN_mid = [x for (p, x) in self.OPEN.enumerate()] + self.INCONS
  36. self.OPEN = queue.QueuePrior()
  37. self.OPEN.put(self.xI, self.fvalue(self.xI))
  38. for x in OPEN_mid:
  39. self.OPEN.put(x, self.fvalue(x))
  40. self.INCONS = []
  41. self.CLOSED = []
  42. self.ImprovePath()
  43. self.path.append(self.extract_path())
  44. return self.path, self.visited
  45. def ImprovePath(self):
  46. visited_each = []
  47. while (self.fvalue(self.xG) >
  48. min([self.fvalue(x) for (p, x) in self.OPEN.enumerate()])):
  49. s = self.OPEN.get()
  50. if s not in self.CLOSED:
  51. self.CLOSED.append(s)
  52. for u_next in self.u_set:
  53. s_next = tuple([s[i] + u_next[i] for i in range(len(s))])
  54. if s_next not in self.obs:
  55. new_cost = self.g[s] + self.get_cost(s, u_next)
  56. if s_next not in self.g or new_cost < self.g[s_next]:
  57. self.g[s_next] = new_cost
  58. self.parent[s_next] = s
  59. visited_each.append(s_next)
  60. if s_next not in self.CLOSED:
  61. self.OPEN.put(s_next, self.fvalue(s_next))
  62. else:
  63. self.INCONS.append(s_next)
  64. self.visited.append(visited_each)
  65. def update_e(self):
  66. c_OPEN, c_INCONS = float("inf"), float("inf")
  67. if not self.OPEN.empty():
  68. c_OPEN = min(self.g[x] + self.Heuristic(x) for (p, x) in self.OPEN.enumerate())
  69. if len(self.INCONS) != 0:
  70. c_INCONS = min(self.g[x] + self.Heuristic(x) for x in self.INCONS)
  71. if min(c_OPEN, c_INCONS) == float("inf"):
  72. return 1
  73. return min(self.e, self.g[self.xG] / min(c_OPEN, c_INCONS))
  74. def fvalue(self, x):
  75. h = self.e * self.Heuristic(x)
  76. return self.g[x] + h
  77. def extract_path(self):
  78. """
  79. Extract the path based on the relationship of nodes.
  80. :param policy: Action needed for transfer between two nodes
  81. :return: The planning path
  82. """
  83. path_back = [self.xG]
  84. x_current = self.xG
  85. while True:
  86. x_current = self.parent[x_current]
  87. path_back.append(x_current)
  88. if x_current == self.xI:
  89. break
  90. return list(path_back)
  91. @staticmethod
  92. def get_cost(x, u):
  93. """
  94. Calculate cost for this motion
  95. :param x: current node
  96. :param u: input
  97. :return: cost for this motion
  98. :note: cost function could be more complicate!
  99. """
  100. return 1
  101. def Heuristic(self, state):
  102. """
  103. Calculate heuristic.
  104. :param state: current node (state)
  105. :param goal: goal node (state)
  106. :param heuristic_type: choosing different heuristic functions
  107. :return: heuristic
  108. """
  109. heuristic_type = self.heuristic_type
  110. goal = self.xG
  111. if heuristic_type == "manhattan":
  112. return abs(goal[0] - state[0]) + abs(goal[1] - state[1])
  113. elif heuristic_type == "euclidean":
  114. return ((goal[0] - state[0]) ** 2 + (goal[1] - state[1]) ** 2) ** (1 / 2)
  115. else:
  116. print("Please choose right heuristic type!")
  117. def main():
  118. x_start = (5, 5) # Starting node
  119. x_goal = (49, 5) # Goal node
  120. arastar = AraStar(x_start, x_goal, "manhattan")
  121. plot = plotting.Plotting(x_start, x_goal)
  122. fig_name = "ARA* algorithm"
  123. path, visited = arastar.searching()
  124. plot.animation_ara_star(path, visited, fig_name)
  125. if __name__ == '__main__':
  126. main()