📄 dongnenpsodejong.asv
字号:
% *********************************************************************
% 陈为栋-微粒群优化算法求解----动能PSO算法
% *********************************************************************
%以DeJong函数为测试对象
%该函数只有一个全局最小点:(0,。。。,0),最小值为:0。
%变量取值范围都是(-10,10)
%无论怎样变化,达到最优的概率很小,但是所有的值都在1.0e-008 *0.0335左右,
%即与最小值很近的概率为100%
function dongnenPSODeJong
clc
for iii=1:1:10 %整个程序运行的次数
iii=iii
% 变量初始化
flag = 0; % 切换标志
PopSize = 30; % 种群数目
MaxIt = 2000; % 最大迭代次数
iter = 0; % 迭代次数计数器
c1 = 2;
c2 = 2;
c3 = 2;
maxw=1.4; minw=0.02; % 惯性权值
%定义计算适应值的函数
dim =2 ; % 问题的维数,也就是问题的变量数目
ErrGoal =0.01; % 误差精度
%碰撞前两个平均速度赋值为零
V1=0;
V2=0;
sum=0;%计算平均值
% 初始化种群的速度、位置,计算适应值
puopul = rand(dim, PopSize)*(10-(-10)) + (-10);
vel = rand(dim, PopSize);
for i = 1:PopSize
fit(i) = DeJong(puopul(:,i)); %计算各粒子的函数适应值
end
%找到个体最优和群体最优
ibestpos = puopul; %个体最优位置
ibestfit = fit; %个体最优适应值
[bestpart,t] = min(fit);%找到全局最优的值和所在位置
gbestfit = bestpart; % 保存全局最优适应值
%第一次迭代
iter=iter+1;
w=maxw-iter*(maxw-minw)/MaxIt; % 当前步所用的惯性权值
% 速度更新
for i=1:PopSize
A(:,i) = puopul(:,t);%把全局最优适应值对应的个体位置赋给A的每一列
end
R1 = rand(dim, PopSize);%两个随机参数都是矩阵
R2 = rand(dim, PopSize);
vel = 0.5*(w*vel + c1*R1.*(ibestpos-puopul) + c2*R2.*(A-puopul));
puopul = puopul + vel;
% 粒子位置更新
clear A; clear R1; clear R2;
% 限幅处理
for j=1:dim
for i=1:PopSize
if puopul(j,i)>10
puopul(j,i)=10 ;
end
if puopul(j,i)<-10
puopul(j,i)=-10 ;
end
end
end
% 种群进化,即计算每个粒子的适应值
for i = 1:PopSize
fit(i) = DeJong(puopul(:,i)); %计算各粒子的函数适应值
end
% 更新个体历史最好位置
for i=1:PopSize
if fit(i)<ibestfit(i),
ibestfit(i)=fit(i);
ibestpos(:,i)=puopul(:,i);
end
end
% 更新全局历史最好位置
[bestpart,t]=min(fit);
if bestpart<gbestfit;
gbestfit=bestpart;
end
%计算平均速度V1
for i=1:PopSize
sum=sum+vel(:,i);
end
V1=sum/PopSize;
%计算最优粒子速度V2
V2 = vel(:,t);
sum=0;
while (iter < MaxIt)
iter = iter + 1;
w=maxw-iter*(maxw-minw)/MaxIt; % 当前步所用的惯性权值
% 速度更新
for i=1:PopSize
A(:,i) = puopul(:,t);%把全局最优适应值对应的个体位置赋给A的每一列
end
R1 = rand(dim, PopSize);%两个随机参数都是矩阵
R2 = rand(dim, PopSize);
R3 = rand(dim, PopSize);
k=(1/3)*rand(1);%给定k值范围
T1=0.25*(1-k^2).*(V1-V2).^2;% 动能补偿函数
%计算平均距离xbar
for i=1:PopSize
sum=sum+puopul(:,i);
end
xbar=sum/PopSize;
sum=0;
%碰撞评价函数判断
u=(puopul(1,t)-xbar(1,1))^2+(puopul(2,t)-xbar(2,1))^2;
if sqrt(u)>ErrGoal
flag=0;
else
flag=1;
end
for i=1:PopSize
T(:,i)=T1;
end
vel=0.5*(w*vel+c1*R1.*(ibestpos-puopul)+c2*R2.*(A-puopul)+flag*c3*R3.*T);%新的速度更新公式
puopul = puopul + vel;
% 粒子位置更新
clear A; clear R1; clear R2; clear R3; clear T;
% 限幅处理
for j=1:dim
for i=1:PopSize
if puopul(j,i)>10
puopul(j,i)=10 ;
end
if puopul(j,i)<-10
puopul(j,i)=-10 ;
end
end
end
% 种群进化,即计算每个粒子的适应值
for i = 1:PopSize
fit(i) = DeJong(puopul(:,i)); %计算各粒子的函数适应值
end
% 更新个体历史最好位置
for i=1:PopSize,
if fit(i)<ibestfit(i),
ibestfit(i)=fit(i);
ibestpos(:,i)=puopul(:,i);
end
end
% 更新全局历史最好位置
[bestpart,t]=min(fit);
if bestpart<gbestfit;
gbestfit=bestpart;
end
%currentTime = etime(clock,startTime); %计算时间
end
%程序运行完一次后得到的最优值
x0=puopul(:,t)
pp(iii)=gbestfit
end
%fprintf(' The best vector is : n');
%fprintf('--%g',xmin);
%figure
%plot(1:iii,psogbestfit(1:iii),'-r')
%xlabel('迭代次数'); % 坐标标注
%ylabel('每一步的最佳适应值');
%title('测试函数为GRIE的优化结果图:');
% dongnenPSO求解结束
%**********************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -