rrt_star.py 5.7 KB

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