一、简介
A算法
A算法是一种典型的启发式搜索算法,建立在Dijkstra算法的基础之上,广泛应用于游戏地图、现实世界中,用来寻找两点之间的最短路径。A算法最主要的是维护了一个启发式估价函数,如式(1)所示。
f(n)=g(n)+h(n)(1)
其中,f(n)是算法在搜索到每个节点时,其对应的启发函数。它由两部分组成,第一部分g(n)是起始节点到当前节点实际的通行代价,第二部分h(n)是当前节点到终点的通行代价的估计值。算法每次在扩展时,都选取f(n)值最小的那个节点作为最优路径上的下一个节点。
在实际应用中,若以最短路程为优化目标,h(n)常取作当前点到终点的欧几里得距离(Euclidean Distance)或曼哈顿距离(Manhattan Distance)等。若令h(n)=0,表示没有利用任何当前节点与终点的信息,A算法就退化为非启发的Dijkstra算法,算法搜索空间随之变大,搜索时间变长。
A*算法步骤如下,算法维护两个集合:P表与Q表。P表存放那些已经搜索到、但还没加入最优路径树上的节点;Q表维护那些已加入最优路径树上的节点。
(1)P表、Q表置空,将起点S加入P表,其g值置0,父节点为空,路网中其他节点g值置为无穷大。
(2)若P表为空,则算法失败。否则选取P表中f值最小的那个节点,记为BT,将其加入Q表中。判断BT是否为终点T,若是,转到步骤(3);否则根据路网拓扑属性和交通规则找到BT的每个邻接节点NT,进行下列步骤:
①计算NT的启发值
f(NT)=g(NT)+h(NT)(2)
g(NT)=g(BT)+cost(BT, NT)(3)
其中,cost(BT, NT)是BT到NT的通行代价。
②如果NT在P表中,且通过式(3)计算的g值比NT原先的g值小,则将NT的g值更新为式(3)结果,并将NT的父节点设为BT。
③如果NT在Q表中,且通过式(3)计算的g值比NT原先的g值小,则将NT的g值更新为式(3)结果,将NT的父节点设为BT,并将NT移出到P表中。
④若NT既不在P表,也不在Q表中,则将NT的父节点设为BT,并将NT移到P表中。
⑤转到步骤(2)继续执行。
(3)从终点T回溯,依次找到父节点,并加入优化路径中,直到起点S,即可得出优化路径。
二、源代码
function varargout = Astar(varargin)
% ASTAR MATLAB code for Astar.fig
% ASTAR, by itself, creates a new ASTAR or raises the existing
% singleton*.
%
% H = ASTAR returns the handle to a new ASTAR or the handle to
% the existing singleton*.
%
% ASTAR('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in ASTAR.M with the given input arguments.
%
% ASTAR('Property','Value',...) creates a new ASTAR or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Astar_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Astar_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help Astar
% Last Modified by GUIDE v2.5 30-Oct-2020 10:58:46
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Astar_OpeningFcn, ...
'gui_OutputFcn', @Astar_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before Astar is made visible.
function Astar_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Astar (see VARARGIN)
% Choose default command line output for Astar
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Astar wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Astar_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
%%%%%%%%%%%%%%%%%%%%%%%%%%初始化参数%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Xo=[0 0];%起点位置
longth=1;%步长
J=2000;%循环迭代次数
Xj=Xo;%j=1循环初始,将车的起始坐标赋给Xj
MAX_X=100;
MAX_Y=100;
%This array stores the coordinates of the map and the
%Objects in each coordinate
MAP=2*(ones(MAX_X,MAX_Y));
% Obtain Obstacle, Target and Robot Position
% Initialize the MAP with input values
% Obstacle=-1,Target = 0,Robot=1,Space=2
j=0;
x_val = 1;
y_val = 1;
axis([-20 120 -20 120])
axis equal;
hold on;
axis off;
%set(gcf,'color','y')
%title ('A*算法路径规划');
fill([-20,120,120,-20],[-20 -20 120 120],'y')
fill([95,120,120,95],[-20 -20 10 10],'w')
text(100,5,'Notes:','FontSize',12)
plot(101,-5,'sb','markerfacecolor','b');
text(101,-5,' Robot','FontSize',12);
plot(101,-15,'om','markerfacecolor','m');
text(101,-15,' Ball','FontSize',12);
plot(1,1,'bs')
car=plot(1,1,'sb','markerfacecolor','b');
%car_name=text(0,0,' ','FontSize',12);
object=plot(0,100,'om','markerfacecolor','m');
%object_name=text(0,100,' Ball','FontSize',12);
but=1;
text(-4,-4,' Start','FontSize',12);
n=0;%Number of Obstacles
% BEGIN Interactive Obstacle, Target, Start Location selection
xTarget=0;%X Coordinate of the Target
yTarget=100;%Y Coordinate of the Target
m_Target=[xTarget,yTarget];
%object=plot(1,100,'om','markerfacecolor','m');
%object_name=text(1,100,' Ball','FontSize',12);
%选择小车起点位置
xval=1;
yval=1;
xStart=xval;
yStart=yval;
MAP(xval,yval)=1;
%car=plot(1,1,'bs','markerfacecolor','b');
%car_name=text(1,1,' ','FontSize',12);
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: place code in OpeningFcn to populate axes1
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
%main
Xo=[0 0];%起点位置
longth=1;%步长
J=2000;%循环迭代次数
Xj=Xo;%j=1循环初始,将车的起始坐标赋给Xj
%DEFINE THE 2-D MAP ARRAY
MAX_X=100;
MAX_Y=100;
%MAX_VAL=100;
%This array stores the coordinates of the map and the
%Objects in each coordinate
MAP=2*(ones(MAX_X,MAX_Y));
% Obtain Obstacle, Target and Robot Position
% Initialize the MAP with input values
% Obstacle=-1,Target = 0,Robot=1,Space=2
j=0;
x_val = 1;
y_val = 1;
axis([-20 120 -20 120])
%grid on;
%hold on;
axis equal;
hold on;
axis off;
%set(gcf,'color','y')
%title ('A*算法路径规划');
fill([-20,120,120,-20],[-20 -20 120 120],'y')
fill([95,120,120,95],[-20 -20 10 10],'w')
text(100,5,'Notes:','FontSize',12)
plot(101,-5,'sb','markerfacecolor','b');
text(101,-5,' Robot','FontSize',12);
plot(101,-15,'om','markerfacecolor','m');
text(101,-15,' Ball','FontSize',12);
plot(1,1,'bs')
car=plot(1,1,'sb','markerfacecolor','b');
%car_name=text(0,0,' ','FontSize',12);
object=plot(0,100,'om','markerfacecolor','m');
%object_name=text(0,100,' Ball','FontSize',12);
but=1;
text(-4,-4,' Start','FontSize',12);
n=0;%Number of Obstacles
% BEGIN Interactive Obstacle, Target, Start Location selection
xTarget=0;%X Coordinate of the Target
yTarget=100;%Y Coordinate of the Target
m_Target=[xTarget,yTarget];
%object=plot(1,100,'om','markerfacecolor','m');
%object_name=text(1,100,' Ball','FontSize',12);
%选择小车起点位置
xval=1;
yval=1;
xStart=xval;
yStart=yval;
MAP(xval,yval)=1;
while but == 1
[xval,yval,but] = ginput(1);
if but==1
xval=floor(xval);
yval=floor(yval);
MAP(xval,yval)=-1;%Put on the closed list as well
%plot(xval,yval,'ro','markerfacecolor','r','MarkerSize',10);
Theta=0:pi/20:pi;
xx =xval+cos(Theta)*3;
yy= yval+sin(Theta)*3;
fill(xx,yy,'w')
Theta=pi:pi/20:2*pi;
xx =xval+cos(Theta)*3;
yy= yval+sin(Theta)*3;
fill(xx,yy,'k')
end
end%End of While loop
三、运行结果
四、备注
版本:2014a