智能优化算法程序代码集锦Word文档下载推荐.docx
《智能优化算法程序代码集锦Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《智能优化算法程序代码集锦Word文档下载推荐.docx(95页珍藏版)》请在冰豆网上搜索。
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;
else
P0=f2;
%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:
ifT0(j)>
=T_Best
T_Best=T0(j);
BestIndex=j;
end;
W=Wmax-(Wmax-Wmin)*(Echo/ECHO);
%局部搜索步长更新参数
forj_g=1:
Ant%全局转移概率求取,当该蚂蚁随在位置不是bestindex时
ifj_g~=BestIndex
r=T0(BestIndex)-T0(j_g);
Prob(j_g)=exp(r)/exp(T0(BestIndex));
else%当j_g=BestIndex的时候进行局部搜索
ifrand
(1)<
0.5
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;
Prob(j_g)=0;
%bestindex的蚂蚁不进行全局转移
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);
forj_g_tr=1:
ifProb(j_g_tr)<
P0
X(j_g_tr,1)=X(j_g_tr,1)+lamda*(X(BestIndex,1)-X(j_g_tr,1));
%Xi=Xi+lamda*(Xbest-Xi)
X(j_g_tr,2)=X(j_g_tr,2)+lamda*(X(BestIndex,2)-X(j_g_tr,2));
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);
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
%信息素更新
subplot(2,2,2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Plot1
bar([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:
%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));
%*************************************************************************
[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);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Plot2
plot(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>
=2
ifmin_local(Echo)<
min_global(Echo-1)
min_global(Echo)=min_local(Echo);
min_global(Echo)=min_global(Echo-1);
min_global(Echo)=minvalue_iter;
subplot(2,2,4);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Plot3
min_global=min_global'
;
index(:
1)=1:
ECHO;
plot(Echo,min_global(Echo),'
y*'
%axis([0ECHO010]);
title([func,'
(X)='
num2str(minvalue_iter)],'
Color'
);
xlabel('
iteration'
ylabel('
f(x)'
%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
人工免疫算法
function[x,y,fx,vfx,vmfit,P,vpm]=AI(func,gen,n,pm,per);
%Example[x,y,fx]=AI('
title([func,'
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;
fat=.1;
P=cadeia(200,44,0,0,0);
gen=40;
pm=0.2;
fat=0.1;
whilen<
=0,
n=input('
nhastobeatleastone.Typeanewvalueforn:
xmin=-100;
xmax=100;
ymin=-100;
ymax=100;
x=decode(P(:
1:
22),xmin,xmax);
y=decode(P(:
23:
end),ymin,ymax);
%fit=eval(f);
%fit=AI_Foxhole_Func(x,y);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fit=feval(func,[x'
y'
%imprime(1,vxp,vyp,vzp,x,y,fit,1,1);
%Hypermutationcontrollingparameters
pma=pm;
itpm=gen;
pmr=0.8;
%Generaldefintions
vpm=[];
vfx=[];
vmfit=[];
valfx=1;
[N,L]=size(P);
it=0;
PRINT=1;
%Generations
whileit<
=gen&
valfx<
=100,
x=decode(P(:
T=[];
cs=[];
%fit=eval(f);
%fit=AI_Foxhole_Func(x,y);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fit=feval(func,[x'
[a,ind]=sort(fit);
valx=x(ind(end-n+1:
end));
valy=y(ind(end-n+1:
fx=a(end-n+1:
end);
%nbestindividuals(maximization)
%Reproduction
[T,pcs]=reprod(n,fat,N,ind,P,T);
%Hypermutation
M=rand(size(T,1),L)<
=pm;
T=T-2.*(T.*M)+M;
T(pcs,:
)=P(fliplr(ind(end-n+1:
end)),:
%NewRe-Selection(Multi-peaksolution)
x=decode(T(:
y=decode(T(:
pcs=[0pcs];
fori=1:
n,
[out(i),bcs(i)]=min(fit(pcs(i)+1:
pcs(i+1)));
%Mimimazionproblem%%%*************************
bcs(i)=bcs(i)+pcs(i);
P(fliplr(ind(end-n+1:
)=T(bcs,:
%Editing(Repertoireshift)
nedit=round(per*N);
it=it+1;
P(ind(1:
nedit),:
)=cadeia(nedit,L,0,0,0);
pm=pmcont(pm,pma,pmr,it,itpm);
valfx=min(fx);
%*************************************************************
vpm=[vpmpm];
vfx=[vfxvalfx];
vmfit=[vmfitmean(fit)];
disp(sprintf('
It.:
%dpm:
%.4fx:
%2.2fy:
%2.2fAv.:
%2.2ff(x,y):
%2.3f'
it,pm,valx
(1),valy
(1),vmfit
(1),valfx));
subplot(2,2,2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Plot1
bar([valx
(1)valy
(1)],0.25);
num2str(it)]);
pause(0.1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Plot2
plot(valx
(1),valy
(1),'
8)
%plot(x(:
1),x(:
k.'
set(gca,'
g'
holdoff;
axis([-100100-100100]);
title(['
GlobalMin='
num2str(valfx)]);
xlabel(['
Min_x='
num2str(valx
(1)),'
Min_y='
num2str(valy
(1))]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Plot3
plot(it,valfx,'
axis([0gen010]);
title([func,'
(X)='
num2str(valfx)]);
%endwhile
%imprime(PRINT,vxp,vyp,vzp,x,y,fit,it,1);
x=valx
(1);
y=valy
(1);
fx=min(fx);
%***********************************************************************
%x=P(ind(end),1:
22);
y=P(ind(end),23:
44);
fx=max(fx);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Plot4
%---------------------%
%INTERNALSUBFUNCTIONS
%Print
function[]=imprime(PRINT,vx,vy,vz,x,y,fx,it,mit);
%x,fx->
actualvalues
%vxplot,vplot->
original(base)function
ifPRINT==1,
ifrem(it,mit)==0,
mesh(vx,vy,vz);
axis([-100100-1001000500]);
x'
y'
zlabel('
f(x,y)'
plot3(x,y,fx,'
k*'
drawnow;
%Reproduction
function[T,pcs]=reprod(n,fat,N,ind,P,T);
%n->
numberofclones
%fat->
multiplyingfactor
%ind->
bestindividuals
%T->
temporarypopulation
%pcs->
finalpositionofeachclone
ifn==1,
cs=N;
T=ones(N,1)*P(ind
(1),:
else,
%cs(i)=round(fat*N/i);
cs(i)=round(fat*N);
pcs(i)=sum(cs);
T=[T;
ones(cs(i),1)*P(ind(end-i+1),:
)];
%Controlofpm
function[pm]=pmcont(pm,pma,pmr,it,itpm);
%pma->
initialvalue
%pmr->
controlrate
%itpm->
iterationsforrestoring
ifrem(it,itpm)==0,
pm=pm*pmr;
ifrem(it,10*itpm)==0,
pm=pma;