資源簡介
博文詳細介紹用MATLAB實現基于A*算法的路徑規劃(附完整的代碼,代碼逐行進行解釋)(四)--------固定障礙物,進一步對比 的附件,包含了文章介紹的完整的matlab文件

代碼片段和文件信息
%matlab初始化
clc;?????????????%清除命令窗口的內容
clear?all;???????%清除工作空間的所有變量,函數,和MEX文件
close?all;???????%關閉所有的figure窗口
%方格數及障礙物比例的設定
n?=?100;???%?產生一個n?x?n的方格,修改此值可以修改生成圖片的方格數
wallpercent?=?0.4;??%?這個變量代表生成的障礙物占總方格數的比例?,如0.5?表示障礙物占總格數的50%
Environmental_Set=0;?%這個參數用來選擇是否隨機生成障礙物,若設定為0,則使用上一次創建的環境信息,若設定為1,則重新隨機生成障礙物
Reset_GS=1;???%這個參數用來選擇是否重新設定起始點和終止點,若設定為1,開始重新設定起始點和終止點,同時需要將變量New_goalposind和New_startposind的值修改為你所選擇的起始點和終止點的索引值,設為0則關閉
New_startposind=4100;???New_goalposind=8200;??%若將Reset_GS設定為1,則需要將變量New_goalposind和New_startposind的值修改為你所選擇的起始點和終止點的索引值,要確保新設的這兩個點處沒有障礙物
%方格以及障礙物的創建
if(Environmental_Set)
[field?startposind?goalposind?costchart?fieldpointers]?=initializeField(nwallpercent);?%隨機生成包含障礙物,起始點,終止點等信息的矩陣
save(‘Environmental‘‘field‘‘startposind‘‘goalposind‘‘costchart‘‘fieldpointers‘?)
else
load(‘Environmental‘)
end
%重新設定起始點和終止點
if(Reset_GS)
[field?startposind?goalposind?costchart?fieldpointers]?=?Reset_G_S(field?startposind?goalposind?costchart?fieldpointersNew_startposindNew_goalposind);
end
%?路徑規劃中用到的一些矩陣的初始化
setOpen?=?[startposind];?setOpenCosts?=?[0];?setOpenHeuristics?=?[Inf];
setClosed?=?[];?setClosedCosts?=?[];
movementdirections?=?{‘R‘‘L‘‘D‘‘U‘};??%移動方向
%?這個函數用來隨機生成環境,障礙物,起點,終點
axishandle?=?createFigure(fieldcostchartstartposindgoalposind);????%將隨機生成的方格及障礙物的數據生成圖像
%%
%?這個while循環是本程序的核心,利用循環進行迭代來尋找終止點
while?~max(ismember(setOpengoalposind))?&&?~isempty(setOpen)
????[temp?ii]?=?min(setOpenCosts?+?setOpenHeuristics);?????%尋找拓展出來的最小值?
????
????%這個函數的作用就是把輸入的點作為父節點,然后進行拓展找到子節點,并且找到子節點的代價,并且把子節點距離終點的代價找到
????[costsheuristicsposinds]?=?findFValue(setOpen(ii)setOpenCosts(ii)?fieldgoalposind‘euclidean‘);
?
??setClosed?=?[setClosed;?setOpen(ii)];?????%?將找出來的拓展出來的點中代價最小的那個點串到矩陣setClosed?中?
??setClosedCosts?=?[setClosedCosts;?setOpenCosts(ii)];????%?將拓展出來的點中代價最小的那個點的代價串到矩陣setClosedCosts?中
??
??%?從setOpen中刪除剛才放到矩陣setClosed中的那個點
??%如果這個點位于矩陣的內部
??if?(ii?>?1?&&?ii?????setOpen?=?[setOpen(1:ii-1);?setOpen(ii+1:end)];
????setOpenCosts?=?[setOpenCosts(1:ii-1);?setOpenCosts(ii+1:end)];
????setOpenHeuristics?=?[setOpenHeuristics(1:ii-1);?setOpenHeuristics(ii+1:end)];
????
??%如果這個點位于矩陣第一行
??elseif?(ii?==?1)
????setOpen?=?setOpen(2:end);
????setOpenCosts?=?setOpenCosts(2:end);
????setOpenHeuristics?=?setOpenHeuristics(2:end);
????
??%如果這個點位于矩陣的最后一行
??else
????setOpen?=?setOpen(1:end-1);
????setOpenCosts?=?setOpenCosts(1:end-1);
????setOpenHeuristics?=?setOpenHeuristics(1:end-1);
??end
??
?%%??
??%?把拓展出來的點中符合要求的點放到setOpen?矩陣中,作為待選點
??for?jj=1:length(posinds)
??
????if?~isinf(costs(jj))???%?判斷該點(方格)處沒有障礙物
????????
??????%?判斷一下該點是否?已經存在于setOpen?矩陣或者setClosed?矩陣中
??????%?如果我們要處理的拓展點既不在setOpen?矩陣,也不在setClosed?矩陣中
??????if?~max([setClosed;?setOpen]?==?posinds(jj))
????????fieldpointers(posinds(jj))?=?movementdirections(jj);
????????costchart(posinds(jj))?=?costs(jj);
????????setOpen?=?[setOpen;?posinds(jj)];
????????s
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????13449??2020-10-10?16:06??A_ROAD_book03.m
?????文件???????17631??2020-10-10?16:03??A_ROAD_book05.m
?????文件???????13345??2020-10-10?16:08??A_ROAD_book02.m
評論
共有 條評論