資源簡介
用A*算法進行二維路徑規劃,即AStar算法, matlab程序,可以直接運行
代碼片段和文件信息
function?astardemo_Hong
%ASTARDEMO?Demonstration?of?ASTAR?algorithm
%
%???Copyright?Bob?L.?Sturm?Ph.?D.?Assistant?Professor
%???Department?of?Architecture?Design?and?Media?Technology
%?????formerly?Medialogy
%???Aalborg?University?i?Ballerup
%?????formerly?Aalborg?University?Copenhagen
%???$Revision:?0.1?$??$Date:?2011?Jan.?15?18h24:24$
n?=?20;???%?field?size?n?x?n?tiles??20*20的界面
wallpercent?=?0.15;??%?this?percent?of?field?is?walls???45%的界面作為阻礙物(墻)
%?create?the?n?x?n?FIELD?with?wallpercent?walls?containing?movement?costs?
%?a?starting?position?STARTPOSIND?a?goal?position?GOALPOSIND?the?costs?
%?A?star?will?compute?movement?cost?for?each?tile?COSTCHART?
%?and?a?matrix?in?which?to?store?the?pointers?FIELDPOINTERS
[field?startposind?goalposind?costchart?fieldpointers]?=?...
??initializeField(nwallpercent);???%初始化界面
%?initialize?the?OPEN?and?CLOSED?sets?and?their?costs
setOpen?=?[startposind];?setOpenCosts?=?[0];?setOpenHeuristics?=?[Inf];
setClosed?=?[];?setClosedCosts?=?[];
movementdirections?=?{‘R‘‘L‘‘D‘‘U‘‘DR‘‘DL‘‘UR‘‘UL‘};
%?keep?track?of?the?number?of?iterations?to?exit?gracefully?if?no?solution
counterIterations?=?1;
%?create?figure?so?we?can?witness?the?magic
axishandle?=?createFigure(fieldcostchartstartposindgoalposind);
%?as?long?as?we?have?not?found?the?goal?or?run?out?of?spaces?to?explore
while?~max(ismember(setOpengoalposind))?&&?~isempty(setOpen)??%ismember(AB)返回與A同大小的矩陣,其中元素1表示A中相應位置的元素在B中也出現,0則是沒有出現
??%?for?the?element?in?OPEN?with?the?smallest?cost
??[temp?ii]?=?min(setOpenCosts?+?setOpenHeuristics);???%從OPEN表中選擇花費最低的點tempii是其下標(也就是標號索引)
??%?find?costs?and?heuristic?of?moving?to?neighbor?spaces?to?goal
??%?in?order?‘R‘‘L‘‘D‘‘U‘
??[costsheuristicsposinds]?=?findFValue(setOpen(ii)setOpenCosts(ii)?...
????fieldgoalposind‘euclidean‘);???????%擴展temp的四個方向點,獲得其坐標posinds,各個方向點的實際代價costs,啟發代價heuristics
??%?put?node?in?CLOSED?and?record?its?cost
??setClosed?=?[setClosed;?setOpen(ii)];?????%將temp插入CLOSE表中
??setClosedCosts?=?[setClosedCosts;?setOpenCosts(ii)];??%將temp的花費計入ClosedCosts
??%?update?OPEN?and?their?associated?costs??更新OPEN表?分為三種情況
??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);???%temp是OPEN表的第一個元素,刪除temp
????setOpenCosts?=?setOpenCosts(2:end);
????setOpenHeuristics?=?setOpenHeuristics(2:end);
??else?????%temp是OPEN表的最后一個元素,刪除temp
????setOpen?=?setOpen(1:end-1);
????setOpenCosts?=?setOpenCosts(1:end-1);
????setOpenHeuristics?=?setOpenHeuristics(1:end-1);
??end
??%?for?each?of?these?neighbor?spaces?assign?costs?and?pointers;?
??%?and?if?some?are?in?the?CLOSED?set?and?their?costs?are?smaller?
??%?update?their?costs?and?pointers
??for?jj=1:length(posinds)??????%對于擴展的四個方向的坐標
???
- 上一篇:圖像拼接程序
- 下一篇:基于直方圖雙峰特性的圖像分割Matlab代碼
評論
共有 條評論