算法实现1——一步一步实现RRT(算法原理及matlab代码)

article/2025/10/4 8:25:54

  首先我们得明白算法的原理,然后写出步骤。根据步骤可以写出主函数包括每一步的输入输出,怎么表示(基本的伪代码表示,当然如果可以也可以写成汉字形式的),最后一步一步写出代码,调试工作是必须的(建议:子函数尽量分开写,功能分明,便于调试)。好了,差不多就这样,开始做吧^-^

1建立地图,设置起始点,目标点,(障碍带)

2初始化参数:

顶点vertices=起始点q_start;

边edges=空集empty;

设置采样数目k;

初始化启发概率p——>q_goal;

3主循环

For k in range

3.1判断是否到达目标点isGoalOnQNearQNewEdge()

输入:q_new,q_goal,delta_q

输出:返回类型:布尔型。1到达,0未到达

操作内容:判断q_goal与q_new距离在delta_q范围内;

If q_new == q_goal

Break;

End For

 

3.2if rand<p q_rand = q_goal;elseq_rand=p(map_x,map,y),生成随机采样点

3.3在顶点表中寻找离q_rand最近的顶点q_near,根据delta_q距离得到新的子节点——q_new

输入:vertices,delta_q,  q_rand

输出:q_new,q_near

操作内容:根据q_rand与vertices的每个点的欧式距离得出q_near,然后根据方向向量得出q_new

3.4 判断是否加入顶点列表中isAddInVerticesList()

输入:map,q_new,vertices,edges

输出:返回类型:布尔型。1加入0不加入

操作内容:

  将它与父节点连接成边edge1,然后将其分解为10个点,间隔总长/10;

If  1

将新节点加入vertices

将edge1加入edges边集合[q_new,q_parent]

Else

Continue;

EndIf

4根据边集合中,返回顶点索引【行向量】,根据行向量索引在顶点集合中,返回路径

path=Findpathnode(q_goal,q_star vertices);

plot(path)

5 path_smooth = smooth(path,vertices,map)%光滑采用的是贪心策略

此处遇到的问题是由于从目标点向起点进行判断连接成的边是否在障碍区内

所以此时增加插值点个数50个

补充:代码及地图数据地址https://download.csdn.net/download/qinze5857/10901150

——下面是自写代码,用matlab发布形式粘贴的哈

Contents

  • color map
  • rrt tree %行是y坐标,列是x坐标
  • sub function
function My_RRT
 My_RRT
clc
clear
close all
all

color map

load maze.mat map
[map_height,map_width]=size(map); %行是高y,列是宽x
q_start = [206, 198]; %q s t a r t ( 1 ) : x宽 , q s t a r t ( 2 ) : y高
q_goal = [416, 612];
colormap=[1 1 10 0 01 0 00 1 00 0 1];
imshow(uint8(map),colormap)
hold on
maze.mat map
[map_height,map_width]=size(map); %行是高y,列是宽x
q_start = [206, 198]; %q s t a r t ( 1 ) : x宽 , q s t a r t ( 2 ) : y高
q_goal = [416, 612];
colormap=[1 1 10 0 01 0 00 1 00 0 1];
imshow(uint8(map),colormap)
hold on
警告: 图像太大,无法在屏幕上显示;将以
67% 显示 

rrt tree %行是y坐标,列是x坐标

%initial
vertices=q_start;
edges = [];
K=10000;
delta_q=50;
p=0.3;
q_rand=[];
q_near=[];
q_new=[];
%main loop
plot(q_start(2),q_start(1),'*b')
plot(q_goal(2),q_goal(1),'*y')
for k = 1:Karrived=is_goal_arrived(vertices,q_goal,delta_q);if arrivedvertices=[vertices;q_goal];edges = [edges;[size(vertices,1),size(vertices,1)-1]];break;endif rand <= pq_rand = q_goal;%q(1)宽x,q(2)高yelseq_rand = [randi(map_height),randi(map_width)];endif map( q_rand(1,1),q_rand(1,2) ) == 1 %map(1)height,map(2)widthcontinue;end[q_new,q_near,q_near_ind,vector_dir] = get_qnew_qnear(delta_q,q_rand,vertices);add_qnew = is_add_in_veritces(map,q_new,q_near,vector_dir,10);if add_qnewvertices=[vertices;q_new];r_v = size(vertices,1);edges = [edges;[r_v,q_near_ind]];elsecontinue;end
%     plot(q_near(1,1),q_near(2,1),'*b');plot([q_near(1,2),q_new(1,2)],[q_near(1,1),q_new(1,1)],'-b')drawnow
end
path =find_path_node(edges);
%plot base path
plot(vertices(path,2),vertices(path,1),'-r')
%smooth
path_smooth = smooth(path,vertices,map);
%plot smooth path
plot(vertices(path_smooth,2),vertices(path_smooth,1),'-g');

vertices=q_start;
edges = [];
K=10000;
delta_q=50;
p=0.3;
q_rand=[];
q_near=[];
q_new=[];
%main loop
plot(q_start(2),q_start(1),'*b')
plot(q_goal(2),q_goal(1),'*y')
for k = 1:Karrived=is_goal_arrived(vertices,q_goal,delta_q);if arrivedvertices=[vertices;q_goal];edges = [edges;[size(vertices,1),size(vertices,1)-1]];break;endif rand <= pq_rand = q_goal;%q(1)宽x,q(2)高yelseq_rand = [randi(map_height),randi(map_width)];endif map( q_rand(1,1),q_rand(1,2) ) == 1 %map(1)height,map(2)widthcontinue;end[q_new,q_near,q_near_ind,vector_dir] = get_qnew_qnear(delta_q,q_rand,vertices);add_qnew = is_add_in_veritces(map,q_new,q_near,vector_dir,10);if add_qnewvertices=[vertices;q_new];r_v = size(vertices,1);edges = [edges;[r_v,q_near_ind]];elsecontinue;end
%     plot(q_near(1,1),q_near(2,1),'*b');plot([q_near(1,2),q_new(1,2)],[q_near(1,1),q_new(1,1)],'-b')drawnow
end
path =find_path_node(edges);
%plot base path
plot(vertices(path,2),vertices(path,1),'-r')
%smooth
path_smooth = smooth(path,vertices,map);
%plot smooth path
plot(vertices(path_smooth,2),vertices(path_smooth,1),'-g');

<span style="color:#0000ff">end</span>

sub function

function arrived=is_goal_arrived(vertices,q_goal,delta_q)
%判断是否到达终点
dist=pdist2(vertices(end,:),q_goal);
if dist <= delta_qarrived=1;
elsearrived=0;
end
endfunction [q_new,q_near,q_near_ind,vector_dir] = get_qnew_qnear(delta_q,q_rand,vertices)
%获得节点中最近的和新节点
dist_rand = pdist2(vertices,q_rand);
[dist_min,q_near_ind]=min(dist_rand);
q_near=vertices(q_near_ind,:);
vector_dir =q_rand-q_near;
vector_dir = vector_dir./dist_min;
if dist_min > delta_q    %随机点到最近点的距离不确定q_new = floor( q_near+delta_q*vector_dir );
elseq_new=q_rand;
end
endfunction add_qnew = is_add_in_veritces(map,q_new,q_near,vector_dir,insert_p)
%判断是否加入到列表中,q_new,与edges_new
%输出:add_qnew=1加入 0不加入
%注意:sub2ind,[y高,x宽]=size(map),q_goal=[x宽,y高]
dist_new2near = norm(q_new - q_near);%此处有问题
dist_gap = dist_new2near/insert_p;
ii =1:insert_p;
insert_point = repmat(q_near,insert_p,1)+ii'.*dist_gap* vector_dir;
insert_point =[floor(insert_point);q_new];
insert_num = sub2ind(size(map),insert_point(:,1),insert_point(:,2));
or =find( map(insert_num)==1 );
if ~isempty(or)add_qnew=0;
elseadd_qnew=1;
endendfunction path =find_path_node(edges)
%返回路径 ,path代表在顶点中的行数,返回的是行向量
e=edges(end,2);
path = edges(end,:);
while trueind= find(edges(:,1)==e);tmp_e = edges(ind,:);e=tmp_e(2);path=[path,e];if e==1break;end
endendfunction path_smooth = smooth(path,vertices,map)
%光滑方法:从起点往前找
% path = fliplr(path);
path_smooth =path(end);
tmp_point = vertices(1,:);
while truel_p = length(path);for i=1:l_pvec = vertices( path(i),:) - tmp_point;vec_dir = vec/norm(vec);or_reduce = is_add_in_veritces(map ,vertices(path(i),: ),tmp_point,vec_dir,60);if or_reduce==1 %可缩减path_smooth = [path_smooth, path(i)];tmp_point = vertices(path(i),: );break;elsecontinue;endendvec_goal = vertices(end,:) - tmp_point;goal_dir = vec_goal/norm(vec_goal);or_goal = is_add_in_veritces(map , vertices(end,: ),tmp_point,goal_dir,60);if or_goal==1  %可以与目标点连接path_smooth = [path_smooth, path(1)];break;elseind_path = find(path==path(i));path=path(1:ind_path);end
endend
 arrived=is_goal_arrived(vertices,q_goal,delta_q)
