主程序: P(i)=fitness2(x(i,:)); childv1(i,:)=(PoolVX(seed1,:)+PoolVX(seed2,:))*norm(PoolVX(seed1,:))/norm(PoolVX(seed1,:)+Po
%------基本粒子群优化算法(Particle Swarm Optimization)-----------
%------名称:混合粒子群优化算法(基于杂交的算法)
%------作用:求解优化问题
%------说明:全局性,并行性,高效的群体智能算法
%------初始格式化--------------------------------------------------
clear all;
clc;
format long;
%------给定初始化条件----------------------------------------------
c1=2; %学习因子1
%c1=3;
%c2=2;
Pc=0.9;
Sp=0.2;
c2=2; %学习因子2
w=0.7; %惯性权重
%MaxDT=500;
MaxDT=10000; %最大迭代次数
D=5; %搜索空间维数(未知数个数)
N=40; %初始化群体个体数目
%eps=10^(-6); %设置精度(在已知最小值时候用)
%------初始化种群的个体(可以在这里限定位置和速度的范围)------------
fori=1:N
for j=1:D
x(i,j)=randn; %随机初始化位置
v(i,j)=randn; %随机初始化速度
end
end
%------先计算各个粒子的适应度,并初始化Pi和Pg----------------------
figure(3)
fori=1:N
P(i)=fitness2(x(i,:));
y(i,:)=x(i,:);
end
Pg=x(N,:); %Pg为全局最优
fori=1:(N-1)
if fitness2(x(i,:))
end
end
%------进入主要循环,按照公式依次迭代,直到满足精度要求------------
for t=1:MaxDT
fori=1:N
v(i,:)=w*v(i,:)+c1*rand*(y(i,:)-x(i,:))+c2*rand*(Pg-x(i,:));
x(i,:)=x(i,:)+v(i,:);
if fitness2(x(i,:))
y(i,:)=x(i,:);
end
if P(i)
end
r1=rand();
if r1
PoolX=x(1:numPool,:); %杂交池中粒子的位置
PoolVX=v(1:numPool,:); %杂交池中粒子的速度
fori=1:numPool
seed1=floor(rand()*(numPool-1))+1;
seed2=floor(rand()*(numPool-1))+1;
pb=rand();
childx1(i,:)=pb*PoolX(seed1,:)+(1-pb)*PoolX(seed2,:); %子代的位置计算
olVX(seed2,:));
end
x(1:numPool,:)=childx1; %子代位置替换父代位置
v(1:numPool,:)=childv1; %子代速度替换父代速度
end
Pbest(t)=fitness2(Pg);
end
end
plot(Pbest)
TempStr=sprintf('c1= %g ,c2=%g',c1,c2);
title(TempStr);
xlabel('迭代次数');
ylabel('适应度值');
%------最后给出计算结果
disp('*************************************************************')
disp('函数的全局最优位置为:')
Solution=Pg
disp('最后得到的优化极值为:')
Result=fitness2(Pg)
disp('*************************************************************')