首页 > 编程知识 正文

matlab智能算法工具箱,模糊神经网络算法matlab

时间:2023-05-05 15:44:29 阅读:23290 作者:2682

MATLAB实现智能优化算法代码的实现分别采用遗传算法(GA )、粒子群算法(PSO )、模拟退火算法(SA )、蚁群算法(ACO ),对函数进行

f(x )=x10sin ) 5x )7cos ) 4x; 在定义域[ 0,10 ]中查找最大值的迭代优化时,源代码如下:

功能测试(para ),para为1 ),遗传算法(,ifpara==1),参数设置NP=50; L=20; G=100; Pc=0.8; Pm=0.1; xmax=10; xmin=0; prex=虚拟发带([ 0,1 ],NP,l ); %二进制代码格式for k=1:G for i=1:NP m=0; forj=1:13360Lm=m2^(j-1 ) *prex(I,j ); endx(I )=m*(xmax-xmin )/(2^L-1 ) xmin; 进制代码格式fit(I )=func ) I ); %计算适应度函数endmaxfit=max(fit ); 最小拟合=min (fit; RR=find (最大fit==fit ); XB est=prex (RR (1,1 ), ); XB est=x (RR (1,1 ); %fit=(fit-minfit )/)/(maxfit-minfit ); 基于轮盘复制操作的sumfit=sum(fit ); fitvalue=fit./sumfit; %归一化处理fitvalue=cumsum(fitvalue ); ms=sort(rand(NP,1 ); fiti=1; newi=1; wilenewi=NPif(ms ) newi ) ) fitvalue ) Fiti ) nf ) newi, )=prex ) Fiti, ); newi=newi 1; else fiti=fiti 1; endend%%%%%%概率交叉操作%%%%forI=1:23360NP=rand; if pPc q=虚拟发带([ 0,1 ],1,l ); forj=1:lifq(j )==1temp=nf ) I1,j ); nf(I1,j )=nf ) I,j ); nf(I,j )=temp; endendend%%%%基于概率变异操作的%%%%I=1; whileI=round(NP*pm ) h=虚拟发带([1,NP],1,1 ); forj=1:round(L*pm ) g=虚拟发带([1,L],1,1 ); nf(h,g )=~nf(h ) h,g ); end i=i 1; end prex=nf; prex (1, )=xbest; 跟踪(k )=最大拟合; endXBestfunc(XBest ) figure(1) subplot (2,2,1 ) plot (trace ) xlabel (ITER ) ylabel () bestfit (title ) ) ) ) ) w=0.8; c1=1.5; c2=1.5; vmax=10; vmin=-10; xmax=10; xmin=0; D=1; iter_max=100; prex=rand(n,d ) * ) *(xmax-xmin ) xmin; v=rand(n,d ) * ) *(vmax-vmin ) vmin; pxbest=prex; forI=1:npbest(I, )=func (prex ) I, ); end [gbest,ind

ex]=max(pbest); gxbest=prex(index,:); iter=1; while iter<iter_max for i=1:N v(i,:)=w*v(i,:)+c1*rand*(pbest(i,:)-prex(i,:))+c2*rand*(gbest-prex(i,:)); if v(i,:)>vmax v(i,:)=vmax; end if v(i,:)<vmin v(i,:)=vmin; end prex(i,:)=prex(i,:)+v(i,:); if prex(i,:)>xmax prex(i,:)=xmax; end if prex(i,:)<xmin prex(i,:)=xmin; end if (func(prex(i,:))>func(pxbest(i,:))) pxbest(i,:)=prex(i,:); pbest(i,:)=func(prex(i,:)); end if (func(prex(i,:))>gbest) gxbest=prex(i,:); gbest=func(prex(i,:)); end end trace(iter)=gbest; iter=iter+1; end gxbest func(gxbest) figure(1) subplot(2,2,2) plot(trace) xlabel('iter') ylabel('bestfit') title('PSO算法')else if para==3 %%%%%%%采用模拟退火算法%%%%%%%%%%% T=100; K=0.998; L=200; YZ=1e-10; xmax=10; xmin=0; S=0.01; D=1; P=0; prex=rand(D,1)*(xmax-xmin)+xmin; prebestx=prex; prex=rand(D,1)*(xmax-xmin)+xmin; bestx=prex; deta=abs(func(bestx)-func(prebestx)); while (deta>YZ&&T>0.01) T=K*T; for i=1:L nextx=prex+S*((xmax-xmin)+xmin); for j=1:D if nextx(j,1)>xmax nextx(j,i)=xmax; end if nextx(j,1)<xmin nextx(j,1)=xmin; end end fit=func(nextx); %%%%更新最优参数和适应度 if fit>func(bestx) prebestx=bestx; bestx=nextx; end %%%%%%以算法确定是否代替旧解%%%%%%% if (func(bestx)-func(prebestx)<0) prex=nextx; P=P+1; else changer=-(func(bestx)-func(prebestx))/T; p1=exp(changer); if p1>rand prex=nextx; P=P+1; end end trace(P+1)=func(bestx); end deta=abs(func(bestx)-func(prebestx)); end bestx func(bestx) figure(1) subplot(2,2,3) plot(trace) xlabel('iter') ylabel('bestfit') title('SA算法') else if para==4 m=50; iter_max=2000; rho=0.9; P0=0.2; S=0.1; xmax=10; xmin=0; iter=1; x=rand(m,1)*(xmax-xmin)+xmin; for i=1:m tau(i)=func(x(i)); end S=0.1; for NC=1:iter_max lamda=1/iter; [taumax,index]=max(tau); %%%%%%计算状态转移矩阵%%%%%%%%% for i=1:m P(NC,i)=(tau(index)-tau(i))/tau(index); end %%%%%%位置更新 for i=1:m if P(NC,i)<P0 temp1=x(i,1)+(2*rand-1)*S*lamda; else temp1=x(i,1)+(xmax-xmin)*(rand-0.5); end if temp1<xmin temp1=xmin; end if temp1>xmax temp1=xmax; end if func(temp1)>func(x(i,:)) x(i)=temp1; end end for i=1:1:m tau(i)=(1-rho)*tau(i)+func(x(i)); end [value,index]=min(tau); trace(NC)=func(x(index)); end [maxval,index]=min(tau); maxx=x(index) maxval=func(maxx) figure(1) subplot(2,2,4) plot(trace) end end endend 仿真结果验证

观察仿真结果可得,x参数最优值聚集于7.85附近,最优函数值为24.85附近,验证了几种算法的有效性。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。