%判断是否到达终点
dist=pdist2(vertices(end,:),q_goal);
if dist <= delta_qarrived=1;
elsearrived=0;
end
endfunction [q_new,q_near,q_near_ind,vector_dir] = get_qnew_qnear(delta_q,q_rand,vertices)
%获得节点中最近的和新节点
dist_rand = pdist2(vertices,q_rand);
[dist_min,q_near_ind]=min(dist_rand);
q_near=vertices(q_near_ind,:);
vector_dir =q_rand-q_near;
vector_dir = vector_dir./dist_min;
if dist_min > delta_q    %随机点到最近点的距离不确定q_new = floor( q_near+delta_q*vector_dir );
elseq_new=q_rand;
end
endfunction add_qnew = is_add_in_veritces(map,q_new,q_near,vector_dir,insert_p)
%判断是否加入到列表中,q_new,与edges_new
%输出:add_qnew=1加入 0不加入
%注意:sub2ind,[y高,x宽]=size(map),q_goal=[x宽,y高]
dist_new2near = norm(q_new - q_near);%此处有问题
dist_gap = dist_new2near/insert_p;
ii =1:insert_p;
insert_point = repmat(q_near,insert_p,1)+ii'.*dist_gap* vector_dir;
insert_point =[floor(insert_point);q_new];
insert_num = sub2ind(size(map),insert_point(:,1),insert_point(:,2));
or =find( map(insert_num)==1 );
if ~isempty(or)add_qnew=0;
elseadd_qnew=1;
endendfunction path =find_path_node(edges)
%返回路径 ,path代表在顶点中的行数,返回的是行向量
e=edges(end,2);
path = edges(end,:);
while trueind= find(edges(:,1)==e);tmp_e = edges(ind,:);e=tmp_e(2);path=[path,e];if e==1break;end
endendfunction path_smooth = smooth(path,vertices,map)
%光滑方法:从起点往前找
% path = fliplr(path);
path_smooth =path(end);
tmp_point = vertices(1,:);
while truel_p = length(path);for i=1:l_pvec = vertices( path(i),:) - tmp_point;vec_dir = vec/norm(vec);or_reduce = is_add_in_veritces(map ,vertices(path(i),: ),tmp_point,vec_dir,60);if or_reduce==1 %可缩减path_smooth = [path_smooth, path(i)];tmp_point = vertices(path(i),: );break;elsecontinue;endendvec_goal = vertices(end,:) - tmp_point;goal_dir = vec_goal/norm(vec_goal);or_goal = is_add_in_veritces(map , vertices(end,: ),tmp_point,goal_dir,60);if or_goal==1  %可以与目标点连接path_smooth = [path_smooth, path(1)];break;elseind_path = find(path==path(i));path=path(1:ind_path);end
endend


Published with MATLAB® R2015b


http://chatgpt.dhexx.cn/article/AcuQkZrE.shtml

相关文章

RRT、RRT-connect、RRT*等算法、A*等等路径规划算法

