?? smpso_f1_0.m
字號:
% A SIMPLE IMPLEMENTATION OF THE PARTICLE SWARM OPTIMIZATION
clear;
clc;
for iii=1:1:2
iii=iii
% *************************************************************************
% 單純形微粒群優化算法求解----smPSO
% *************************************************************************
% 初始化
PopSize=20; MaxIt=3000;
maxw=1.8; minw=0.02; c1=2.0; c2=2.0; dim=2;
popul(1,:)=rand(1,PopSize)*(12.1+3.0)-3.0; % 位置初始化
popul(2,:)=rand(1,PopSize)*(5.8-4.1)+4.1; % 位置初始化
vel=rand(dim,PopSize); % 速度初始化
for i=1:PopSize
fit(i)=21.5+popul(1,i)*sin(4.0*pi*popul(1,i))+popul(2,i)*sin(20.0*pi*popul(2,i));
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
if popul(1,i)>12.1,
popul(1,i)=12.1;
end
if popul(1,i)<-3.0,
popul(1,i)=-3.0;
end
if popul(2,i)>5.8
popul(2,i)=5.8;
end
if popul(2,i)<4.1
popul(2,i)=4.1;
end
end
for i=1:PopSize
fit(i)=21.5+popul(1,i)*sin(4.0*pi*popul(1,i))+popul(2,i)*sin(20.0*pi*popul(2,i));
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=21.5+x1(1)*sin(4.0*pi*x1(1))+x1(2)*sin(20.0*pi*x1(2));
fx2=21.5+x2(1)*sin(4.0*pi*x2(1))+x2(2)*sin(20.0*pi*x2(2));
fx3=21.5+x3(1)*sin(4.0*pi*x3(1))+x3(2)*sin(20.0*pi*x3(2));
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*15.1/2+a1; % 位置初始化
x2(2)=(rand(1,1)-0.5)*2*1.7/2+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*15.1/2+a1; % 位置初始化
x3(2)=(rand(1,1)-0.5)*2*1.7/2+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*15.1/2+a1; % 位置初始化
x3(2)=(rand(1,1)-0.5)*2*1.7/2+a2; % 位置初始化
end
clear error;
if x2(1)>12.1,
x2(1)=12.1;
end
if x2(1)<-3.0,
x2(1)=-3.0;
end
if x2(2)>5.8
x2(2)=5.8;
end
if x2(2)<4.1
x2(2)=4.1;
end
if x3(1)>12.1,
x3(1)=12.1;
end
if x3(1)<-3.0,
x3(1)=-3.0;
end
if x3(2)>5.8
x3(2)=5.8;
end
if x3(2)<4.1
x3(2)=4.1;
end
fx1=21.5+x1(1)*sin(4.0*pi*x1(1))+x1(2)*sin(20.0*pi*x1(2));
fx2=21.5+x2(1)*sin(4.0*pi*x2(1))+x2(2)*sin(20.0*pi*x2(2));
fx3=21.5+x3(1)*sin(4.0*pi*x3(1))+x3(2)*sin(20.0*pi*x3(2));
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; % 反射
if x4(1)>12.1,
x4(1)=12.1;
end
if x4(1)<-3.0,
x4(1)=-3.0;
end
if x4(2)>5.8
x4(2)=5.8;
end
if x4(2)<4.1
x4(2)=4.1;
end
fx4=21.5+x4(1)*sin(4.0*pi*x4(1))+x4(2)*sin(20.0*pi*x4(2));
kzxs=1.5;
if fx4>fx1 % 擴張操作
x5=(x1+x2)/2+kzxs*(x4-(x1+x2)/2);
if x5(1)>12.1,
x5(1)=12.1;
end
if x5(1)<-3.0,
x5(1)=-3.0;
end
if x5(2)>5.8
x5(2)=5.8;
end
if x5(2)<4.1
x5(2)=4.1;
end
fx5=21.5+x5(1)*sin(4.0*pi*x5(1))+x5(2)*sin(20.0*pi*x5(2));
if fx5>=fx4 % 再次擴張
x4=x5; fx4=fx5; % 保留最佳
x5=(x1+x2)/2+kzxs*(x4-(x1+x2)/2);
if x5(1)>12.1,
x5(1)=12.1;
end
if x5(1)<-3.0,
x5(1)=-3.0;
end
if x5(2)>5.8
x5(2)=5.8;
end
if x5(2)<4.1
x5(2)=4.1;
end
fx5=21.5+x5(1)*sin(4.0*pi*x5(1))+x5(2)*sin(20.0*pi*x5(2));
if fx5>=fx4 % 再次擴張
x4=x5; fx4=fx5; % 保留最佳
x5=(x1+x2)/2+kzxs*(x4-(x1+x2)/2);
if x5(1)>12.1,
x5(1)=12.1;
end
if x5(1)<-3.0,
x5(1)=-3.0;
end
if x5(2)>5.8
x5(2)=5.8;
end
if x5(2)<4.1
x5(2)=4.1;
end
fx5=21.5+x5(1)*sin(4.0*pi*x5(1))+x5(2)*sin(20.0*pi*x5(2));
if fx5>=fx4 % 再次擴張
x4=x5; fx4=fx5; % 保留最佳
x5=(x1+x2)/2+kzxs*(x4-(x1+x2)/2);
if x5(1)>12.1,
x5(1)=12.1;
end
if x5(1)<-3.0,
x5(1)=-3.0;
end
if x5(2)>5.8
x5(2)=5.8;
end
if x5(2)<4.1
x5(2)=4.1;
end
fx5=21.5+x5(1)*sin(4.0*pi*x5(1))+x5(2)*sin(20.0*pi*x5(2));
else
x5=x4+0.5*(x5-x4);
if x5(1)>12.1,
x5(1)=12.1;
end
if x5(1)<-3.0,
x5(1)=-3.0;
end
if x5(2)>5.8
x5(2)=5.8;
end
if x5(2)<4.1
x5(2)=4.1;
end
fx5=21.5+x5(1)*sin(4.0*pi*x5(1))+x5(2)*sin(20.0*pi*x5(2));
end
else
x5=x4+0.5*(x5-x4);
if x5(1)>12.1,
x5(1)=12.1;
end
if x5(1)<-3.0,
x5(1)=-3.0;
end
if x5(2)>5.8
x5(2)=5.8;
end
if x5(2)<4.1
x5(2)=4.1;
end
fx5=21.5+x5(1)*sin(4.0*pi*x5(1))+x5(2)*sin(20.0*pi*x5(2));
end
else
x5=x4+0.5*(x5-x4);
if x5(1)>12.1,
x5(1)=12.1;
end
if x5(1)<-3.0,
x5(1)=-3.0;
end
if x5(2)>5.8
x5(2)=5.8;
end
if x5(2)<4.1
x5(2)=4.1;
end
fx5=21.5+x5(1)*sin(4.0*pi*x5(1))+x5(2)*sin(20.0*pi*x5(2));
if fx5>=fx4 % 再次擴張
x4=x5; fx4=fx5; % 保留最佳
x5=(x1+x2)/2+kzxs*(x4-(x1+x2)/2);
if x5(1)>12.1,
x5(1)=12.1;
end
if x5(1)<-3.0,
x5(1)=-3.0;
end
if x5(2)>5.8
x5(2)=5.8;
end
if x5(2)<4.1
x5(2)=4.1;
end
fx5=21.5+x5(1)*sin(4.0*pi*x5(1))+x5(2)*sin(20.0*pi*x5(2));
if fx5>=fx4 % 再次擴張
x4=x5; fx4=fx5; % 保留最佳
x5=(x1+x2)/2+kzxs*(x4-(x1+x2)/2);
if x5(1)>12.1,
x5(1)=12.1;
end
if x5(1)<-3.0,
x5(1)=-3.0;
end
if x5(2)>5.8
x5(2)=5.8;
end
if x5(2)<4.1
x5(2)=4.1;
end
fx5=21.5+x5(1)*sin(4.0*pi*x5(1))+x5(2)*sin(20.0*pi*x5(2));
else
x5=x4+0.5*(x5-x4);
if x5(1)>12.1,
x5(1)=12.1;
end
if x5(1)<-3.0,
x5(1)=-3.0;
end
if x5(2)>5.8
x5(2)=5.8;
end
if x5(2)<4.1
x5(2)=4.1;
end
fx5=21.5+x5(1)*sin(4.0*pi*x5(1))+x5(2)*sin(20.0*pi*x5(2));
end
else
x5=x4+0.5*(x5-x4);
if x5(1)>12.1,
x5(1)=12.1;
end
if x5(1)<-3.0,
x5(1)=-3.0;
end
if x5(2)>5.8
x5(2)=5.8;
end
if x5(2)<4.1
x5(2)=4.1;
end
fx5=21.5+x5(1)*sin(4.0*pi*x5(1))+x5(2)*sin(20.0*pi*x5(2));
if fx5>=fx4 % 再次擴張
x4=x5; fx4=fx5; % 保留最佳
x5=(x1+x2)/2+kzxs*(x4-(x1+x2)/2);
if x5(1)>12.1,
x5(1)=12.1;
end
if x5(1)<-3.0,
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -