智能优化算法源代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
人工蚂蚁算法%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function [x,y, minvalue] = AA(func)
% Example [x, y,minvalue] =
AA('Foxhole')
clc;
tic;
subplot(2,2,1); %%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%% plot 1
draw(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;
%初始化蚂蚁位置
for i=1:Ant
X(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; %至此初始化完成
for Echo=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);
if Echo<=(ECHO/2)
P0=f1;
else
P0=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);
for j=1:Ant
if T0(j)>=T_Best
T_Best=T0(j);
BestIndex=j;
end;
end;
W=Wmax-(Wmax-
Wmin)*(Echo/ECHO); %局部搜索步长更新参数
for j_g=1:Ant %全局转移概率求取,当该蚂蚁随在位置不是bestindex时
if j_g~=BestIndex
r=T0(BestIndex)-T0(j_g);
Prob(j_g)=exp(r)/exp(T0(BestIndex));
else%当j_g=BestIndex的时候进行局部搜索
if rand(1)<0.5
1
temp(1,1)=X(BestIndex,1)+W*step;
temp(1,2)=X(BestIndex,2)+W*step;
else
temp(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;
if exp(-F1_T)>exp(-F1_B) X(BestIndex,1)=temp(1,1);
X(BestIndex,2)=temp(1,2);
end;
end;
for j_g_tr=1:Ant
if Prob(j_g_tr) X(j_g_tr,1)=X(j_g_tr,1)+lamda*(X(Best Index,1)- X(j_g_tr,1));%Xi=Xi+lamda*(Xbest-Xi) X(j_g_tr,2)=X(j_g_tr,2)+lamda*(X(Best Index,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); else X(j_g_tr,1)=X(j_g_tr,1)+((- 1)+2*rand(1))*Len1;%Xi=Xi+rand(- 1,1)*Len1 X(j_g_tr,2)=X(j_g_tr,2)+((- 1)+2*rand(1))*Len2;%Xi=Xi+rand(- 1,1)*Len2 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); end; end; %信息素更新 subplot(2,2,2); %%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%% Plot 1 bar([X(BestIndex,1) X(BestIndex,2)],0.25); %colormap (cool); axis([0 3 -40 40 ]) ; title ({date;['Iteration ', num2str(Echo)]}); xlabel(['Min_x = ',num2str(X(BestIndex,1)),' ', 'Min_y = ', num2str(X(BestIndex,2))]); 2