1 原始RRT算法运行结果&#xff1a;python&#xff0c;这里以2D_rrt及其衍生相关算法为例&#xff08;边做边更新&#xff09; CV搬运工们&#xff0c;先上github连接&#xff1a;&#xff08;点个赞呗&#xff09;&#xff08;不想要拿github包的后面有现成代码&#xff09;…

RRT路径规划算法

RRT路径规划算法 地图RRT算法原理路径平滑处理总结 RRT&#xff08;Rapidly-Exploring Random Tree&#xff09;算法是一种基于采样的路径规划算法&#xff0c;常用于移动机器人路径规划&#xff0c;适合解决高维空间和复杂约束下的路径规划问题。基本思想是以产生随机点的方式…

自动驾驶路径规划——基于概率采样的路径规划算法(RRT、RRT*)

目录 1. RRT算法背景1.1 RRT算法核心思想1.2 RRT算法优缺点 2. 经典RRT算法2.1 RRT算法流程2.2 RRT伪代码 3. 基于目标概率采样4. RRT*算法4.1 RRT与RRT*的区别4.2 RRT*算法详解4.2.1 RRT*算法总体伪代码4.2.2 重新选择父节点4.2.3 重新布线4.2.4 RRT*算法Choose Parent过程详解…

基于Python的RRT算法实现

基于Python的RRT算法实现 一、RRT算法原理及实现效果二、RRT算法python代码实现 本文为博主原创内容&#xff0c;未经博主允许不得转载。尊重他人知识成果&#xff0c;共同建立良好学习分享氛围&#xff0c;谢谢&#xff01; 一、RRT算法原理及实现效果 关于RRT算法的原理&…

基于采样的路径规划算法RRT的优化:RRT*,Kinodynamic-RRT*,Anytime-RRT *,Informed RRT *

基于采样的路径规划算法RRT的优化 RRT * 算法Kinodynamic-RRT*Anytime-RRT *Informed RRT *关于搜索树按搜索方向生长的计算方法 基本的基于采样的路径规划算法RRT&#xff0c;在地图中进行采样取点&#xff0c;直到新的节点取到终点的一定阈值范围内&#xff0c;视为查找到路径…

RRT*算法图解

原文链接&#xff1a; https://blog.csdn.net/yuxuan20062007/article/details/88843690 【运动规划RRT*算法图解】 https://blog.csdn.net/weixin_43795921/article/details/88557317 尽管RRT算法是一个相对高效率&#xff0c;同时可以较好的处理带有非完整约束的路径规划问题…

快速扩展随机树(RRT)算法

RRT是Steven M. LaValle和James J. Kuffner Jr.提出的一种通过随机构建Space Filling Tree实现对非凸高维空间快速搜索的算法。该算法可以很容易的处理包含障碍物和差分运动约束的场景&#xff0c;因而广泛的被应用在各种机器人的运动规划场景中。 1. Basic RRT算法 原始的RR…

RRT算法及其部分改进算法介绍

基于采样的运动规划算法-RRT&#xff08;Rapidly-exploring Random Trees&#xff09; RRT&#xff1a;一种通过随机构建Space Filling Tree实现对非凸高维空间快速搜索的算法。该算法可以很容易的处理包含障碍物和差分运动约束的场景&#xff0c;被广泛的应用在各种机器人的运…

RRT*算法

简介 RRT* 和RRTconnect一样&#xff0c;是对RRT算法的优化。RRT算法的一个问题在于&#xff0c;它只是找到了可行的路径&#xff0c;不能保证路径是相对优化的。RRT*算法在每次迭代后&#xff0c;都会在局部更新搜索树&#xff0c;以优化路径。 多了两个过程&#xff0c;为&…

RRT算法介绍

RRT算法介绍 RRT算法原理介绍&#xff1a;RRT搜索树与树的生长相类似&#xff0c;即不断生长的同时又向四周扩散。算法以路径起点Xstart作为随机树T的根节点&#xff0c;树中节点xj用集合V存储&#xff0c;节点间的连接用连接边集E存储&#xff0c;所有节点xj满足属于集合Xfree…

【规划】RRT*算法图解

尽管RRT算法是一个相对高效率&#xff0c;同时可以较好的处理带有非完整约束的路径规划问题的算法&#xff0c;并且在很多方面有很大的优势&#xff0c;但是RRT算法并不能保证所得出的可行路径是相对优化的。因此许多关于RRT算法的改进也致力于解决路径优化的问题&#xff0c;R…

RRT算法简介

声明&#xff1a;本文为转载内容非原创&#xff0c;来源会在文末声明&#xff0c;绝无冒犯之意&#xff0c;只为一时复习之方便&#xff0c;侵权必删&#xff01; 感谢原作者写出如此优秀的博文&#xff0c;让我对RRT算法有个大致的理解。 对RRT算法感兴趣&#xff0c;是因为…

RRT 算法原理以及过程演示

RRT 适用于涉及非完整约束场合下的路径规划问题。 RRT 算法为一种递增式的构造方法&#xff0c;在构造过程中&#xff0c;算法不断在搜索空间中随机生成状态点&#xff0c;如果该点位于无碰撞位置&#xff0c;则寻找搜索树中离该节点最近的结点为基准结点&#xff0c;由基准结点…

【机器人学:运动规划】快速搜索随机树(RRT---Rapidly-exploring Random Trees)入门及在Matlab中演示

快速搜索随机树&#xff08;RRT -Rapidly-ExploringRandom Trees&#xff09;&#xff0c;是一种常见的用于机器人路径&#xff08;运动&#xff09;规划的方法&#xff0c;它本质上是一种随机生成的数据结构—树&#xff0c;这种思想自从LaValle在[1]中提出以后已经得到了极大…

【自动驾驶轨迹规划之RRT算法】

目录 1 RRT算法的简介 2 RRT算法原理 2.1 算法流程 2.2 算法伪代码 2.3 算法流程图 3 RRT算法matlab实现 3.1 测试地图 3.2 distance函数 3.3 RRT算法 3.4 动画效果 4 RRT的缺陷 1 RRT算法的简介 天下武功唯快不破&#xff0c;快是 RRT 的最大优势。RRT 的思想是快…

RRT算法

简介 RRT 算法&#xff08;快速扩展随机树&#xff0c;rapidly exploring random tree&#xff09;是一种随机性算法&#xff0c;它可以直接应用于非完整约束系统的规划&#xff0c;不需进行路径转换&#xff0c;所以它的算法复杂度较小&#xff0c;尤为适用于高维多自由度的系…

RRT(快速随机搜索树)算法原理及代码实践

RRT算法简介 RRT 算法为一种递增式的路径规划算法&#xff0c;算法不断在搜索空间中随机生成采样点&#xff0c;如果该点位于无碰撞位置&#xff0c;则寻找搜索树中离该节点最近的结点为基准结点&#xff0c;由基准结点出发以一定步长朝着该随机结点进行延伸&#xff0c;延伸线…

RRT算法原理和代码详解(快速扩展随机树)

文章目录 优缺点伪代码具体流程效率问题代码 优缺点 优缺点先明说&#xff0c;优点RRT Star适用于任何地图&#xff0c;不像A Star&#xff0c;Dijkstra那样受限于栅格地图。 缺点&#xff1a;1.找到的路径可能不是最优的&#xff1b;2.路径可能不符合机器人的运动学动力学模型…

RRT与RRT*算法具体步骤与程序详解(python)

提示&#xff1a;前面写了A*、Dijkstra算法 文章目录 前言一、RRT的原理与步骤二、RRT算法编写的步骤1.算法步骤2.算法的实现 三、RRT*算法编写的步骤1.算法的步骤2.算法的实现 三、所有程序附录RRT算法RRT*算法 前言 RRT和RRT*的区别&#xff1a; RRT的中文名为快速随机探索…

RRT算法原理图解

RRT算法原理图解 开始 本人很懒&#xff0c;习惯了只看不写。废话少说&#xff0c;直奔主题&#xff1a;原始RRT算法原理图文简介&#xff08;图都是我自己按照步骤一幅幅画的——闲的蛋疼&#xff0c;但应该比较直观易懂&#xff0c;能被借鉴参考也算我的功德&#xff09;。 R…