ARAstar.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. """
  2. ARA_star 2D (Anytime Repairing A*)
  3. @author: huiming zhou
  4. """
  5. import os
  6. import sys
  7. import math
  8. sys.path.append(os.path.dirname(os.path.abspath(__file__)) +
  9. "/../../Search-based Planning/")
  10. from Search_2D import queue
  11. from Search_2D import plotting
  12. from Search_2D import env
  13. class AraStar:
  14. def __init__(self, s_start, s_goal, e, heuristic_type):
  15. self.s_start, self.s_goal = s_start, s_goal
  16. self.heuristic_type = heuristic_type
  17. self.Env = env.Env() # class Env
  18. self.u_set = self.Env.motions # feasible input set
  19. self.obs = self.Env.obs # position of obstacles
  20. self.e = e # initial weight
  21. self.g = {self.s_start: 0, self.s_goal: float("inf")} # cost to come
  22. self.OPEN = queue.QueuePrior() # priority queue / U
  23. self.CLOSED = set() # closed set
  24. self.INCONS = [] # incons set
  25. self.PARENT = {self.s_start: self.s_start} # relations
  26. self.path = [] # planning path
  27. self.visited = [] # order of visited nodes
  28. def searching(self):
  29. self.OPEN.put(self.s_start, self.fvalue(self.s_start))
  30. self.ImprovePath()
  31. self.path.append(self.extract_path())
  32. while self.update_e() > 1: # continue condition
  33. self.e -= 0.5 # increase weight
  34. OPEN_mid = [x for (p, x) in self.OPEN.enumerate()] + self.INCONS # combine two sets
  35. self.OPEN = queue.QueuePrior()
  36. self.OPEN.put(self.s_start, self.fvalue(self.s_start))
  37. for x in OPEN_mid:
  38. self.OPEN.put(x, self.fvalue(x)) # update priority
  39. self.INCONS = []
  40. self.CLOSED = set()
  41. self.ImprovePath() # improve path
  42. self.path.append(self.extract_path())
  43. return self.path, self.visited
  44. def ImprovePath(self):
  45. """
  46. :return: a e'-suboptimal path
  47. """
  48. visited_each = []
  49. while (self.fvalue(self.s_goal) >
  50. min([self.fvalue(x) for (p, x) in self.OPEN.enumerate()])):
  51. s = self.OPEN.get()
  52. if s not in self.CLOSED:
  53. self.CLOSED.add(s)
  54. for s_n in self.get_neighbor(s):
  55. new_cost = self.g[s] + self.cost(s, s_n)
  56. if s_n not in self.g or new_cost < self.g[s_n]:
  57. self.g[s_n] = new_cost
  58. self.PARENT[s_n] = s
  59. visited_each.append(s_n)
  60. if s_n not in self.CLOSED:
  61. self.OPEN.put(s_n, self.fvalue(s_n))
  62. else:
  63. self.INCONS.append(s_n)
  64. self.visited.append(visited_each)
  65. def get_neighbor(self, s):
  66. """
  67. find neighbors of state s that not in obstacles.
  68. :param s: state
  69. :return: neighbors
  70. """
  71. s_list = set()
  72. for u in self.u_set:
  73. s_next = tuple([s[i] + u[i] for i in range(2)])
  74. if s_next not in self.obs:
  75. s_list.add(s_next)
  76. return s_list
  77. def update_e(self):
  78. c_OPEN, c_INCONS = float("inf"), float("inf")
  79. if self.OPEN:
  80. c_OPEN = min(self.g[x] +
  81. self.Heuristic(x) for (p, x) in self.OPEN.enumerate())
  82. if self.INCONS:
  83. c_INCONS = min(self.g[x] +
  84. self.Heuristic(x) for x in self.INCONS)
  85. if min(c_OPEN, c_INCONS) == float("inf"):
  86. return 1
  87. return min(self.e, self.g[self.s_goal] / min(c_OPEN, c_INCONS))
  88. def fvalue(self, x):
  89. return self.g[x] + self.e * self.Heuristic(x)
  90. def extract_path(self):
  91. """
  92. Extract the path based on the PARENT set.
  93. :return: The planning path
  94. """
  95. path = [self.s_goal]
  96. s = self.s_goal
  97. while True:
  98. s = self.PARENT[s]
  99. path.append(s)
  100. if s == self.s_start:
  101. break
  102. return list(path)
  103. def Heuristic(self, s):
  104. """
  105. Calculate heuristic.
  106. :param s: current node (state)
  107. :return: heuristic function value
  108. """
  109. heuristic_type = self.heuristic_type # heuristic type
  110. goal = self.s_goal # goal node
  111. if heuristic_type == "manhattan":
  112. return abs(goal[0] - s[0]) + abs(goal[1] - s[1])
  113. else:
  114. return math.hypot(goal[0] - s[0], goal[1] - s[1])
  115. @staticmethod
  116. def cost(s_start, s_goal):
  117. """
  118. Calculate cost for this motion
  119. :param s_start: starting node
  120. :param s_goal: end node
  121. :return: cost for this motion
  122. :note: cost function could be more complicate!
  123. """
  124. return 1
  125. def main():
  126. x_start = (5, 5) # Starting node
  127. x_goal = (45, 25) # Goal node
  128. arastar = AraStar(x_start, x_goal, 2.5, "manhattan")
  129. plot = plotting.Plotting(x_start, x_goal)
  130. fig_name = "Anytime Repairing A* (ARA*)"
  131. path, visited = arastar.searching()
  132. plot.animation_ara_star(path, visited, fig_name)
  133. if __name__ == '__main__':
  134. main()