?? smpso_f2.m
字號:
% 爬山微粒群算法求解結束
%**********************************************************************
clear gbestpos; clear gbestfit; clear ibestfit; clear ibestpos;
clear bestpart; clear g; clear popul; clear vel; clear fit;
% *************************************************************************
% 微粒群優化算法求解----基本PSO
% *************************************************************************
% 初始參數
PopSize0=PopSize; % 微粒個數
puopul=rand(2,PopSize0)*200.0-100.0; % 位置初始化
vell=rand(dim,PopSize0); % 速度初始化
for i=1:PopSize0, % 計算適應值
fitt(i)=(sin(sqrt((puopul(1,i)-50.0)^2+(puopul(2,i)-50.0)^2)+exp(1)))/(sqrt((puopul(1,i)-50.0)^2+(puopul(2,i)-50.0)^2)+exp(1))+1.0;
end
% 將各微粒的位置設置為當前各微粒最好的位置
ibestpos=puopul; % 個體最好位置初始化
ibestfit=fitt; % 各個體的適應值
% 找出全局最好的初始微粒
[bestpart,g]=max(fitt); % 找全局最好的適應值
gbestfit=bestpart; % 全局最好的適應值
gbestpos=puopul(:,g); % 全局最好的適應值對應的個體
% 迭代開始/主程序開始運行
iter=0;
while (iter<MaxIt), % 迭代開始
iter=iter+1;
w=maxw-iter*(maxw-minw)/MaxIt; % 當前步所用的慣性權值
for i=1:PopSize0, % 將全局最好的適應值對應的個體展開(若不展開來,沒法相減)
A(:,i)=gbestpos;
end
R1=rand(dim,PopSize0); R2=rand(dim,PopSize0);
vell=0.8*(w*vell+c1*R1.*(ibestpos-puopul)+c2*R2.*(A-puopul)); % 速度迭代計算
puopul=puopul+vell; % 位置迭代計算
clear A; clear R1; clear R2;
% 位置限幅處理
for i=1:PopSize0
for j=1:dim
if puopul(j,i)>100.0, % 限幅處理
puopul(j,i)=100.0;
end
if puopul(j,i)<-100.0,
puopul(j,i)=-100.0;
end
end
end
% 各微粒的適應值計算
for i=1:PopSize0, % 適應值計算
fitt(i)=(sin(sqrt((puopul(1,i)-50.0)^2+(puopul(2,i)-50.0)^2)+exp(1)))/(sqrt((puopul(1,i)-50.0)^2+(puopul(2,i)-50.0)^2)+exp(1))+1.0;
end
% 更新個體歷史最好位置
for i=1:PopSize0,
if fitt(i)>ibestfit(i),
ibestfit(i)=fitt(i);
ibestpos(:,i)=puopul(:,i);
end
end
% 更新全局歷史最好位置
[bestpart,g]=max(fitt);
if bestpart>gbestfit;
gbestfit=bestpart;
gbestpos=puopul(:,g);
end
end
PSOgbestfit(iii)=gbestfit; % 輸出全局歷史最好適應值(最優f1值)
% 基本PSO求解結束
%**************************************************************************
clear gbestpos; clear gbestfit; clear ibestfit; clear ibestpos; clear i;
clear bestpart; clear g; clear puopul; clear vell; clear fitt;
%************************************************************************
% 單純形搜索----基本sm
%**********************************************************************
x=rand(2,3)*200.0-100.0; % 位置初始化
x1=x(:,1); % 選單純形的頂點
x2=x(:,2);
x3=x(:,3);
gbestfit=-600; % 附初始最好值
for tempii=1:MaxIt % 進行單純形法搜索
% 按最優次序x1,x2,x3排列
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;
error=sqrt((x1(1)-x2(1))^2+(x1(2)-x2(2))^2);
if error<0.000001
x2=rand(2,1)*200-100.0; % 位置初始化
end
error=sqrt((x1(1)-x3(1))^2+(x1(2)-x3(2))^2);
if error<0.000001
x3=rand(2,1)*200-100.0; % 位置初始化
end
error=sqrt((x2(1)-x3(1))^2+(x2(2)-x3(2))^2);
if error<0.000001
x3=rand(2,1)*200-100.0; % 位置初始化
end
clear error;
% 按最優次序x1,x2,x3再次排列
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;
% 反射
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;
% 擴張
if fx4>fx1 % 擴張
x5=(x1+x2)/2+1.5*(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;
x3=x2; x2=x1;
if fx5>=fx4
x1=x5;
else
x1=x4;
end
end
clear x5; clear fx5;
% 替代操作
if fx4<=fx1 % 不壓縮也不擴張
if fx4>=fx2
x3=x2; x2=x4; x1=x1;
end
end
% 壓縮與收縮
if fx4<fx2 % 壓縮
if fx4>fx3
xa=x4; fxa=fx4;
else
xa=x3; fxa=fx3;
end
x6=(x1+x2)/2+0.7*(xa-(x1+x2)/2);
for i=1:2
if x6(i)>100.0,
x6(i)=100.0;
end
if x6(i)<-100.0,
x6(i)=-100.0;
end
end
fx6=(sin(sqrt((x6(1)-50.0)^2+(x6(2)-50.0)^2)+exp(1)))/(sqrt((x6(1)-50.0)^2+(x6(2)-50.0)^2)+exp(1))+1.0;
if fx6>=fxa
x3=x6; x2=x2; x1=x1; % 壓縮
else
x7=x2+(x1-x2)/2; % 收縮
x8=x3+(x1-x3)/2;
x1=x1; x2=x7; x3=x8;
end
end
clear xa; clear fxa; clear x6; clear fx6; clear x7; clear x8;
% 本次搜索的最佳值
fx0(1)=(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;
fx0(2)=(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;
fx0(3)=(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;
[smbestpart,smg]=max(fx0);
if smbestpart>gbestfit
gbestfit=smbestpart;
end
clear smg;
end
smgbestfit(iii)=gbestfit;
clear x1; clear x2; clear x3; clear fx0; clear tempii; clear gbestfit;
end
% 顯示過程軌跡
figure;
plot(1:iii,smPSOgbestfit(1:iii),'-r',1:iii,PSOgbestfit(1:iii),'-.b',1:iii,smgbestfit(1:iii),'--g')
xlabel('獨立運行次數'); % 坐標標注
ylabel('每一次的最佳適應值');
title('測試函數為函數2時的優化結果圖:');
legend('SM-PSO','PSO','SM');
sortpso=sort(PSOgbestfit);
sortsmpso=sort(smPSOgbestfit);
sortsm=sort(smgbestfit);
meansmpso=mean(smPSOgbestfit);
meanpso=mean(PSOgbestfit);
meansm=mean(smgbestfit);
save SMPSO_F2;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -