基于蚁群算法的障碍物路径搜索算法的matlab仿真

 

 

 

function [ROUTES,PL,Tau]=ACASP(G,Tau,K,M,S,E,Alpha,Beta,Rho,Q)
%% ---------------------------------------------------------------
%  ACASP.m
%  ACS for Path Planning
%% ---------------------------------------------------------------
%  input parameters
%  G        gragh, which is 01 matrix, 1 represents obstcles
%  Tau      initial pheromone matrix
%  K        iterative times
%  M        ant number for once iteration
%  S        Start Point
%  E        Goal point
%  Alpha    parameter indicates the importance of pheromone
%  Beta     parameter indicates the importance of visibility
%  Rho      pheromone evaporation rate
%  Q        pheromone accumulation parameter
%
%  output parameters
%  ROUTES   the route that every ant in every iteration
%  passes
%  PL       the length that every ant in every iteration
%  passes
%  Tau      output updated pheromone 

%% -------------------- variableinitialization-----------------------------
D=G2D(G);
N=size(D,1);%N is the peoblem scale(pixel number)
MM=size(G,1);
a=1;% edge length of pixel
Ex=a*(mod(E,MM)-0.5);% goal point coordinate
if Ex==-0.5
    Ex=MM-0.5;
end
Ey=a*(MM+0.5-ceil(E/MM));% Y coordinate of goal point
Eta=zeros(1,N);
%construct the initialization matrix
for i=1:N
    ix=a*(mod(i,MM)-0.5);
    if ix==-0.5
        ix=MM-0.5;
    end
    iy=a*(MM+0.5-ceil(i/MM));    
    if i~=E
        Eta(1,i)=1/((ix-Ex)^2+(iy-Ey)^2)^0.5;
    else
        Eta(1,i)=100;
    end
end
ROUTES=cell(K,M);% use cell construct to load the passing route of every ant for every iteration    
PL=zeros(K,M);% use matrix to load the passing length of every ant for every iteration
%% -----------iteration time is k, ant number is M--------------------
for k=1:K
    disp(k);
    for m=1:M
%%     First step: status initialization
        W=S;%set current point as the starting point
        Path=S;%initialize the passing route
        PLkm=0;%initialize the passing length
        TABUkm=ones(1,N);%initialize the forbbiden table
        TABUkm(S)=0;% eliminate this point for it is the starting point
        DD=D;%initialize adjacency matrix
%%     Second Step:Choose the nodes that can step forward
        DW=DD(W,:);
        DW1=find(DW<inf);
        for j=1:length(DW1)
            if TABUkm(DW1(j))==0
                DW(j)=inf;
            end
        end
        LJD=find(DW<inf);% set of points can be selected
        Len_LJD=length(LJD);%number of points can be selected
%%     Colony stop condition:there is no food or go into a impass
        while W~=E&&Len_LJD>=1
%%         Third Step:the method to choose the next step
            PP=zeros(1,Len_LJD);
            for i=1:Len_LJD
                PP(i)=(Tau(W,LJD(i))^Alpha)*(Eta(LJD(i))^Beta);
            end
            PP=PP/(sum(PP));%construct probability distributing
            Pcum=cumsum(PP);
            Select=find(Pcum>=rand);
            to_visit=LJD(Select(1));%the next step node
%%         Fourth Step:status update and record
            Path=[Path,to_visit];%Paths accumulation
            PLkm=PLkm+DD(W,to_visit);%Path length accumulation
            W=to_visit;%move to the next point
            for kk=1:N
                if TABUkm(kk)==0
                    DD(W,kk)=inf;
                    DD(kk,W)=inf;
                end
            end
            TABUkm(W)=0;%eliminate the travelled point in the forbidden table
            DW=DD(W,:);
            DW1=find(DW<inf);
            for j=1:length(DW1)
                if TABUkm(DW1(j))==0
                    DW(j)=inf;
                end
            end
            LJD=find(DW<inf);%set of optional points
            Len_LJD=length(LJD);%number of optional points
        end
%%     Fifth Step:record the pate and length that every ant for every
%%     iteration passes
        ROUTES{k,m}=Path;
        if Path(end)==E
            PL(k,m)=PLkm;
        else
            PL(k,m)=inf;
        end
    end
