• 大小: 1.07MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-10-21
  • 语言: 其他
  • 标签: 路径规划  A*算法  

资源简介

A星算法实现路径规划,可以直接运行代码。随机生成起点和终点,采用A*算法实现最短路径,同时有绘图展现最终结果。

资源截图

代码片段和文件信息

function astardemo
%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
wallpercent = 0.45;  % this percent of field is walls

% 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‘};

% 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)
  % for the element in OPEN with the smallest cost
  [temp ii] = min(setOpenCosts + setOpenHeuristics);
  % 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‘);
  % put node in CLOSED and record its cost
  setClosed = [setClosed; setOpen(ii)];
  setClosedCosts = [setClosedCosts; setOpenCosts(ii)];
  % update OPEN and their associated costs
  if (ii > 1 && ii < length(setOpen))
    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
  % 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)
    % if cost infinite then it‘s a wall so ignore
    if ~isinf(costs(jj))
      % if node is not in OPEN or CLOSED then insert into costchart and 
      % movement pointers and put node in OPEN
      if ~max([setClosed; setOpen] == posinds(jj))
        fieldpointers(posinds(jj)) = movementdirections(jj);
        costchart(posinds(jj)) = costs(jj);
        setOpen = [setOpen; posinds(jj)];
        setOpenCosts = [setOpenCosts; costs(jj)]

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-07-05 13:33  A-star-master\
     目录           0  2018-07-05 13:33  A-star-master\C++\
     目录           0  2018-07-05 13:33  A-star-master\C++\Astar\
     文件         939  2018-07-05 13:33  A-star-master\C++\Astar\Astar.sln
     文件       27136  2018-07-05 13:33  A-star-master\C++\Astar\Astar.v12.suo
     目录           0  2018-07-05 13:33  A-star-master\C++\Astar\Astar\
     文件        5754  2018-07-05 13:33  A-star-master\C++\Astar\Astar\Astar.cpp
     文件        4435  2018-07-05 13:33  A-star-master\C++\Astar\Astar\Astar.vcxproj
     文件        1277  2018-07-05 13:33  A-star-master\C++\Astar\Astar\Astar.vcxproj.filters
     目录           0  2018-07-05 13:33  A-star-master\C++\Astar\Astar\Debug\
     文件        2041  2018-07-05 13:33  A-star-master\C++\Astar\Astar\Debug\Astar.log
     文件      339332  2018-07-05 13:33  A-star-master\C++\Astar\Astar\Debug\Astar.obj
     文件     2031616  2018-07-05 13:33  A-star-master\C++\Astar\Astar\Debug\Astar.pch
     目录           0  2018-07-05 13:33  A-star-master\C++\Astar\Astar\Debug\Astar.tlog\
     文件         176  2018-07-05 13:33  A-star-master\C++\Astar\Astar\Debug\Astar.tlog\Astar.lastbuildstate
     文件       16406  2018-07-05 13:33  A-star-master\C++\Astar\Astar\Debug\Astar.tlog\CL.read.1.tlog
     文件        2490  2018-07-05 13:33  A-star-master\C++\Astar\Astar\Debug\Astar.tlog\CL.write.1.tlog
     文件        3034  2018-07-05 13:33  A-star-master\C++\Astar\Astar\Debug\Astar.tlog\cl.command.1.tlog
     文件        2786  2018-07-05 13:33  A-star-master\C++\Astar\Astar\Debug\Astar.tlog\link.command.1.tlog
     文件        5688  2018-07-05 13:33  A-star-master\C++\Astar\Astar\Debug\Astar.tlog\link.read.1.tlog
     文件        1334  2018-07-05 13:33  A-star-master\C++\Astar\Astar\Debug\Astar.tlog\link.write.1.tlog
     文件       11205  2018-07-05 13:33  A-star-master\C++\Astar\Astar\Debug\stdafx.obj
     文件      363520  2018-07-05 13:33  A-star-master\C++\Astar\Astar\Debug\vc120.idb
     文件      430080  2018-07-05 13:33  A-star-master\C++\Astar\Astar\Debug\vc120.pdb
     文件        1466  2018-07-05 13:33  A-star-master\C++\Astar\Astar\ReadMe.txt
     文件         203  2018-07-05 13:33  A-star-master\C++\Astar\Astar\stdafx.cpp
     文件         219  2018-07-05 13:33  A-star-master\C++\Astar\Astar\stdafx.h
     文件         228  2018-07-05 13:33  A-star-master\C++\Astar\Astar\targetver.h
     目录           0  2018-07-05 13:33  A-star-master\C++\Astar\Debug\
     文件      104960  2018-07-05 13:33  A-star-master\C++\Astar\Debug\Astar.exe
     文件      453500  2018-07-05 13:33  A-star-master\C++\Astar\Debug\Astar.ilk
............此处省略7个文件信息

评论

共有 条评论