📄 smpso_f2_0.m
字号:
% A SIMPLE IMPLEMENTATION OF THE PARTICLE SWARM OPTIMIZATION
clear;
clc;
for iii=1:1:100
iii=iii
% *************************************************************************
% 单纯形微粒群优化算法求解----smPSO
% *************************************************************************
% 初始化
PopSize=20; MaxIt=2000;
maxw=1.8; minw=0.02; c1=2.0; c2=2.0; dim=2;
popul=rand(2,PopSize)*200.0-100.0; % 位置初始化
vel=rand(dim,PopSize); % 速度初始化
for i=1:PopSize
fit(i)=(sin(sqrt((popul(1,i)-50.0)^2+(popul(2,i)-50.0)^2)+exp(1)))/(sqrt((popul(1,i)-50.0)^2+(popul(2,i)-50.0)^2)+exp(1))+1.0;
end
ibestpos=popul; % 个体最好位置初始化---inCHEpso
ibestfit=fit; % 各个体的适应值
[bestpart,g]=max(fit); % 找全局最好的适应值
gbestfit=bestpart; % 全局最好的适应值
gbestpos=popul(:,g);
gbestchange=gbestfit;
gbestchangecounter=0;
% 主程序开始运行
iter=0;
while (iter<MaxIt), % 迭代开始
iter=iter+1;
w=minw+(maxw-minw)*(1+cos((iter-1)*pi/(MaxIt-1)))/2.0;
for i=1:PopSize
A(:,i)=gbestpos;
end
R1=rand(dim,PopSize); R2=rand(dim,PopSize);
vel=0.8*(w*vel+c1*R1.*(ibestpos-popul)+c2*R2.*(A-popul)); % 速度计算
popul=popul+vel; % 位置计算
clear A; clear R1; clear R2;
for i=1:PopSize
for j=1:dim
if popul(j,i)>100.0,
popul(j,i)=100.0;
end
if popul(j,i)<-100.0,
popul(j,i)=-100.0;
end
end
end
for i=1:PopSize
fit(i)=(sin(sqrt((popul(1,i)-50.0)^2+(popul(2,i)-50.0)^2)+exp(1)))/(sqrt((popul(1,i)-50.0)^2+(popul(2,i)-50.0)^2)+exp(1))+1.0;
end
for i=1:PopSize
if fit(i)>ibestfit(i)
ibestfit(i)=fit(i);
ibestpos(:,i)=popul(:,i);
end
end
[bestpart,g]=max(fit);
if bestpart>gbestfit;
gbestfit=bestpart;
gbestpos=popul(:,g);
end
if gbestfit>gbestchange
gbestchange=gbestfit;
gbestchangecounter=0;
else
gbestchangecounter=gbestchangecounter+1;
end
if gbestchangecounter>=1
[OderF,IndexF]=sort(fit); % 从小到大排列适应值
x1=gbestpos; % 选单纯形的顶点
x2=popul(:,IndexF(ceil(PopSize*3/4)));
x3=popul(:,IndexF(ceil(PopSize/2)));
clear OderF; clear IndexF;
for tempii=1:15 % 进行5次单纯形法搜索
fx1=(sin(sqrt((x1(1)-50.0)^2+(x1(2)-50.0)^2)+exp(1)))/(sqrt((x1(1)-50.0)^2+(x1(2)-50.0)^2)+exp(1))+1.0;
fx2=(sin(sqrt((x2(1)-50.0)^2+(x2(2)-50.0)^2)+exp(1)))/(sqrt((x2(1)-50.0)^2+(x2(2)-50.0)^2)+exp(1))+1.0;
fx3=(sin(sqrt((x3(1)-50.0)^2+(x3(2)-50.0)^2)+exp(1)))/(sqrt((x3(1)-50.0)^2+(x3(2)-50.0)^2)+exp(1))+1.0;
fx(1)=fx1; fx(2)=fx2; fx(3)=fx3;
x(:,1)=x1; x(:,2)=x2; x(:,3)=x3;
[OrderF,IndexF]=sort(fx);
x1=x(:,IndexF(3)); x2=x(:,IndexF(2)); x3=x(:,IndexF(1));
fx1=OrderF(3); fx2=OrderF(2); fx3=OrderF(1);
clear OrderF; clear IndexF; clear fx; clear x;
error=sqrt((x1(1)-x2(1))^2+(x1(2)-x2(2))^2);
if error<0.000001
a1=gbestpos(1,1); a2=gbestpos(2,1);
x2(1)=(rand(1,1)-0.5)*2*200.0/10+a1; % 位置初始化
x2(2)=(rand(1,1)-0.5)*2*200.0/10+a2; % 位置初始化
end
error=sqrt((x1(1)-x3(1))^2+(x1(2)-x3(2))^2);
if error<0.000001
a1=gbestpos(1,1); a2=gbestpos(2,1);
x3(1)=(rand(1,1)-0.5)*2*200.0/10+a1; % 位置初始化
x3(2)=(rand(1,1)-0.5)*2*200.0/10+a2; % 位置初始化
end
error=sqrt((x2(1)-x3(1))^2+(x2(2)-x3(2))^2);
if error<0.000001
a1=gbestpos(1,1); a2=gbestpos(2,1);
x3(1)=(rand(1,1)-0.5)*2*200.0/10+a1; % 位置初始化
x3(2)=(rand(1,1)-0.5)*2*200.0/10+a2; % 位置初始化
end
clear error; clear a1; clear a2;
for i=1:2
if x2(i)>100.0,
x2(i)=100.0;
end
if x2(i)<-100.0,
x2(i)=-100.0;
end
end
for i=1:2
if x3(i)>100.0,
x3(i)=100.0;
end
if x3(i)<-100.0,
x3(i)=-100.0;
end
end
fx1=(sin(sqrt((x1(1)-50.0)^2+(x1(2)-50.0)^2)+exp(1)))/(sqrt((x1(1)-50.0)^2+(x1(2)-50.0)^2)+exp(1))+1.0;
fx2=(sin(sqrt((x2(1)-50.0)^2+(x2(2)-50.0)^2)+exp(1)))/(sqrt((x2(1)-50.0)^2+(x2(2)-50.0)^2)+exp(1))+1.0;
fx3=(sin(sqrt((x3(1)-50.0)^2+(x3(2)-50.0)^2)+exp(1)))/(sqrt((x3(1)-50.0)^2+(x3(2)-50.0)^2)+exp(1))+1.0;
fx(1)=fx1; fx(2)=fx2; fx(3)=fx3;
x(:,1)=x1; x(:,2)=x2; x(:,3)=x3;
[OrderF,IndexF]=sort(fx);
x1=x(:,IndexF(3)); x2=x(:,IndexF(2)); x3=x(:,IndexF(1));
fx1=OrderF(3); fx2=OrderF(2); fx3=OrderF(1);
clear OrderF; clear IndexF; clear fx; clear x;
x4=x1+x2-x3; % 反射
for i=1:2
if x4(i)>100.0,
x4(i)=100.0;
end
if x4(i)<-100.0,
x4(i)=-100.0;
end
end
fx4=(sin(sqrt((x4(1)-50.0)^2+(x4(2)-50.0)^2)+exp(1)))/(sqrt((x4(1)-50.0)^2+(x4(2)-50.0)^2)+exp(1))+1.0;
kzxs=1.5;
if fx4>fx1 % 扩张操作
x5=(x1+x2)/2+kzxs*(x4-(x1+x2)/2);
for i=1:2
if x5(i)>100.0,
x5(i)=100.0;
end
if x5(i)<-100.0,
x5(i)=-100.0;
end
end
fx5=(sin(sqrt((x5(1)-50.0)^2+(x5(2)-50.0)^2)+exp(1)))/(sqrt((x5(1)-50.0)^2+(x5(2)-50.0)^2)+exp(1))+1.0;
if fx5>=fx4 % 再次扩张
x4=x5; fx4=fx5; % 保留最佳
x5=(x1+x2)/2+kzxs*(x4-(x1+x2)/2);
for i=1:2
if x5(i)>100.0,
x5(i)=100.0;
end
if x5(i)<-100.0,
x5(i)=-100.0;
end
end
fx5=(sin(sqrt((x5(1)-50.0)^2+(x5(2)-50.0)^2)+exp(1)))/(sqrt((x5(1)-50.0)^2+(x5(2)-50.0)^2)+exp(1))+1.0;
if fx5>=fx4 % 再次扩张
x4=x5; fx4=fx5; % 保留最佳
x5=(x1+x2)/2+kzxs*(x4-(x1+x2)/2);
for i=1:2
if x5(i)>100.0,
x5(i)=100.0;
end
if x5(i)<-100.0,
x5(i)=-100.0;
end
end
fx5=(sin(sqrt((x5(1)-50.0)^2+(x5(2)-50.0)^2)+exp(1)))/(sqrt((x5(1)-50.0)^2+(x5(2)-50.0)^2)+exp(1))+1.0;
if fx5>=fx4 % 再次扩张
x4=x5; fx4=fx5; % 保留最佳
x5=(x1+x2)/2+kzxs*(x4-(x1+x2)/2);
for i=1:2
if x5(i)>100.0,
x5(i)=100.0;
end
if x5(i)<-100.0,
x5(i)=-100.0;
end
end
fx5=(sin(sqrt((x5(1)-50.0)^2+(x5(2)-50.0)^2)+exp(1)))/(sqrt((x5(1)-50.0)^2+(x5(2)-50.0)^2)+exp(1))+1.0;
else
x5=x4+0.5*(x5-x4);
for i=1:2
if x5(i)>100.0,
x5(i)=100.0;
end
if x5(i)<-100.0,
x5(i)=-100.0;
end
end
fx5=(sin(sqrt((x5(1)-50.0)^2+(x5(2)-50.0)^2)+exp(1)))/(sqrt((x5(1)-50.0)^2+(x5(2)-50.0)^2)+exp(1))+1.0;
end
else
x5=x4+0.5*(x5-x4);
for i=1:2
if x5(i)>100.0,
x5(i)=100.0;
end
if x5(i)<-100.0,
x5(i)=-100.0;
end
end
fx5=(sin(sqrt((x5(1)-50.0)^2+(x5(2)-50.0)^2)+exp(1)))/(sqrt((x5(1)-50.0)^2+(x5(2)-50.0)^2)+exp(1))+1.0;
end
else
x5=x4+0.5*(x5-x4);
for i=1:2
if x5(i)>100.0,
x5(i)=100.0;
end
if x5(i)<-100.0,
x5(i)=-100.0;
end
end
fx5=(sin(sqrt((x5(1)-50.0)^2+(x5(2)-50.0)^2)+exp(1)))/(sqrt((x5(1)-50.0)^2+(x5(2)-50.0)^2)+exp(1))+1.0;
if fx5>=fx4 % 再次扩张
x4=x5; fx4=fx5; % 保留最佳
x5=(x1+x2)/2+kzxs*(x4-(x1+x2)/2);
for i=1:2
if x5(i)>100.0,
x5(i)=100.0;
end
if x5(i)<-100.0,
x5(i)=-100.0;
end
end
fx5=(sin(sqrt((x5(1)-50.0)^2+(x5(2)-50.0)^2)+exp(1)))/(sqrt((x5(1)-50.0)^2+(x5(2)-50.0)^2)+exp(1))+1.0;
if fx5>=fx4 % 再次扩张
x4=x5; fx4=fx5; % 保留最佳
x5=(x1+x2)/2+kzxs*(x4-(x1+x2)/2);
for i=1:2
if x5(i)>100.0,
x5(i)=100.0;
end
if x5(i)<-100.0,
x5(i)=-100.0;
end
end
fx5=(sin(sqrt((x5(1)-50.0)^2+(x5(2)-50.0)^2)+exp(1)))/(sqrt((x5(1)-50.0)^2+(x5(2)-50.0)^2)+exp(1))+1.0;
else
x5=x4+0.5*(x5-x4);
for i=1:2
if x5(i)>100.0,
x5(i)=100.0;
end
if x5(i)<-100.0,
x5(i)=-100.0;
end
end
fx5=(sin(sqrt((x5(1)-50.0)^2+(x5(2)-50.0)^2)+exp(1)))/(sqrt((x5(1)-50.0)^2+(x5(2)-50.0)^2)+exp(1))+1.0;
end
else
x5=x4+0.5*(x5-x4);
for i=1:2
if x5(i)>100.0,
x5(i)=100.0;
end
if x5(i)<-100.0,
x5(i)=-100.0;
end
end
fx5=(sin(sqrt((x5(1)-50.0)^2+(x5(2)-50.0)^2)+exp(1)))/(sqrt((x5(1)-50.0)^2+(x5(2)-50.0)^2)+exp(1))+1.0;
if fx5>=fx4 % 再次扩张
x4=x5; fx4=fx5; % 保留最佳
x5=(x1+x2)/2+kzxs*(x4-(x1+x2)/2);
for i=1:2
if x5(i)>100.0,
x5(i)=100.0;
end
if x5(i)<-100.0,
x5(i)=-100.0;
end
end
fx5=(sin(sqrt((x5(1)-50.0)^2+(x5(2)-50.0)^2)+exp(1)))/(sqrt((x5(1)-50.0)^2+(x5(2)-50.0)^2)+exp(1))+1.0;
else
x5=x4+0.5*(x5-x4);
for i=1:2
if x5(i)>100.0,
x5(i)=100.0;
end
if x5(i)<-100.0,
x5(i)=-100.0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -