📄 pso1.m
字号:
%function [gBest]=PSO()%PSO主函数
pso_size=100;%群体规模
c1=0.5;c2=0.5;%学习因子
w_max=0.8;%最大权重
w_min=0.4;%最小权重
w=w_max;
Pb=10000000;%适应度值
Pb1=10000000;
Pb2=10000000;
dimens=1;%待优化问题的维数
run_max=100;%迭代次数上限
X=zeros(1,pso_size);
V=zeros(1,pso_size);
Xb=zeros(1,pso_size);
Xb1=zeros(1,pso_size);
Xb2=zeros(1,pso_size);
Yb=zeros(1,pso_size);
Yb1=zeros(1,pso_size);
Yb2=zeros(1,pso_size);
P=zeros(1,8000);
PL=zeros(1,pso_size);
Pp=0;Pp1=0;Pp2=0;
dpBest=zeros(1,pso_size);
dgBest=0;
gBest=0;
s1=zeros(1,run_max);
s2=zeros(1,run_max);
q=0;
L=0.05;%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!精度
t=1;% !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!P(t)中t初值为1
for i=1:pso_size%初始化粒子速度位置
X(i)=-5+12*rand;
X1(i)=X(i);
X2(i)=X(i);
Xb(i)=X(i);
Xb1(i)=X(i);
Xb2(i)=X(i);
V(i)=(-12+24*rand)/10;
end
for i=1:pso_size
Yb1(i)=f1(X(i));%$$$$计算f1中个体适应度值,步骤3$$$$
if Yb1(i)<Pb1
Pb1=Yb1(i);%$$$$f1中全局极值$$$$ 步骤3,4
Pp1=X(i);
end
end
for i=1:pso_size
Yb2(i)=f2(X(i));%$$$$计算f2中个体适应度值$$$$
if Yb2(i)<Pb2
Pb2=Yb2(i);%$$$$f2中全局极值$$$$ 步骤3,4
Pp2=X(i);
end
end
gBest=(Pp1+Pp2)/2;% 步骤5
Pp=gBest;
dgBest=abs(Pp1-Pp2);%$$$$计算全局最优值的距离
for i=1:pso_size
dpBest(i)=abs(Xb1(i)-Xb2(i));%$$$$计算各粒子间的距离
if dpBest(i)<dgBest
q=rand;
if q>0.5
Xb(i)=Xb1(i);
else
Xb(i)=Xb2(i);
end %$$$$个体极值随机选取
else
Xb(i)=(Xb1(i)+Xb2(i))/2;%$$$$取两函数个体极值的均值
end
PL(i)=(Yb1(i)+Yb2(i))/2;%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if PL(i)<L
P(t)=X(i);
t=t+1;
end%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
end
%P
for count=1:run_max%循环迭代
for k=1:pso_size
V(k)=w*V(k)+c1*rand*(Xb(k)-X(k))+c2*rand*(Pp-X(k));
if abs(V(k))>12%判断是否超出Vmax
if V(k)>0
V(k)=12;
else
V(k)=-12;
end
end
X(k)=X(k)+V(k);
X1(k)=X(k);
X2(k)=X(k);
temp1=f1(X1(k)); %$$$$在f1中计算个体适应度值$$$$
if temp1<Yb1(k)
Yb1(k)=temp1;%$$$$判断是否更改个体极值$$$$
Xb1(k)=X1(k);
end
if Yb1(k)<Pb1
Pb1=Yb1(k);%$$$$得到新的f1中全局极值Pb1$$$$
Pp1=X1(k);
end
temp2=f2(X2(k)); %$$$$在f2中计算个体适应度值$$$$
if temp2<Yb2(k)
Yb2(k)=temp2;%$$$$判断是否更改个体极值$$$$
Xb2(k)=X2(k);
end
if Yb2(k)<Pb2
Pb2=Yb2(k);%$$$$
Pp2=X2(k);%$$$$
end
gBest=(Pp1+Pp2)/2;%得到全局最优
Pp=gBest;%$$$$全局极值,
dgBest=abs(Pp1-Pp2);%$$$$计算全局最优值的距离
dpBest(k)=abs(Xb1(k)-Xb2(k));
if dpBest(k)<dgBest %$$$$比较个体极值的距离$$$$
q=rand;
if q>0.5
Xb(k)=Xb1(k);
else
Xb(k)=Xb2(k);
end %$$$$个体极值随机选取
else
Xb(k)=(Xb1(k)+Xb2(k))/2;%$$$$取两函数个体极值的均值,得到目标均衡适应度值$$$$
end
PL(k)=(Yb1(k)+Yb2(k))/2;
%flag=0;
if PL(k)<L%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
a=0;
for e=1:t-1
if P(e)==X(k)
a=1;
end
end
if a~=1
P(t)=X(k);
t=t+1;
end
end
% if PL(k)<L
% for e=1:t
% if P(t)不等于X(k)
%P(t)=X(k);
%t=t+1;
%end
%end
end%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
%end
w=w_max-count*(w_max-w_min)/run_max;%权值更新公式
s1(count)=Pb1;
s2(count)=Pb2;
end
t
for r=1:t
S3(r)=f1(P(r));
S4(r)=f2(P(r));
end
s1;
s2;
S3;
S4;
plot(S3,S4,'r*')
hold on
plot(s1,s2,'b#')
hold off
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -