A*算法程序、结果分析与思考
(2021年6月3日重新测试成功,有问题欢迎评论和私信我,我看到就会回复)
2.0 使用方法
建立一个文件夹,里面要包含下面的9个m文件,每个m文件的代码都在下面写好了,复制进去。都弄好以后,运行createMAP.m文件,就好了。
如果还是弄不成的,我自己上传了测试好的文件夹,大伙可以下载下来测试:花费5积分。
Astar算法matlab.zip
结果展示:
2.1 程序目录
1.createMAP.m(总调用程序)
2.AStar.m
3.FindList.m
4.Getpath.m
5.isObstacle.m
6.isopen.m
7.MotionModel.m
8.plot_map.m
9.h.m
2.2 程序
1. createMAP.m(生成地图、包括障碍点、鼠标选取起点、终点以及主要运行程序)
clear all;
clear all;
figure;
MAX_X=50;
MAX_Y=50;
map.XYMAX=50;
p_obstocle = 0.3;%障碍率
O = ones(MAX_X,MAX_Y)*p_obstocle;%生成一个50*50的矩阵,里面都是0.3
MAP = 9999*((rand(MAX_X,MAX_Y))>O)-1;%随机生成50*50的矩阵,里面0到1之间随机,大于0.3的返回1,小于的返回0,再都乘9999,都减一。里面一堆9998和-1
j=0;
x_val = 1;
y_val = 1;
axis([0.5 MAX_X+0.5 0.5 MAX_Y+0.5])%坐标轴范围和纵横比
set(gca,'YTick',0:1:MAX_Y);
set(gca,'XTick',0:1:MAX_X);%设定xy坐标轴显示轴的刻度线
for i = 1:map.XYMAX+3
line([-0.5,map.XYMAX+1.5],[i-1.5,i-1.5]);
end
for j = 1:map.XYMAX+3
line([j-1.5,j-1.5],[-0.5,map.XYMAX+1.5]);
end
hold on; %添加新绘图时候保留原绘图
obstacle=[];
for i=1:MAX_X %遍历map数组,是-1的画红叉
for j=1:MAX_Y
if MAP(i,j) == -1
obstacle=[obstacle;[i j]];
plot(i,j,'rx');%在每一格是障碍物的地方画红色叉
end
end
end
%%地图上选择起始位置
pause(1);
h=msgbox('Please Select the Vehicle initial position using the Left Mouse button');%出现一句话
uiwait(h,5);%出现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,yval,'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