MATLAB实现智能优化算法
作者:互联网
MATLAB实现智能优化算法
代码实现
分别使用了遗传算法(GA)、粒子群算法(PSO)、模拟退火算法(SA)、蚁群算法(ACO),针对函数
f(x)=x+10sin(5x)+7cos(4x);
在定义域[0,10]内寻找最大值的迭代优化,源代码如下所示:
function test(para)
%%%% para为1
%%%%%%%%%遗传算法%%%%%%%%
if para==1
%%%%%参数设置
NP=50;
L=20;
G=100;
Pc=0.8;
Pm=0.1;
xmax=10;
xmin=0;
prex=randi([0,1],NP,L); %二进制编码形式
for k=1:G
for i=1:NP
m=0;
for j=1:1:L
m=m+2^(j-1)*prex(i,j);
end
X(i)=m*(xmax-xmin)/(2^L-1)+xmin; %十进制编码形式
fit(i)=func(X(i)); %计算适应度函数
end
maxfit=max(fit);
minfit=min(fit);
rr=find(maxfit==fit);
xbest=prex(rr(1,1),:);
Xbest=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;
while newi<=NP
if (ms(newi))<fitvalue(fiti)
nf(newi,:)=prex(fiti,:);
newi=newi+1;
else
fiti=fiti+1;
end
end
%%%%%%%%%基于概率交叉操作%%%%%%
for i=1:2:NP
p=rand;
if p<Pc
q=randi([0,1],1,L);
for j=1:L
if q(j)==1
temp=nf(i+1,j);
nf(i+1,j)=nf(i,j);
nf(i,j)=temp;
end
end
end
end
%%%%%%%基于概率变异操作%%%%%%%%%
i=1;
while i<=round(NP*Pm)
h=randi([1,NP],1,1);
for j=1:round(L*Pm)
g=randi([1,L],1,1);
nf(h,g)=~nf(h,g);
end
i=i+1;
end
prex=nf;
prex(1,:)=xbest;
trace(k)=maxfit;
end
Xbest
func(Xbest)
figure(1)
subplot(2,2,1)
plot(trace)
xlabel('iter')
ylabel('bestfit')
title('GA算法')
else if para==2
%%%%%%%采用粒子群优化算法%%%%%%%%%%
N=100;
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;
for i=1:N
pbest(i,:)=func(prex(i,:));
end
[gbest,index]=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
end
end
仿真结果验证
观察仿真结果可得,x参数最优值聚集于7.85附近,最优函数值为24.85附近,验证了几种算法的有效性。
标签:end,xmax,智能,算法,bestx,MATLAB,func,xmin,prex 来源: https://blog.csdn.net/qq_42278205/article/details/115439556