%%    Sixth Step: update pheromone
    Delta_Tau=zeros(N,N);%initialize updating ammount
    for m=1:M
        if PL(k,m)<inf
            ROUT=ROUTES{k,m};
            TS=length(ROUT)-1;
            PL_km=PL(k,m);
            for s=1:TS
                x=ROUT(s);
                y=ROUT(s+1);
                Delta_Tau(x,y)=Delta_Tau(x,y)+Q/PL_km;
                Delta_Tau(y,x)=Delta_Tau(y,x)+Q/PL_km;
            end
        end
    end
    Tau=(1-Rho).*Tau+Delta_Tau;%pheromone evaporates some and accumulates some
end
%% ---------------------------PLOT--------------------------------
plotif=1;%control parameter, determine if plot or not
if plotif==1
    %plot convergence curve
    meanPL=zeros(1,K);
    minPL=zeros(1,K);
    for i=1:K
        PLK=PL(i,:);
        Nonzero=find(PLK<inf);
        PLKPLK=PLK(Nonzero);
        meanPL(i)=mean(PLKPLK);
        minPL(i)=min(PLKPLK);
    end
    figure(1)
    plot(minPL);
    hold on
    plot(meanPL);
    grid on
    title('Convergence Curve(Average Length and Minimum Length)');
    xlabel('Iteration Times');
    ylabel('Path Length'); 
    %Plot Passing Graph
    figure(2)
    axis([0,MM,0,MM])
    for i=1:MM
        for j=1:MM
            if G(i,j)==1
                x1=j-1;y1=MM-i;
                x2=j;y2=MM-i;
                x3=j;y3=MM-i+1;
                x4=j-1;y4=MM-i+1;
                fill([x1,x2,x3,x4],[y1,y2,y3,y4],[0.2,0.2,0.2]);
                hold on
            else
                x1=j-1;y1=MM-i;
                x2=j;y2=MM-i;
                x3=j;y3=MM-i+1;
                x4=j-1;y4=MM-i+1;
                fill([x1,x2,x3,x4],[y1,y2,y3,y4],[1,1,1]);
                hold on
            end
        end
    end
    hold on
    ROUT=ROUTES{K,M};
    LENROUT=length(ROUT);
    Rx=ROUT;
    Ry=ROUT;
    for ii=1:LENROUT
        Rx(ii)=a*(mod(ROUT(ii),MM)-0.5);
        if Rx(ii)==-0.5
            Rx(ii)=MM-0.5;
        end
        Ry(ii)=a*(MM+0.5-ceil(ROUT(ii)/MM));
    end
    plot(Rx,Ry)
end
title('Shortest Path');
axis([0,MM,0,MM]);
plotif2=1;%Plot route for every iteration
if plotif2==1
    figure(3)
    axis([0,MM,0,MM])
    for i=1:MM
        for j=1:MM
            if G(i,j)==1
                x1=j-1;y1=MM-i;
                x2=j;y2=MM-i;
                x3=j;y3=MM-i+1;
                x4=j-1;y4=MM-i+1;
                fill([x1,x2,x3,x4],[y1,y2,y3,y4],[0.2,0.2,0.2]);
                hold on
            else
                x1=j-1;y1=MM-i;
                x2=j;y2=MM-i;
                x3=j;y3=MM-i+1;
                x4=j-1;y4=MM-i+1;
                fill([x1,x2,x3,x4],[y1,y2,y3,y4],[1,1,1]);
                hold on
            end
        end
    end
    for k=1:K
        PLK=PL(k,:);
        minPLK=min(PLK);
        pos=find(PLK==minPLK);
        m=pos(1);
        ROUT=ROUTES{k,m};
        LENROUT=length(ROUT);
        Rx=ROUT;
        Ry=ROUT;
        for ii=1:LENROUT
            Rx(ii)=a*(mod(ROUT(ii),MM)-0.5);
            if Rx(ii)==-0.5
                Rx(ii)=MM-0.5;
            end
            Ry(ii)=a*(MM+0.5-ceil(ROUT(ii)/MM));
        end
        plot(Rx,Ry)
        hold on
    end
end
axis([0,MM,0,MM])
title('Routes that All the Ants Passed');
disp('Shortest Length is')
disp(minPLK);

C71

おすすめ

転載: blog.csdn.net/ccsss22/article/details/121804456