A*算法路径规划之Matlab实现

2023-05-16

A*算法路径规划之matlab实现

A*算法是一种传统的路径规划算法,相较于Dijkstra算法,其引入了启发式算子,有效的提高了路径的搜索效率。主要步骤包括:
1)设置起始点、目标点以及带障碍物的栅格地图
2)选择当前节点的可行后继节点加入到openlist中
3)从openlist中选择成本最低的节点加入closelist节点
4)重复执行步骤2和步骤3,直到当前节点到达目标点,否则地图中不存在可行路径
5)从closelist中选择从起点到终点的路径,并画图展示

所有代码如下(均可直接运行):

  1. 执行脚本,其功能是产生地图并设置起始点和目标点,通过调用astarf函数,构建closelist,并从closelist中找到路径
clear all;
clear all;
figure;
MAX_X=50;
MAX_Y=50;
dm=[1,1
    1,0
    0,1
    -1,1
    -1,-1
    0,-1
    -1,0
    1,-1];  %八个方向
p_obstocle = 0.3;%障碍率
O = ones(MAX_X,MAX_Y)*p_obstocle;
MAP = 9999*((rand(MAX_X,MAX_Y))>O)-1;
j=0;
x_val = 1;
y_val = 1;
axis([1 MAX_X+1 1 MAX_Y+1])
set(gca,'YTick',0:1:MAX_Y);
set(gca,'XTick',0:1:MAX_X);
grid on;
hold on;
for i=1:MAX_X
    for j=1:MAX_Y
        if MAP(i,j) == -1
            plot(i+.5,j+.5,'rx');
        end
    end
end
%%地图上选择起始位置
pause(1);
h=msgbox('Please Select the Vehicle initial position using the Left Mouse button');
uiwait(h,5);
if ishandle(h) == 1
    delete(h);
end
xlabel('Please Select the Vehicle initial position ','Color','black');
but=0;
while (but ~= 1) %Repeat until the Left button is not clicked
    [xval,yval,but]=ginput(1);
    xval=floor(xval);
    yval=floor(yval);
end
xStart=xval;%Starting Position
yStart=yval;%Starting Position
MAP(xval,yval) = 0;
 plot(xval+.5,yval+.5,'bo');
%%地图上选择目标点
pause(1);
h=msgbox('Please Select the Target using the Left Mouse button in the space');
uiwait(h,5);
if ishandle(h) == 1
    delete(h);
end
xlabel('Please Select the Target using the Left Mouse button','Color','black');
but = 0;
while (but ~= 1) %Repeat until the Left button is not clicked
    [xval,yval,but]=ginput(1);
end
xval = floor(xval);
yval = floor(yval);
xTarget = xval;
yTarget = yval;
MAP(xval,yval) = 9998;
plot(xval+.5,yval+.5,'gd');
text(xval+1,yval+.5,'Target');
node = [xStart,yStart,xTarget,yTarget];
save map MAP;
save point node;

path=astarf(node,MAP);
[rnp,cnp]=size(path);
num=rnp-1;
curpoint=path(rnp,1:2);
while num>1 
    plot(curpoint(1,1)+.5,curpoint(1,2)+.5,'k>');
    for pj=1:8
       if(curpoint(1,1)+dm(pj,1)==path(num,1)&&curpoint(1,2)+dm(pj,2)==path(num,2)) 
           if(curpoint(1,1)+dm(pj,1)==path(num-1,1)&&curpoint(1,2)+dm(pj,2)==path(num-1,2)) 
              curpoint=path(num-1,1:2);
              step=2;
           else
               curpoint=path(num,1:2);
               step=1;
           end
       end
    end
    plot(curpoint(1,1)+.5,curpoint(1,2)+.5,'k>');
    num=num-step;
end
  1. astarf函数,其功能是不断更新openlist和closelist
function [closelist] = astarf(setpoint,map)
sp=setpoint(1,1:2);%  起始点
dp=setpoint(1,3:4);% 目标点
cp=sp; %当前点
% h=hdistance(cp,dp);% 当前点与目标点的曼哈顿距离
openlist=[cp,hdistance(cp,dp)+hdistance(cp,sp),hdistance(cp,dp)];%opne 集合
    closelist=[];%clsoe 集合
existlist=[];
while judge(cp,dp)==0  %未到达目标点
    
    openlist=sortrows(openlist,[3,4]);% 先按照g排序,再按照h排序
    best=openlist(1,:); %最优子代
    cp=best(1,1:2);  %当前节点
    
    fprintf("x=%d,y=%d\n",best(1,1),best(1,2));
    closelist=[closelist;best]; %从openlist中选择best加入到closelist
    openlist(1,:)=[];  %移除已经加入close的节点
    existlist=[existlist;openlist;closelist];
    openlist=[openlist;findp(existlist,best,map,sp,dp)];
 
end
closelist=closelist(:,1:2);
end
  1. 后继节点探索函数,探索中,不能超越地图边界,且不能探索已经在openlist和closelist中已经存在的节点
function [rp] = findp(ep,b,m,sp,dp)
%寻找周围节点
d=[1,1
    1,0
    0,1
    -1,1
    -1,-1
    0,-1
    -1,0
    1,-1];  %八个方向
rp=[];
[r,c]=size(ep);  %计算openlist行数
for di=1:8
    flagf=0;
    rn=r;
    if(b(1,1)+d(di,1)>=1&&b(1,1)+d(di,1)<=50&&b(1,2)+d(di,2)>=1&&b(1,2)+d(di,2)<=50) % 不越出边界
    if(m((b(1,1)+d(di,1)),b(1,2)+d(di,2))>0)  %候选节点不是障碍物
        fp=[b(1,1)+d(di,1),b(1,2)+d(di,2)];
        while rn>0
            if((b(1,1)+d(di,1))==ep(rn,1)&&(b(1,2)+d(di,2))==ep(rn,2)) %此点已经探索过
                flagf=1;
                break;
            end
            rn=rn-1;
        end
        if flagf==0
        plot(b(1,1)+d(di,1)+.5,b(1,2)+d(di,2)+.5,'yh');
        fp=[fp,hdistance(fp,sp)+hdistance(fp,dp),hdistance(fp,dp)];
        rp=[rp;fp];
        end
    end
    end
end
  1. 辅助函数,曼哈顿距离计算函数和相同节点判断函数
function [hd] = hdistance(p1,p2)
hd=((p2(1,1)-p1(1,1))^2+(p2(1,2)-p1(1,2))^2)^0.5;%曼哈顿距离
end
function [flagd] = judge(p1,p2)
if(p1(1,1)==p2(1,1)&&p1(1,2)==p2(1,2))
    flagd=1;
else
    flagd=0;
end
end

执行效果如下图所示:
在这里插入图片描述
声明:此代码系作者独立撰写,能力有限,算法尚且不能保证找到最优路径,但是算法不存在错误。

注:起始点、目标点以及地图创建代码,来自MOOC网北京理工大学无人驾驶车辆课程所提供的资料包

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

A*算法路径规划之Matlab实现 的相关文章

