1 簡介
人工魚群算法是一種基于自然界魚群的生活行為而提出的一種智能優化算法。人工魚群算法由于其諸多優點自從提出來後就得到了廣泛的應用,這是群體智能算法的一個典型應用,是人們在群體智能研究的方面的又一個重要成就。人工魚群算法可以簡單的描述為:在自然界的一片水域中,魚類一般是通過随機自由遊動來尋找食物的,在尋找食物的過程中,魚類往往聚內建群,這是魚類長期自然選擇形成的生活習性。一般情況下,魚個數較多的地方往往是食物也較多的地方。人工魚群算法就是通過模 仿自然魚的覓食、聚群、追尾、随機遊動的行為而形成的一種優化算法。
2 部分代碼
function [C_AF,bestp]=AF_main(I,Ptot)
global K h N deltaf n m mm
%%%%%%%%初始化%%%%%%%%%
fishnum=300;%生成3條人工魚
visual=1;%感覺範圍
try_number=100;%最大嘗試次數
step=0.1;%移動步長
delta=0.618;%擁擠度因子
MAXGEN=100;%最大疊代次數
p=rand(12,fishnum);
p1=p./(ones(12,1)*sum(p))*Ptot;
for i=1:fishnum
while(find((transpose(p1(:,i))*K>I))>0)
p1(:,i)=p1(:,i)*0.5;
p1(find(p1<0))=0;
end
end %初始化人工魚及其參數 是的每個參數滿足L+1個限制
C1=sum(log2(1+transpose(h)*ones(1,fishnum).*p1));% 根據初始化參數 得到的初始化每條對應的容量
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
gen=1;
BestC=zeros(1,MAXGEN);%每步中最優的容量值
BestP=zeros(N,MAXGEN);%每步中最優的自變量
bestC=0;
while gen<=MAXGEN
for i=1:fishnum
[p2,C2]=AF_swarm(p1,i,visual,step,try_number,C1(i),delta);%%%%%%覓食行為%%%%%%%%%%%
[p3,C3]=AF_follow(p1,i,visual,step,try_number,C1(i),delta);%%%%%%聚群行為%%%%%%%%%%%
if C2>C3
p1(:,i)=p2;
C1(i)=C2;
else
p1(:,i)=p3;
C1(i)=C3;
end
end
[Cmax,index]=max(C1);
if n==1&&m==1&&mm==1
figure(1);
hold on
plot( p1(1,index),p1(2,index),p1(3,index),p1(4,index),p1(5,index),...
p1(6,index),p1(7,index),p1(8,index),p1(9,index),p1(10,index),...
p1(11,index),p1(12,index),'.','color',[gen/MAXGEN,0,0]);
end
if Cmax>bestC
bestC=Cmax;
bestp=p1(:,index);
BestC(gen)=Cmax;
[BestP(:,gen)]=p1(:,index);
else
BestC(gen)=BestC(gen-1);
[BestP(:,gen)]=BestP(:,gen-1);
end
gen=gen+1;
end
C_AF=bestC;
if n==1&&m==1&&mm==1
plot( bestp(1),bestp(2),bestp(3),bestp(4),bestp(5),...
bestp(6),bestp(7),bestp(8),bestp(9),bestp(10),...
bestp(11),bestp(12),'ro','MarkerSize',30);
xlabel('p')
ylabel('C')
title('魚群算法疊代過程中最優坐标移動')
figure(2)
plot(1:MAXGEN,BestC*deltaf)
end