A星算法matlab源码及详细注释.docx

A星算法matlab源码及详细注释.docx

ID:52563416

大小:19.86 KB

页数:13页

时间:2020-03-28

A星算法matlab源码及详细注释.docx_第1页
A星算法matlab源码及详细注释.docx_第2页
A星算法matlab源码及详细注释.docx_第3页
A星算法matlab源码及详细注释.docx_第4页
A星算法matlab源码及详细注释.docx_第5页
资源描述:

《A星算法matlab源码及详细注释.docx》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

1、A星算法matlab源码及详细注释functionastardemo%ASTARDEMODemonstrationofASTARalgorithm%%CopyrightBobL.Sturm,Ph.D.,AssistantProfessor%DepartmentofArchitecture,DesignandMediaTechnology%formerlyMedialogy%AalborgUniversityiBallerup%formerlyAalborgUniversityCopenhagen%$Revision:

2、0.1$$Date:2011Jan.1518h24:24$n=20;%fieldsizenxntiles20*20的界面wallpercent=0.45;%thispercentoffieldiswalls45%的界面作为阻碍物(墙)%createthenxnFIELDwithwallpercentwallscontainingmovementcosts,%astartingpositionSTARTPOSIND,agoalpositionGOALPOSIND,thecosts%Astarwillcomputemov

3、ementcostforeachtileCOSTCHART,%andamatrixinwhichtostorethepointersFIELDPOINTERS[field,startposind,goalposind,costchart,fieldpointers]=...initializeField(n,wallpercent);%初始化界面%initializetheOPENandCLOSEDsetsandtheircostssetOpen=[startposind];setOpenCosts=[0];setO

4、penHeuristics=[Inf];setClosed=[];setClosedCosts=[];movementdirections={'R','L','D','U'};%keeptrackofthenumberofiterationstoexitgracefullyifnosolutioncounterIterations=1;%createfiguresowecanwitnessthemagicaxishandle=createFigure(f

5、ield,costchart,startposind,goalposind);%aslongaswehavenotfoundthegoalorrunoutofspacestoexplorewhile~max(ismember(setOpen,goalposind))&&~isempty(setOpen)%ismember(A,B)返回与A同大小的矩阵,其中元素1表示A中相应位置的元素在B中也出现,0则是没有出现%fortheelementinOPENwiththesmallestcost[temp,i

6、i]=min(setOpenCosts+setOpenHeuristics);%从OPEN表中选择花费最低的点temp,ii是其下标(也就是标号索引)%findcostsandheuristicofmovingtoneighborspacestogoal%inorder'R','L','D','U'[costs,heuristics,posinds]=findFValue(setOpen(ii),setOpenCosts(ii),...field,goa

7、lposind,'euclidean');%扩展temp的四个方向点,获得其坐标posinds,各个方向点的实际代价costs,启发代价heuristics%putnodeinCLOSEDandrecorditscostsetClosed=[setClosed;setOpen(ii)];%将temp插入CLOSE表中setClosedCosts=[setClosedCosts;setOpenCosts(ii)];%将temp的花费计入ClosedCosts%updateOPENandtheirasso

8、ciatedcosts更新OPEN表分为三种情况if(ii>1&&ii<length(setOpen))%temp在OPEN表的中间,删除tempsetOpen=[setOpen(1:ii-1);setOpen(ii+1:end)];setOpenCosts=[setOpenCosts(1:ii-1);setOpen

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。