本文的源码来自以下博客:
Jump Point Search-跳点搜索法-原理&matlab代码-与A*算法比较(路径规划)_跳点搜索算法-CSDN博客
在复制了代码进行实验后,我发现代码中存在问题,主要如下:
insert_closelist(将跳点插入到closelist)函数,这个函数将跳点插入closelist,但是我发现,在其中一行它把父节点相同的跳点给覆盖了,它为什么要这么做呢?
function closelist = insert_closelist(point,closelist)
% 将跳点插入到closelist
% 此处显示详细说明
flag=0;
point_temp=[point(1,1:3),point(1,7:8)];
for i = 1:length(closelist(:,1))
if isequal(point(1,7:8),closelist(i,4:5))
closelist(i,:)=point_temp; %这一行
flag=1;
break
end
end
if flag
return
else
closelist=[point_temp;closelist];
end
end
原因在下面这段代码,这一段代码是将closelist中的点拿出用来绘制路径的,显然,它提取了closelist中所有的点,这是有问题的,因为一个父节点可能会有几个子节点,所以作者在插入时直接将父节点相同的节点覆盖掉了。
%将closelist中的节点x y坐标分别拿出 准备绘制路线
for pp=1:length(closelist(:,1))
x = closelist(pp,1);
y = closelist(pp,2);
A(pp,1)=x;
A(pp,2)=y;%这部分是将路径坐标拿出来另外存放
这种覆盖是有问题的,我们最后得到的路径会出现跳跃,因为它们的父节点是不连续的。要解决这个问题,只需要正常的将跳点加入closelist中,不进行覆盖。
insert_closelist.m
function closelist = insert_closelist(point,closelist)
% 将跳点插入到closelist
% 此处显示详细说明
%flag=0;
%point_temp=[point(1,1:3),point(1,7:8)];
%for i = 1:length(closelist(:,1))
% if isequal(point(1,7:8),closelist(i,4:5))
% closelist(i,:)=point_temp; %这一行
% flag=1;
% break
% end
%end
%if flag
% return
%else
closelist=[point_temp;closelist];
end
end
然后再选择路径时,正常的从终点不断的找到父节点,画出完整路径就行了,增加一个函数:
增加一个函数文件,此文件是从终点开始,通过父节点找路径的
GetPath.m
function path=GetPath(close,start)
ind=1;
path=[];
while 1
path=[path; close(ind,1:2)];
if isequal(close(ind,1:2),start) %到起点了
break;
end
for io=1:length(close(:,1))
if isequal(close(io,1:2),close(ind,4:5))%找到父节点
ind=io;
break;
end
end
end
end
路径搜索的代码是以前找到的一个A*算法代码中的,但我忘记出处了,就不标注了。