rrt_star.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. from rrt_2D import queue
  15. class Node:
  16. def __init__(self, n):
  17. self.x = n[0]
  18. self.y = n[1]
  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.s_start = Node(x_start)
  24. self.s_goal = 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.s_start]
  30. self.path = []
  31. self.env = env.Env()
  32. self.plotting = plotting.Plotting(x_start, x_goal)
  33. self.utils = utils.Utils()
  34. self.x_range = self.env.x_range
  35. self.y_range = self.env.y_range
  36. self.obs_circle = self.env.obs_circle
  37. self.obs_rectangle = self.env.obs_rectangle
  38. self.obs_boundary = self.env.obs_boundary
  39. def planning(self):
  40. for k in range(self.iter_max):
  41. node_rand = self.generate_random_node(self.goal_sample_rate)
  42. node_near = self.nearest_neighbor(self.vertex, node_rand)
  43. node_new = self.new_state(node_near, node_rand)
  44. if k % 500 == 0:
  45. print(k)
  46. if node_new and not self.utils.is_collision(node_near, node_new):
  47. neighbor_index = self.find_near_neighbor(node_new)
  48. self.vertex.append(node_new)
  49. if neighbor_index:
  50. self.choose_parent(node_new, neighbor_index)
  51. self.rewire(node_new, neighbor_index)
  52. index = self.search_goal_parent()
  53. self.path = self.extract_path(self.vertex[index])
  54. self.plotting.animation(self.vertex, self.path, "rrt*, N = " + str(self.iter_max))
  55. def new_state(self, node_start, node_goal):
  56. dist, theta = self.get_distance_and_angle(node_start, node_goal)
  57. dist = min(self.step_len, dist)
  58. node_new = Node((node_start.x + dist * math.cos(theta),
  59. node_start.y + dist * math.sin(theta)))
  60. node_new.parent = node_start
  61. return node_new
  62. def choose_parent(self, node_new, neighbor_index):
  63. cost = [self.get_new_cost(self.vertex[i], node_new) for i in neighbor_index]
  64. cost_min_index = neighbor_index[int(np.argmin(cost))]
  65. node_new.parent = self.vertex[cost_min_index]
  66. def rewire(self, node_new, neighbor_index):
  67. for i in neighbor_index:
  68. node_neighbor = self.vertex[i]
  69. if self.cost(node_neighbor) > self.get_new_cost(node_new, node_neighbor):
  70. node_neighbor.parent = node_new
  71. def search_goal_parent(self):
  72. dist_list = [math.hypot(n.x - self.s_goal.x, n.y - self.s_goal.y) for n in self.vertex]
  73. node_index = [i for i in range(len(dist_list)) if dist_list[i] <= self.step_len]
  74. if len(node_index) > 0:
  75. cost_list = [dist_list[i] + self.cost(self.vertex[i]) for i in node_index
  76. if not self.utils.is_collision(self.vertex[i], self.s_goal)]
  77. return node_index[int(np.argmin(cost_list))]
  78. return len(self.vertex) - 1
  79. def get_new_cost(self, node_start, node_end):
  80. dist, _ = self.get_distance_and_angle(node_start, node_end)
  81. return self.cost(node_start) + dist
  82. def generate_random_node(self, goal_sample_rate):
  83. delta = self.utils.delta
  84. if np.random.random() > goal_sample_rate:
  85. return Node((np.random.uniform(self.x_range[0] + delta, self.x_range[1] - delta),
  86. np.random.uniform(self.y_range[0] + delta, self.y_range[1] - delta)))
  87. return self.s_goal
  88. def find_near_neighbor(self, node_new):
  89. n = len(self.vertex) + 1
  90. r = min(self.search_radius * math.sqrt((math.log(n) / n)), self.step_len)
  91. dist_table = [math.hypot(nd.x - node_new.x, nd.y - node_new.y) for nd in self.vertex]
  92. dist_table_index = [ind for ind in range(len(dist_table)) if dist_table[ind] <= r and
  93. not self.utils.is_collision(node_new, self.vertex[ind])]
  94. return dist_table_index
  95. @staticmethod
  96. def nearest_neighbor(node_list, n):
  97. return node_list[int(np.argmin([math.hypot(nd.x - n.x, nd.y - n.y)
  98. for nd in node_list]))]
  99. @staticmethod
  100. def cost(node_p):
  101. node = node_p
  102. cost = 0.0
  103. while node.parent:
  104. cost += math.hypot(node.x - node.parent.x, node.y - node.parent.y)
  105. node = node.parent
  106. return cost
  107. def update_cost(self, parent_node):
  108. OPEN = queue.QueueFIFO()
  109. OPEN.put(parent_node)
  110. while not OPEN.empty():
  111. node = OPEN.get()
  112. if len(node.child) == 0:
  113. continue
  114. for node_c in node.child:
  115. node_c.Cost = self.get_new_cost(node, node_c)
  116. OPEN.put(node_c)
  117. def extract_path(self, node_end):
  118. path = [[self.s_goal.x, self.s_goal.y]]
  119. node = node_end
  120. while node.parent is not None:
  121. path.append([node.x, node.y])
  122. node = node.parent
  123. path.append([node.x, node.y])
  124. return path
  125. @staticmethod
  126. def get_distance_and_angle(node_start, node_end):
  127. dx = node_end.x - node_start.x
  128. dy = node_end.y - node_start.y
  129. return math.hypot(dx, dy), math.atan2(dy, dx)
  130. def main():
  131. x_start = (18, 8) # Starting node
  132. x_goal = (37, 18) # Goal node
  133. rrt_star = RrtStar(x_start, x_goal, 10, 0.10, 20, 10000)
  134. rrt_star.planning()
  135. if __name__ == '__main__':
  136. main()