智能优化算法程序代码集锦

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

1人工蚂蚁算法%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%function[x,y,minvalue]=AA(func)%Example[x,y,minvalue]=AA('Foxhole')clc;tic;subplot(2,2,1);%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%plot1draw(func);title([func,'Function']);%初始化各参数Ant=100;%蚂蚁规模ECHO=200;%迭代次数step=0.01*rand(1);%局部搜索时的步长temp=[0,0];%各子区间长度start1=-100;end1=100;start2=-100;end2=100;Len1=(end1-start1)/Ant;Len2=(end2-start2)/Ant;%P=0.2;%初始化蚂蚁位置fori=1:AntX(i,1)=(start1+(end1-start1)*rand(1));X(i,2)=(start2+(end2-start2)*rand(1));%func=AA_Foxhole_Func(X(i,1),X(i,2));val=feval(func,[X(i,1),X(i,2)]);T0(i)=exp(-val);%初始信息素,随函数值大,信息素浓度小,反之亦然%%%%%*********************************************************************end;%至此初始化完成forEcho=1:ECHO%开始寻优%P0函数定义,P0为全局转移选择因子a1=0.9;b1=(1/ECHO)*2*log(1/2);f1=a1*exp(b1*Echo);a2=0.225;b2=(1/ECHO)*2*log(2);f2=a2*exp(b2*Echo);ifEcho=(ECHO/2)P0=f1;elseP0=f2;end;%P函数定义,P为信息素蒸发系数a3=0.1;b3=(1/ECHO).*log(9);P=a3*exp(b3*Echo);lamda=0.10+(0.14-0.1)*rand(1);%全局转移步长参数Wmax=1.0+(1.4-1.0)*rand(1);%步长更新参数上限Wmin=0.2+(0.8-0.2)*rand(1);%步长更新参数下限%寻找初始最优值T_Best=T0(1);forj=1:AntifT0(j)=T_BestT_Best=T0(j);BestIndex=j;end;end;W=Wmax-(Wmax-Wmin)*(Echo/ECHO);%局部搜索步长更新参数forj_g=1:Ant%全局转移概率求取,当该蚂蚁随在位置不是bestindex时ifj_g~=BestIndexr=T0(BestIndex)-T0(j_g);Prob(j_g)=exp(r)/exp(T0(BestIndex));else%当j_g=BestIndex的时候进行局部搜索ifrand(1)0.5temp(1,1)=X(BestIndex,1)+W*step;temp(1,2)=X(BestIndex,2)+W*step;elsetemp(1,1)=X(BestIndex,1)-W*step;temp(1,2)=X(BestIndex,2)-W*step;end;Prob(j_g)=0;%bestindex的蚂蚁不进行全局转移end;X1_T=temp(1,1);X2_T=temp(1,2);X1_B=X(BestIndex,1);X2_B=X(BestIndex,2);%func1=AA_Foxhole_Func(X1_T,X2_T);%%%%%%%%%%%***************************************************%F1_T=func1;F1_T=feval(func,[X(i,1),X(i,2)]);F1_B=feval(func,[X1_B,X2_B]);%F1_T=(X1_T-1).^2+(X2_T-2.2).^2+1;%func2=AA_Foxhole_Func(X1_B,X2_B);%%%%%%%%%%%%%***************************************************%F1_B=func2;%F1_B=(X1_B-1).^2+(X2_B-2.2).^2+1;ifexp(-F1_T)exp(-F1_B)X(BestIndex,1)=temp(1,1);X(BestIndex,2)=temp(1,2);end;end;forj_g_tr=1:AntifProb(j_g_tr)P0X(j_g_tr,1)=X(j_g_tr,1)+lamda*(X(BestIndex,1)-X(j_g_tr,1));%Xi=Xi+lamda*(Xbest-Xi)2X(j_g_tr,2)=X(j_g_tr,2)+lamda*(X(BestIndex,2)-X(j_g_tr,2));%Xi=Xi+lamda*(Xbest-Xi)X(j_g_tr,1)=bound(X(j_g_tr,1),start1,end1);X(j_g_tr,2)=bound(X(j_g_tr,2),start2,end2);elseX(j_g_tr,1)=X(j_g_tr,1)+((-1)+2*rand(1))*Len1;%Xi=Xi+rand(-1,1)*Len1X(j_g_tr,2)=X(j_g_tr,2)+((-1)+2*rand(1))*Len2;%Xi=Xi+rand(-1,1)*Len2X(j_g_tr,1)=bound(X(j_g_tr,1),start1,end1);X(j_g_tr,2)=bound(X(j_g_tr,2),start2,end2);end;end;%信息素更新subplot(2,2,2);%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Plot1bar([X(BestIndex,1)X(BestIndex,2)],0.25);%colormap(cool);axis([03-4040]);title({date;['Iteration',num2str(Echo)]});xlabel(['Min_x=',num2str(X(BestIndex,1)),'','Min_y=',num2str(X(BestIndex,2))]);fort_t=1:Ant%func=AA_Foxhole_Func(X(t_t,1),X(t_t,2));val1=feval(func,[X(t_t,1),X(t_t,2)]);T0(t_t)=(1-P)*T0(t_t)+(exp(-val1));%*************************************************************************end;[c_iter,i_iter]=max(T0);%求取每代全局最优解minpoint_iter=[X(i_iter,1),X(i_iter,2)];%func3=AA_Foxhole_Func(X(i_iter,1),X(i_iter,2));%%%%%%%%%***************************************************************************val2=feval(func,[X(i_iter,1),X(i_iter,2)]);minvalue_iter=val2;%minvalue_iter=(X(i_iter,1)-1).^2+(X(i_iter,2)-2.2).^2+1;min_local(Echo)=minvalue_iter;%保存每代局部最优解subplot(2,2,3);%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Plot2plot(X(BestIndex,1),X(BestIndex,2),'rs','MarkerFaceColor','r','MarkerSize',8),gridon;title(['GlobalMinValue=',num2str(minvalue_iter)]);holdon;plot(X(:,1),X(:,2),'g.'),pause(0.02);holdoff;axis([-100100-100100]);gridon;%将每代全局最优解存到min_global矩阵中ifEcho=2ifmin_local(Echo)min_global(Echo-1)min_global(Echo)=min_local(Echo);elsemin_global(Echo)=min_global(Echo-1);end;elsemin_global(Echo)=minvalue_iter;end;subplot(2,2,4);%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Plot3min_global=min_global';index(:,1)=1:ECHO;plot(Echo,min_global(Echo),'y*')%axis([0ECHO010]);holdon;title([func,'(X)=',num2str(minvalue_iter)],'Color','r');xlabel('iteration');ylabel('f(x)');gridon;end;%ECHO循环结束[c_max,i_max]=max(T0);minpoint=[X(i_max,1),X(i_max,2)];%func3=AA_Foxhole_Func(X(i_max,1),X(i_max,2));%%%*************************************************************************%minvalue=func3;minvalue=feval(func,[X(i_max,1),X(i_max,2)]);x=X(BestIndex,1);y=X(BestIndex,2);runtime=toc人工免疫算法3function[x,y,fx,vfx,vmfit,P,vpm]=AI(func,gen,n,pm,per);%Example[x,y,fx]=AI('Foxhole')subplot(2,2,1);draw(func);title([func,'Function']);ifnargin==1,%gen=200;n=round(size(P,1)/2);pm=0.0005;per=0.0;fat=10;%gen=250;n=size(P,1);pm=0.01;per=0.0;fat=.1;P=cadeia(200,44,0,0,0);gen=40;n=size(P,1);pm=0.2;per=0.0;fat=0.1;end;whilen=0,n=input('nhastobeatleastone.Typeanewvalueforn:');end;xmin=-100;xmax=100;ymin=-100;ymax=100;x=decode(P(:,1:22),xmin,xmax);y=decode(P(:,23:end),ymin,ymax)

1 / 31
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功