www.pudn.com > AFSA.rar > AFSA.m, change:2011-04-18,size:4434b


clear; 
a=rand(50,5) 
tic %计算时间 
a= 
step=0.5; 
m=size(a,1) 
try_number=5 
xx=[]; 
ban=[]; 
bann=[]; 
xxx=zeros(1,5); 
yy=zeros(m,1);%用来保存变化后的鱼的食物浓度 
yyf=zeros(m,1); 
yys=zeros(m,1); 
yyp=zeros(m,1); 
afps=zeros(m,5); 
 
for i=1:m 
    yyst(i)=food(a(i,:)); 
end 
bann=min(yyst); %公告板初始化 
gen=0 
while gen<=20 
    %开始循环 
    forj=1:m 
    %行为的选择 先执行追尾,然后是觅食,然后是聚群,最后是随机 
    yy(j,:)=food(a(i,:));b=a(i,:); 
    [yyf(j,:),afps]=follow(b,a,step,try_number,m); 
    if yyf(j,:)<yy(j,:) %判断追尾行为是否有进步,没进步就执行觅食行为 
       yy(j,:)=yyf(j,:); 
       a(j,:)=afps; 
    else [yyp(j,:),afps]=prey(b,try_number,step); 
        if yyp(j,:)<yy(j,:) %判断觅食行为是否有进步,没进步就执行觅食行为 
           yy(j,:)=yyp(j,:); 
           a(j,:)=afps; 
        else [yys(j,:),afps]=swarm(b,a,step,try_number,m); 
           if yys(j,:)<yy(j,:) %判断聚群行为是否有进步,没进步就执行觅食行为 
           yy(j,:)=yys(j,:); 
           a(j,:)=afps; 
           else a(j,:)=floor(100001*rand(1,5))/100000; %   否则执行随机行为 
            yy(j,:)=food(a(j,:)); 
           end 
        end 
    end 
end 
%公告板修改 
gen=gen+1 
%找出yy中的最小值及其位置 
[ban(gen,:),s]=min(yy); 
xx(gen,:)=a(s,:); 
%bann=[bann;gen ban]; %%把结果累加到一起 
end  % while gen<=200 
bann=[bann;ban]; 
plot(bann) 
[built,ss]=min(bann); %求所有代中的最小值 
xxx=xx(ss-1,:); 
toc 
t=toc 
 
 
function [y,x] = prey(x,try_number,step) 
y=food(x); 
for j=1:try_number 
    xx=floor(100001*rand(1,5))/100000; 
    if sum(abs(xx-x)<0.2)>=2 && food(xx)<food(x)  %判断找到的点是否在xi的随机邻域中 
        x=x+rand*step*(xx-x)/sqrt(sum((xx-x).^2)); 
        %对x的值进行修正,使其在0-1范围内 
        for j=1:5 
            if x(j)<0 
                x(j)=0; 
            end 
            if x(j)>1 
                x(j)=1; 
            end 
        end 
        y=food(x),break; 
    end 
end 
 
 
function [y,x] = swarm(x,a,step,try_number,m) 
%m=200; %鱼群中鱼的个数 
xx=[]; 
yy=[]; 
y=food(x); 
z=[]; 
nf=[]; 
delt=0.9*10; 
step=0.5; 
%寻找x邻域的中心位置 
b=[] 
for i=1:m 
    if sum(abs(x-a(i,:))<0.3)>=2 
        b=[b;a(i,:)]; 
    end 
end 
%b 
xx=sum(b)/size(b,1); %邻域的中心 
yy=food(xx); 
if yy<food(x) 
    %计算中心位置xx邻域中鱼的个数 
    nf=0; 
    for i=1:m 
        if sum(abs(x-a(i,:))<0.3)>=2 
            nf=nf+1 
        end 
    end 
 %结束 
 %nf 
 %判断x周围是否拥挤,拥挤就觅食,不拥挤就向这点移动 
 if yy/nf<delt*food(x) 
     x=x+rand*step*(xx-x)/sqrt(sum((xx-x).^2)); 
     for j=1:5 
            if x(j)<0 
                x(j)=0; 
            end 
            if x(j)>1 
                x(j)=1; 
            end 
        end 
        y=food(x); 
 else prey(x,try_number,step); 
 end 
else prey(x,try_number,step); 
end 
 
 
function [y,x] = follow(x,a,step,try_number,m) 
%m=size(a,1);  %鱼群中鱼的个数 由主程序中来 
xx=[]; 
yy=20; 
z=[]; 
y=food(x); 
delt=0.9*10; 
step=0.5; 
%寻找x邻域中具有最小适应度的那条鱼 
for i=1:m 
     if sum(abs(x-a(i,:))<0.2)>=2 && food(a(i,:))<yy 
         yy=food(a(i,:)); 
         xx=a(i,:); 
         z=i; 
     end 
end 
if yy<food(x) 
    %计算a(z.:)的邻域中鱼的个数 
    nf=0;%%没有设定nf=-1是因为此时a(z.:)也可以看到x 
    for i=1:m 
     if sum(abs(x-a(i,:))<0.2)>=2 
         nf=mf+1 
     end 
    end 
    %判断x周围是否拥挤,拥挤就觅食,不拥挤就向这点移动 
if yy/nf<delt*food(x) 
    x=x+rand*step*(xx-x)/sqrt(sum((xx-x).^2)); 
    %对x的值进行修正,使其在0-1范围内 
         for j=1:5 
     if x(j)<0 
         x(j)=0; 
     end 
     if x(j)>1 
         x(j)=1; 
     end 
         end % for 
    y=food(x); 
else prey(x,try_number,step); 
end 
%结束 
else prey(x,try_number,step); 
end 
 
 
function [L] = food(Fish)  %这里只是对单条鱼进行求值 
start=[0,0]; 
goal=[5.4,5.5]; 
duandian[0.5,2,0.8,1; 
         1.1,3.5,2,3.2; 
         2.3,5,2.6,4; 
         3.4,4.2,4.1,5.1; 
         4.6,4.9,5,4.5]; 
[m,n]=size(Fish); 
point=zeros(5,2) 
L=0; 
k=0; 
for i=1:n %维数=5 
   k=sign((duandian(i,4)-duandian(i,2))/(duandian(i,3)-duandian(i,1)));%计算斜率 
   point(i,1)=duandian(i,1)+abs(duandian(i,1)-duandian(i,3))*Fish(:,i); 
   %计算每条鱼的各个路往点的坐标-横坐标 不需要乘斜率,因为是按顺序写的 
   point(i,2)=duandian(i,2)+abs(duandian(i,4)-duandian(i,2))*k*Fish(:,i); 
   %纵坐标 
end 
L=distance(start,point(1,:))+distance(point(5,:),goal); 
for i=1:4 
    L=L+distance(point(i,:)),point(i+1,:)); 
end 
plot(food);