optimal_bi_rrt.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. """
  2. Optimal_Bidirectional_RRT 2D
  3. @author: huiming zhou
  4. """
  5. import os
  6. import sys
  7. import math
  8. import numpy as np
  9. import matplotlib.pyplot as plt
  10. import matplotlib.patches as patches
  11. sys.path.append(os.path.dirname(os.path.abspath(__file__)) +
  12. "/../../Sampling-based Planning/")
  13. from rrt_2D import env
  14. from rrt_2D import plotting
  15. from rrt_2D import utils
  16. from rrt_2D import queue
  17. class Node:
  18. def __init__(self, n):
  19. self.x = n[0]
  20. self.y = n[1]
  21. self.parent = None
  22. class OBiRrt:
  23. def __init__(self, x_start, x_goal, step_len,
  24. goal_sample_rate, search_radius, iter_max):
  25. self.x_init = Node(x_start)
  26. self.x_goal = Node(x_goal)
  27. self.step_len = step_len
  28. self.goal_sample_rate = goal_sample_rate
  29. self.search_radius = search_radius
  30. self.iter_max = iter_max
  31. self.Ta = [self.x_init]
  32. self.Tb = [self.x_goal]
  33. self.c_best = np.inf
  34. self.sigma_best = {}
  35. self.path = []
  36. self.visited = []
  37. self.env = env.Env()
  38. self.plotting = plotting.Plotting(x_start, x_goal)
  39. self.utils = utils.Utils()
  40. # self.fig, self.ax = plt.subplots()
  41. self.x_range = self.env.x_range
  42. self.y_range = self.env.y_range
  43. self.obs_circle = self.env.obs_circle
  44. self.obs_rectangle = self.env.obs_rectangle
  45. self.obs_boundary = self.env.obs_boundary
  46. def planning(self):
  47. for k in range(self.iter_max):
  48. x_rand = self.generate_random_node(self.x_goal, self.goal_sample_rate)
  49. x_nearest = self.nearest_neighbor(self.Ta, x_rand)
  50. x_new = self.steer(x_nearest, x_rand)
  51. X_near_ind = self.nearest_neighbor(self.Ta, x_new)
  52. L_near = []
  53. for x_near_ind in X_near_ind:
  54. x_near = self.Ta[x_near_ind]
  55. sigma_near = self.steer(x_near, x_new)
  56. c_near = self.cost(x_near) + self.cost(sigma_near)
  57. L_near.append((c_near, x_near, sigma_near))
  58. L_near.sort()
  59. for c_near, x_near, sigma_near in L_near:
  60. if c_near + self.cost_to_go()
  61. def cost_to_go(self, x_start, x_goal):
  62. return math.hypot(x_goal.x - x_start.x, x_goal.y - x_start.y)
  63. def generate_random_node(self, goal_point, goal_sample_rate):
  64. delta = self.utils.delta
  65. if np.random.random() > goal_sample_rate:
  66. return Node((np.random.uniform(self.x_range[0] + delta, self.x_range[1] - delta),
  67. np.random.uniform(self.y_range[0] + delta, self.y_range[1] - delta)))
  68. return goal_point
  69. @staticmethod
  70. def nearest_neighbor(V, node):
  71. return V[int(np.argmin([math.hypot(nd.x - node.x, nd.y - node.y) for nd in V]))]
  72. def steer(self, node_start, node_goal):
  73. dist, theta = self.get_distance_and_angle(node_start, node_goal)
  74. dist = min(self.step_len, dist)
  75. node_new = Node((node_start.x + dist * math.cos(theta),
  76. node_start.y + dist * math.sin(theta)))
  77. node_new.parent = node_start
  78. return node_new
  79. def find_near_neighbor(self, V, node):
  80. n = len(V) + 1
  81. r = min(self.search_radius * math.sqrt((math.log(n) / n)), self.step_len)
  82. dist_table = [(nd.x - node.x) ** 2 + (nd.y - node.y) ** 2 for nd in V]
  83. dist_table_index = [ind for ind in range(len(dist_table)) if dist_table[ind] <= r and
  84. not self.utils.is_collision(node, V[ind])]
  85. return dist_table_index
  86. def get_new_cost(self, node_start, node_end):
  87. dist, _ = self.get_distance_and_angle(node_start, node_end)
  88. return self.cost(node_start) + dist
  89. @staticmethod
  90. def cost(node_p):
  91. node = node_p
  92. cost = 0.0
  93. while node.parent:
  94. cost += math.hypot(node.x - node.parent.x, node.y - node.parent.y)
  95. node = node.parent
  96. return cost
  97. @staticmethod
  98. def get_distance_and_angle(node_start, node_end):
  99. dx = node_end.x - node_start.x
  100. dy = node_end.y - node_start.y
  101. return math.hypot(dx, dy), math.atan2(dy, dx)