ARAstar.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. """
  2. ARA_star 2D (Anytime Repairing A*)
  3. @author: huiming zhou
  4. @description: local inconsistency: g-value decreased.
  5. g(s) decreased introduces a local inconsistency between s and its successors.
  6. """
  7. import os
  8. import sys
  9. import math
  10. sys.path.append(os.path.dirname(os.path.abspath(__file__)) +
  11. "/../../Search_based_Planning/")
  12. from Search_2D import plotting, 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 # weight
  21. self.g = dict() # Cost to come
  22. self.OPEN = dict() # priority queue / OPEN set
  23. self.CLOSED = set() # CLOSED set
  24. self.INCONS = {} # INCONSISTENT set
  25. self.PARENT = dict() # relations
  26. self.path = [] # planning path
  27. self.visited = [] # order of visited nodes
  28. def init(self):
  29. """
  30. initialize each set.
  31. """
  32. self.g[self.s_start] = 0.0
  33. self.g[self.s_goal] = math.inf
  34. self.OPEN[self.s_start] = self.f_value(self.s_start)
  35. self.PARENT[self.s_start] = self.s_start
  36. def searching(self):
  37. self.init()
  38. self.ImprovePath()
  39. self.path.append(self.extract_path())
  40. while self.update_e() > 1: # continue condition
  41. self.e -= 0.4 # increase weight
  42. self.OPEN.update(self.INCONS)
  43. self.OPEN = {s: self.f_value(s) for s in self.OPEN} # update f_value of OPEN set
  44. self.INCONS = dict()
  45. self.CLOSED = set()
  46. self.ImprovePath() # improve path
  47. self.path.append(self.extract_path())
  48. return self.path, self.visited
  49. def ImprovePath(self):
  50. """
  51. :return: a e'-suboptimal path
  52. """
  53. visited_each = []
  54. while True:
  55. s, f_small = self.calc_smallest_f()
  56. if self.f_value(self.s_goal) <= f_small:
  57. break
  58. self.OPEN.pop(s)
  59. self.CLOSED.add(s)
  60. for s_n in self.get_neighbor(s):
  61. if s_n in self.obs:
  62. continue
  63. new_cost = self.g[s] + self.cost(s, s_n)
  64. if s_n not in self.g or new_cost < self.g[s_n]:
  65. self.g[s_n] = new_cost
  66. self.PARENT[s_n] = s
  67. visited_each.append(s_n)
  68. if s_n not in self.CLOSED:
  69. self.OPEN[s_n] = self.f_value(s_n)
  70. else:
  71. self.INCONS[s_n] = 0.0
  72. self.visited.append(visited_each)
  73. def calc_smallest_f(self):
  74. """
  75. :return: node with smallest f_value in OPEN set.
  76. """
  77. s_small = min(self.OPEN, key=self.OPEN.get)
  78. return s_small, self.OPEN[s_small]
  79. def get_neighbor(self, s):
  80. """
  81. find neighbors of state s that not in obstacles.
  82. :param s: state
  83. :return: neighbors
  84. """
  85. return {(s[0] + u[0], s[1] + u[1]) for u in self.u_set}
  86. def update_e(self):
  87. v = float("inf")
  88. if self.OPEN:
  89. v = min(self.g[s] + self.h(s) for s in self.OPEN)
  90. if self.INCONS:
  91. v = min(v, min(self.g[s] + self.h(s) for s in self.INCONS))
  92. return min(self.e, self.g[self.s_goal] / v)
  93. def f_value(self, x):
  94. """
  95. f = g + e * h
  96. f = cost-to-come + weight * cost-to-go
  97. :param x: current state
  98. :return: f_value
  99. """
  100. return self.g[x] + self.e * self.h(x)
  101. def extract_path(self):
  102. """
  103. Extract the path based on the PARENT set.
  104. :return: The planning path
  105. """
  106. path = [self.s_goal]
  107. s = self.s_goal
  108. while True:
  109. s = self.PARENT[s]
  110. path.append(s)
  111. if s == self.s_start:
  112. break
  113. return list(path)
  114. def h(self, s):
  115. """
  116. Calculate heuristic.
  117. :param s: current node (state)
  118. :return: heuristic function value
  119. """
  120. heuristic_type = self.heuristic_type # heuristic type
  121. goal = self.s_goal # goal node
  122. if heuristic_type == "manhattan":
  123. return abs(goal[0] - s[0]) + abs(goal[1] - s[1])
  124. else:
  125. return math.hypot(goal[0] - s[0], goal[1] - s[1])
  126. def cost(self, s_start, s_goal):
  127. """
  128. Calculate Cost for this motion
  129. :param s_start: starting node
  130. :param s_goal: end node
  131. :return: Cost for this motion
  132. :note: Cost function could be more complicate!
  133. """
  134. if self.is_collision(s_start, s_goal):
  135. return math.inf
  136. return math.hypot(s_goal[0] - s_start[0], s_goal[1] - s_start[1])
  137. def is_collision(self, s_start, s_end):
  138. """
  139. check if the line segment (s_start, s_end) is collision.
  140. :param s_start: start node
  141. :param s_end: end node
  142. :return: True: is collision / False: not collision
  143. """
  144. if s_start in self.obs or s_end in self.obs:
  145. return True
  146. if s_start[0] != s_end[0] and s_start[1] != s_end[1]:
  147. if s_end[0] - s_start[0] == s_start[1] - s_end[1]:
  148. s1 = (min(s_start[0], s_end[0]), min(s_start[1], s_end[1]))
  149. s2 = (max(s_start[0], s_end[0]), max(s_start[1], s_end[1]))
  150. else:
  151. s1 = (min(s_start[0], s_end[0]), max(s_start[1], s_end[1]))
  152. s2 = (max(s_start[0], s_end[0]), min(s_start[1], s_end[1]))
  153. if s1 in self.obs or s2 in self.obs:
  154. return True
  155. return False
  156. def main():
  157. s_start = (5, 5)
  158. s_goal = (45, 25)
  159. arastar = AraStar(s_start, s_goal, 2.5, "euclidean")
  160. plot = plotting.Plotting(s_start, s_goal)
  161. path, visited = arastar.searching()
  162. plot.animation_ara_star(path, visited, "Anytime Repairing A* (ARA*)")
  163. if __name__ == '__main__':
  164. main()