随机推荐

  • VS Code远程SSH免密登录配置

    最近更新了VS Code之后 xff0c 发现Remote ssh拓展里的端口转发功能没了 xff0c 很伤心 xff0c 在探索的同时 xff0c 顺手配置了一下VS Code ssh免密登录 xff0c 以省去每次连接远程文件夹时输入两
  • 目标检测之一(传统算法和深度学习的源码学习)

    目标检测之一 xff08 传统算法和深度学习的源码学习 xff09 本系列写一写关于目标检测的东西 xff0c 包括传统算法和深度学习的方法都会涉及到 xff0c 注重实验而不着重理论 xff0c 理论相关的看论文去哈 xff0c 主要依赖
  • FreeRTOS中任务切换过程的分析

    FreeRTOS中Pendsv任务切换过程的分析 一 Pendsv中断任务解析 xff08 1 xff09 uxCriticalNesting 是进入临界区的次数 xff08 2 xff09 pxCurrentTCB是FreeRTOS运行时
  • CentOS6关闭防火墙使用以下命令

    cmd命令关闭防火墙 net stop mpssvc CentOS6关闭防火墙使用以下命令 xff0c 临时关闭 service iptables stop 禁止开机启动 chkconfig iptables off CentOS7中若使用
  • 《软件工程》试题举例-简答题

    Please give out 3 pieces of recommendations regarding language independent good programming practice 6 marks 良好的编程实践的建议
  • 2020届电子信息类专业保研经历分享

    文章目录 一 个人基本情况二 初心三 夏令营 九推情况介绍1 上海交大自动化系直硕面试 xff08 7月8日 xff09 2 中科大信息学院夏令营 xff08 7月15日 xff09 3 中科院自动化所夏令营 xff08 7月23日 xff
  • RGB图与灰度图相互转换关系表达式

    RGB图转灰度图 1 Y 61 0 3R 43 0 59G 43 0 11B 2 平均值法 xff0c 将RGB平均 灰度图转RGB图 先将单通道的灰度图转为三通道的RGB图 xff0c 各通道值的初值赋值为与灰度值相同 然后按照下式映射关
  • sklearn包导入错误:ImportError: cannot import name ‘Type‘解决办法

    在python3 5环境下使用pip直接安装sklearn包后 xff0c 导入出现如下错误 xff1a 仔细观察报错信息可以发现 xff0c 出错的是sklearn中使用到的scipy包 单独导入scipy包发现出错 xff1a 看来 x
  • PyTorch Dataloader报错ValueError: num_samples的另一种可能原因

    先粘报错信息 xff1a Traceback most recent call last File train py line 169 in train test File train py line 29 in train test da
  • Focal loss变种汇总

    VariFocal loss 只对负样本做难易样本挖掘 xff08 正样本数量少 xff0c 不做loss压缩 xff09 Generalized Focal loss xff1a quality focal loss 43 distrib
  • 视觉Transformer中的位置编码方式

    绝对位置编码 基本形式 xff1a x 61 x 43 p 可学习的绝对位置编码 xff08 ViT xff09 ViT中提出的位置编码方式简单粗暴 xff0c 设置一组可学习的编码tokens xff0c 并在patch embeding
  • 秋招问题汇总

    1 Python变量作用域 xff1a 局部作用域 xff08 Local xff0c 简写为 L xff09 作用于闭包函数外的函数中的作用域 xff08 Enclosing xff0c 简写为 E xff09 全局作用域 xff08 G
  • 38、OpenCV之C++教程

    一 OpenCV的下载与安装 下载完成后会得到一个 opencv 3 4 15 vc14 vc15 exe 文件 点击运行后会生成一个文件夹 此文件夹为下一步工程创建使用 xff0c 文件夹可移动 复制和重命名 xff0c 这里命名如下 x
  • Java大数据之路--HDFS详解(3)--基本命令

    HDFS 分布式文件存储系统 基本命令 目录 HDFS 分布式文件存储系统 基本命令 一 常见命令 二 其他命令 一 常见命令 命令 说明 hadoop fs mkdir park 在hdfs 的根目录下 xff0c 创建 park目录 h
  • C# 连接 SqlServer 数据库

    目录 一 创建表 二 给表添加数据 三 新建 C 项目 四 SqlServerHelper 五 连接数据库 一 创建表 首先 xff0c 新建一个数据库 Test xff0c 然后新建一个表 Users xff0c 字段名如下图 xff0c
  • org.xml.sax.SAXParseException的错误解决 2020-11-20

    span class token number 2020 span span class token operator span span class token number 11 span span class token operat
  • JS如何优雅的删除对象中的指定属性?

    要优雅的话 xff0c 使用 Lodash 的 omit 方法移除不要的属性 xff1a const object 61 a 1 b 2 c 3 const result 61 omit object a c 61 gt b 2 或者用 p
  • python 使用 isdigit 判断字符串中是否只由数字组成

    span class token operator span span class token operator span span class token operator span span class token operator s
  • 快速排序详解(Java实现)

    一 快速排序的基本思想 每一轮的排序都会将区域分割成两个独立的分区 xff0c 其中左分区的序列的所有值均会比右分区的所有值小 然后对子分区进行同样的分割操作 xff0c 最后达到整体有序 在排序的过程中 xff0c 由于已经分开的两部分的
  • A*算法路径规划之Matlab实现

    A 算法路径规划之matlab实现 A 算法是一种传统的路径规划算法 xff0c 相较于Dijkstra算法 xff0c 其引入了启发式算子 xff0c 有效的提高了路径的搜索效率 主要步骤包括 xff1a 1 xff09 设置起始点 目标