路径规划——RRT算法

路径规划——RRT算法

算法原理

RRT算法的全称是快速扩展随机树算法(Rapidly Exploring Random Tree),它的思想是选取一个初始点作为根节点,通过随机采样,增加叶子节点的方式,生成一个随机扩展树,当随机树中的叶子节点包含了目标点或进入了目标区域,边可以在随机树中通过回溯的方式,找到这条从初始点到目标点的路径。

此算法的重点随机采样+步⻓限制+碰撞检测

算法流程:
1.初始化:以起点start为根节点,创建一棵树(通常用二叉树表示),树的根节点表示起始位置。
2.随机采样:在搜索空间中随机生成一个点x_rand。这个点可能在自由空间中,也可能在障碍物中。
3.寻找最近的节点:在当前的树中找到距离x_rand最近的节点x_near
4.扩展树:从x_near沿着指向x_rand的方向移动一小步,生成一个新的节点x_new。如果x_new在自由空间中(即不与障碍物碰撞),则将x_new加入到树中,并将x_nearn_new用一条边连接。
5.检查目标:检查x_new是否在目标区域附近,这里的“附近”可以设置一个搜索距离来量化。如果是,则生成一条路径从起点到x_new,并结束算法。
6.迭代:重复步骤2~步骤5,直到找到目标点goal,或达到预设的迭代次数。

由于RRT采用随机采样的方法,RRT生成的路径通常不一定是最优路径,但可以通过多次运行RRT或结合其他优化算法来获得更优路径。

在这里插入图片描述

算法实现

import numpy as np
import random
import math
from itertools import combinations
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib.patches as patches

class RRT:
    def __init__(self,start,goal,obstacles,board_size,max_try,max_dist,goal_sample_rate,env) -> None:
        self.start = self.Node(start,None,0)
        self.goal = self.Node(goal,None,0)
        self.obstacles = obstacles
        self.board_size = board_size
        self.max_try = max_try # Number of iterations
        self.max_dist = max_dist # Maximum sampling distance
        self.goal_sample_rate = goal_sample_rate
        self.env = env
        self.inflation = 1
        self.searched = []

    class Node:
        def __init__(self,position,parent,cost):
            self.position = position
            self.parent = parent
            self.cost = cost

    def run(self):
        cost,path = self.plan()
        self.visualize(cost,path)

    def plan(self):
        self.searched.append(self.start)
        closed_list = {
   self.start.position: self.start}
        # plan max_try times
        for i in range(self.max_try):
            node_rand = self.get_random_node()
            # visited
            if node_rand.position in closed_list:
                continue
            # Get the nearest neighbor node
            node_near = self.get_nearest_neighbor(list(closed_list.values()),node_rand)
            # Get the new node
            node_new = self.get_new_node(node_rand,node_near)
            if node_new
### RRTRRT* 和 RRT_Connect 算法的差异 #### RRT (Rapidly-exploring Random Tree) RRT是一种基础性的路径规划算法,主要特点是能够在高维空间中高效探索未知区域。该算法通过在配置空间内随机选取样本点并逐步构建一棵由初始位置出发的成长树来实现这一点。每当新增加一个节点时,会检查是否存在通往目标的位置的可能性,并且遵循一定的步长限制以确保每一步都是合理的[^4]。 ```python def rrt_algorithm(start, goal, obstacles): tree = {start} while True: random_point = sample_random_point() nearest_node = find_nearest(tree, random_point) new_node = extend_towards(nearest_node, random_point) if not collide(new_node, obstacles): add_to_tree(tree, new_node) if close_enough(new_node, goal): break return reconstruct_path(tree, start, goal) ``` #### RRT* RRT*作为RRT的一个改进版本,在保持原有特性的同时引入了优化机制。这意味着当新节点被加入到生长中的树里时,不仅考虑如何最接近随机选定点,还会评估现有路径的成本以便调整已有的分支使之更优。这种策略使得最终得到的结果不仅是可行解而且尽可能趋向于全局最优解[^3]。 ```python def rrt_star_algorithm(start, goal, obstacles): tree = {start: None} # key is node, value is parent for _ in range(max_iterations): random_point = sample_random_point() nearest_node = find_nearest(tree.keys(), random_point) new_node = steer(nearest_node, random_point) if not collide_segment(new_node, nearest_node, obstacles): near_nodes = get_nearby_points(tree.keys(), new_node) best_parent = choose_best_parent(near_nodes, new_node, obstacles) connect_and_update_costs(best_parent, new_node, tree) rewire_neighbors(near_nodes, new_node, tree) if close_enough(new_node, goal): final_path = backtrack_from_goal(tree, goal) return final_path or [] ``` #### RRT-Connect 不同于前两者单向增长的方式,RRT-Connect采用了双向搜索的方法——即同时从起点和终点各自建立一颗快速扩张随机树并向对方靠拢直到两棵树成功交汇形成完整的路径解决方案。这种方法显著提高了收敛速度特别是在面对遥远的目标或是狭窄通道等问题场景下表现尤为突出[^1]。 ```python def rrt_connect_algorithm(start, goal, obstacles): forward_tree = {start: None} backward_tree = {goal: None} success = False while not success and len(forward_tree) < max_attempts: q_rand = sample_free_space(obstacles) q_new_forward = grow_rrt(q_rand, forward_tree, obstacles) if q_new_forward is not None: forward_tree[q_new_forward] = closest_in_tree(forward_tree, q_rand) q_new_backward = grow_rrt(q_new_forward, backward_tree, obstacles) if q_new_forward else None if q_new_backward is not None: backward_tree[q_new_backward] = closest_in_tree(backward_tree, q_new_forward) if can_connect(q_new_forward, q_new_backward, obstacles): path = combine_paths(forward_tree, backward_tree, q_new_forward, q_new_backward) success = True return path if success else None ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笨小古

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值