内容:
popsize=40; %微粒数
MAXITER=100; %执行次数
dimension=3; %维数
runno=10;
xmin1=0.316115;
xmin2=0.9;
xmin3=0.64057;
xmax1=12.328485;
xmax2=35.1;
xmax3=24.98223;
%x(i,j)的范围50-100
vmax1=(xmax1-xmin1)/2;
vmax2=(xmax2-xmin2)/2;
vmax3=(xmax3-xmin3)/2;
%M=(xmax-xmin)/2;
c1=2;
c2=2;
data2=zeros(runno,MAXITER);
sum2=0;
G=zeros(runno,dimension);
st=0;
for run=1:runno
x1=(xmax1-xmin1)*rand(popsize,1) + xmin1; %初始化x,20*30
x2=(xmax2-xmin2)*rand(popsize,1) + xmin2;
x3=(xmax3-xmin3)*rand(popsize,1) + xmin3;
v1=2*vmax1*rand(popsize,1)-vmax1; %初始化v,20*30
v2=2*vmax2*rand(popsize,1)-vmax2;
v3=2*vmax3*rand(popsize,1)-vmax3;
x=rand(popsize,dimension);
v=zeros(popsize,dimension);
v11=zeros(popsize,dimension);
x(:,1)=x1;
x(:,2)=x2;
x(:,3)=x3;
v(:,1)=v1;
v(:,2)=v2;
v(:,3)=v3;
pbest=x; %每个微粒的最优解,20*30
for i=1:popsize
f_x(i)=f1(x(i,:)); %用自定义的函数f1来初始化f_x(i)
f_pbest(i)=f_x(i); %并将f_x(i)的植暂时充当每个微粒的最优函数解f_pbest(i)
end
g=min(find(f_pbest==min(f_pbest(1:popsize)))); %找出20个微粒中f_pbest最小的那个,把该微粒的编号赋予g。
gbest=pbest(g,:); %将这个微粒的最优解当作全局最优解gbest
f_gbest=f_pbest(g); %这个微粒的函数解也当作全局最优函数解f_gbest