Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. from rrt_node import *
  2. import matplotlib.pyplot as plt
  3. from sympy import *
  4. from sympy.geometry import *
  5.  
  6. def check_if_feasible(c1, c2, obstacles):
  7. for obstacle in obstacles:
  8. if len(obstacle.intersection(Segment(Point(c2[0,0], c2[1,0]), Point(c1[0,0], c1[1,0])))) > 0:
  9. return False
  10. return True
  11.  
  12.  
  13. def sample_configuration():
  14. return np.random.uniform(np.array([0.0, 0.0]).reshape(2, 1), np.array([6.0, 6.0]).reshape(2, 1))
  15.  
  16.  
  17. def extend_rrt(rrt_nodes, rejected_nodes, configuration, count, threshold, obstacles, heuristic=False):
  18. epsilon = 0.15
  19. best_rrt_node = rrt_nodes[0]
  20. best_rejected_node = rejected_nodes[0]
  21.  
  22. for node in rejected_nodes:
  23. if np.linalg.norm(node.node_pos - configuration) < np.linalg.norm(best_rejected_node.node_pos - configuration):
  24. best_rejected_node = node
  25.  
  26. for node in rrt_nodes:
  27. if np.linalg.norm(node.node_pos - configuration) < np.linalg.norm(best_rrt_node.node_pos - configuration):
  28. best_rrt_node = node
  29.  
  30. new_rej_configuration = best_rejected_node.node_pos + epsilon * (
  31. configuration - best_rejected_node.node_pos) / np.linalg.norm(
  32. configuration - best_rejected_node.node_pos)
  33.  
  34. if check_if_feasible(new_rej_configuration, best_rejected_node.node_pos, obstacles) and np.linalg.norm(best_rejected_node.node_pos - rej_nodes[0].node_pos) > 0:
  35. rej_nodes.append(TreeNode(new_rej_configuration))
  36.  
  37. if (heuristic is True and count > threshold and np.linalg.norm(best_rejected_node.node_pos - configuration) < np.linalg.norm(
  38. best_rrt_node.node_pos - configuration)):
  39. return 2
  40.  
  41. new_configuration = best_rrt_node.node_pos + epsilon * (configuration - best_rrt_node.node_pos) / np.linalg.norm(configuration - best_rrt_node.node_pos)
  42.  
  43. if heuristic is True:
  44. epsilon_counter = 0
  45. for node in nodes:
  46. if np.linalg.norm(node.node_pos - new_configuration) < 2.5 * epsilon:
  47. epsilon_counter += 1
  48.  
  49. if epsilon_counter >= 5:
  50. return 2
  51.  
  52. if not check_if_feasible(new_configuration, best_rrt_node.node_pos, obstacles):
  53. rej_nodes.append(TreeNode(new_configuration))
  54. return 3
  55.  
  56. new_node = TreeNode(new_configuration, best_rrt_node)
  57. rrt_nodes.append(new_node)
  58. best_rrt_node.children.append(new_node)
  59. return 1
  60.  
  61.  
  62. if __name__ == '__main__':
  63. epsilon = 0.15
  64. exp_nodes = []
  65. map_nodes_ = [np.array([float(i % int( 6 / epsilon)) * epsilon * 0.5, float(i / int(6 / epsilon)) * epsilon * 0.5]).reshape(2, 1) for i in range(int( 6 * 6 / (epsilon * epsilon)))]
  66.  
  67. nodes = [TreeNode(np.array([1, 2]))]
  68. rej_nodes = [TreeNode(np.array([10, 10]).reshape(2, 1))]
  69. obstacles = [Segment(Point(0, 1.8), Point(5.5, 1.8)), Segment(Point(0, 2.2), Point(5.2, 2.2)), Segment(Point(0, 1.8), Point(0, 8)),
  70. Segment(Point(5.2, 2.2), Point(5.2, 4)), Segment(Point(5.5, 1.8), Point(5.5, 4)), Segment(Point(0, 4), Point(5.2, 4)),
  71. Segment(Point(5.5, 4), Point(6, 4))]
  72.  
  73. """obstacles = [Segment(Point(0, 2), Point(2, 2)),
  74. Segment(Point(2, 2), Point(2, 1.5)),
  75. Segment(Point(2, 0), Point(2, 1)),
  76. Segment(Point(2, 1), Point(6, 1)),
  77. Segment(Point(2, 1.5), Point(5.5, 1.5)),
  78. Segment(Point(5.5, 1.5), Point(5.5, 5.5)),
  79. Segment(Point(6, 1), Point(6, 6)),
  80. Segment(Point(5.5, 5.5), Point(1.5, 5.5)),
  81. Segment(Point(6, 6), Point(1, 6)),
  82. Segment(Point(1.5, 5.5), Point(1.5, 3)),
  83. Segment(Point(1, 6), Point(1, 2.5)),
  84. Segment(Point(1.5, 3), Point(4.5, 3)),
  85. Segment(Point(1, 2.5), Point(5, 2.5)),
  86. Segment(Point(4.5, 3), Point(4.5, 4.5)),
  87. Segment(Point(5, 2.5), Point(5, 5)),
  88. Segment(Point(4.5, 4.5), Point(2.5, 4.5)),
  89. Segment(Point(5, 5), Point(2, 5))]"""
  90.  
  91. fig = plt.figure()
  92. plt.ylim(0, 6)
  93. plt.xlim(0, 6)
  94.  
  95. i = 0
  96. cnt = 0
  97. while i < 1000:
  98. conf = sample_configuration()
  99. z = extend_rrt(nodes, rej_nodes, conf, cnt, 100, obstacles, heuristic=True)
  100. if z != 2:
  101. exp_nodes.append(conf)
  102. cnt += 1
  103. elif z == 2:
  104. i -= 1
  105. i += 1 #3.5 3.5
  106. if np.linalg.norm(nodes[len(nodes) - 1].node_pos - np.array([2.5, 5.1]).reshape(2, 1)) < 0.25:
  107. print('break')
  108. break
  109.  
  110.  
  111. print(len(rej_nodes), cnt)
  112. input()
  113.  
  114. for obstacle in obstacles:
  115. x1, y1 = [obstacle.p1[0], obstacle.p2[0]], [obstacle.p1[1], obstacle.p2[1]]
  116. plt.plot(x1, y1, marker='.', color='r', markersize=0.5)
  117.  
  118.  
  119. rej_nodes.pop(0)
  120. for node_ in nodes:
  121. if node_.parent_node is not None:
  122. x1, y1 = [node_.node_pos[0, 0], node_.parent_node.node_pos[0, 0]], [node_.node_pos[1, 0], node_.parent_node.node_pos[1, 0]]
  123. plt.plot(x1, y1, marker='.', color='b', markersize=0.1)
  124. fig.canvas.draw()
  125. plt.pause(0.001)
  126.  
  127. for node_ in rej_nodes:
  128. plt.scatter(node_.node_pos[0, 0], node_.node_pos[1, 0], s=4, color='r')
  129. fig.canvas.draw()
  130. plt.pause(0.001)
  131.  
  132. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement