ara_star.py 4.6 KB

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