rrt_star.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. """
  2. RRT_star 2D
  3. @author: huiming zhou
  4. """
  5. from rrt_2D import env
  6. from rrt_2D import plotting
  7. import numpy as np
  8. import math
  9. class Node:
  10. def __init__(self, n):
  11. self.x = n[0]
  12. self.y = n[1]
  13. self.cost = 0.0
  14. self.parent = None
  15. class RrtStar:
  16. def __init__(self, x_start, x_goal, expand_len,
  17. goal_sample_rate, search_radius, iter_limit):
  18. self.xI = Node(x_start)
  19. self.xG = Node(x_goal)
  20. self.expand_len = expand_len
  21. self.goal_sample_rate = goal_sample_rate
  22. self.search_radius = search_radius
  23. self.iter_limit = iter_limit
  24. self.vertex = [self.xI]
  25. self.env = env.Env()
  26. self.plotting = plotting.Plotting(x_start, x_goal)
  27. self.x_range = self.env.x_range
  28. self.y_range = self.env.y_range
  29. self.obs_circle = self.env.obs_circle
  30. self.obs_rectangle = self.env.obs_rectangle
  31. self.obs_boundary = self.env.obs_boundary
  32. def planning(self):
  33. for k in range(self.iter_limit):
  34. node_rand = self.random_state(self.goal_sample_rate)
  35. node_near = self.nearest_neighbor(self.vertex, node_rand)
  36. node_new = self.new_state(node_near, node_rand)
  37. if node_new and not self.check_collision(node_new):
  38. neighbor_index = self.find_near_neighbor(node_new)
  39. if neighbor_index:
  40. node_new = self.choose_parent(node_new, neighbor_index)
  41. self.vertex.append(node_new)
  42. self.rewire(node_new, neighbor_index)
  43. index = self.search_goal_parent()
  44. return self.extract_path(self.vertex[index])
  45. def random_state(self, goal_sample_rate):
  46. if np.random.random() > goal_sample_rate:
  47. return Node((np.random.uniform(self.x_range[0], self.x_range[1]),
  48. np.random.uniform(self.y_range[0], self.y_range[1])))
  49. return self.xG
  50. def nearest_neighbor(self, node_list, n):
  51. return self.vertex[int(np.argmin([math.hypot(nd.x - n.x, nd.y - n.y)
  52. for nd in node_list]))]
  53. def new_state(self, node_start, node_goal):
  54. node_new = Node((node_start.x, node_start.y))
  55. dist, theta = self.get_distance_and_angle(node_new, node_goal)
  56. dist = min(self.expand_len, dist)
  57. node_new.x += dist * math.cos(theta)
  58. node_new.y += dist * math.sin(theta)
  59. node_new.parent = node_start
  60. return node_new
  61. def find_near_neighbor(self, node_new):
  62. n = len(self.vertex) + 1
  63. r = min(self.search_radius * math.sqrt((math.log(n) / n)), self.expand_len)
  64. dist_table = [math.hypot(nd.x - node_new.x, nd.y - node_new.y) for nd in self.vertex]
  65. return [dist_table.index(d) for d in dist_table if d <= r]
  66. def choose_parent(self, node_new, neighbor_index):
  67. cost = []
  68. for i in neighbor_index:
  69. node_neighbor = self.vertex[i]
  70. cost.append(self.get_new_cost(node_neighbor, node_new))
  71. cost_min_index = neighbor_index[int(np.argmin(cost))]
  72. node_new = self.new_state(self.vertex[cost_min_index], node_new)
  73. node_new.cost = min(cost)
  74. return node_new
  75. def search_goal_parent(self):
  76. dist_list = [math.hypot(n.x - self.xG.x, n.y - self.xG.y) for n in self.vertex]
  77. node_index = [dist_list.index(i) for i in dist_list if i <= self.expand_len]
  78. if node_index:
  79. cost_list = [dist_list[i] + self.vertex[i].cost for i in node_index]
  80. return node_index[int(np.argmin(cost_list))]
  81. return None
  82. def rewire(self, node_new, neighbor_index):
  83. for i in neighbor_index:
  84. node_neighbor = self.vertex[i]
  85. new_cost = self.get_new_cost(node_new, node_neighbor)
  86. if node_neighbor.cost > new_cost:
  87. self.vertex[i] = self.new_state(node_new, node_neighbor)
  88. self.propagate_cost_to_leaves(node_new)
  89. def get_new_cost(self, node_start, node_end):
  90. dist, _ = self.get_distance_and_angle(node_start, node_end)
  91. return node_start.cost + dist
  92. def propagate_cost_to_leaves(self, parent_node):
  93. for node in self.vertex:
  94. if node.parent == parent_node:
  95. node.cost = self.get_new_cost(parent_node, node)
  96. self.propagate_cost_to_leaves(node)
  97. def extract_path(self, node_end):
  98. path = [[self.xG.x, self.xG.y]]
  99. node = node_end
  100. while node.parent is not None:
  101. path.append([node.x, node.y])
  102. node = node.parent
  103. path.append([node.x, node.y])
  104. return path
  105. def check_collision(self, node_end):
  106. for (ox, oy, r) in self.obs_circle:
  107. if math.hypot(node_end.x - ox, node_end.y - oy) <= r:
  108. return True
  109. for (ox, oy, w, h) in self.obs_rectangle:
  110. if 0 <= (node_end.x - ox) <= w and 0 <= (node_end.y - oy) <= h:
  111. return True
  112. for (ox, oy, w, h) in self.obs_boundary:
  113. if 0 <= (node_end.x - ox) <= w and 0 <= (node_end.y - oy) <= h:
  114. return True
  115. return False
  116. @staticmethod
  117. def get_distance_and_angle(node_start, node_end):
  118. dx = node_end.x - node_start.x
  119. dy = node_end.y - node_start.y
  120. return math.hypot(dx, dy), math.atan2(dy, dx)
  121. def main():
  122. x_start = (2, 2) # Starting node
  123. x_goal = (49, 28) # Goal node
  124. rrt_star = RrtStar(x_start, x_goal, 1, 0.1, 10, 5000)
  125. path = rrt_star.planning()
  126. if path:
  127. rrt_star.plotting.animation(rrt_star.vertex, path)
  128. else:
  129. print("No Path Found!")
  130. if __name__ == '__main__':
  131. main()