一种非线性动态自适应惯性权重PSO算法(IPSO)Matlab代码
[1]王生亮,刘根友.一种非线性动态自适应惯性权重PSO算法[J].计算机仿真,2021,38(04):249-253+451.
部分代码:
function [gbest,gbestval,fitcount]= CLPSO_new_func(fhd,Max_Gen,Max_FES,Particle_Number,Dimension,VRmin,VRmax,varargin)
%[gbest,gbestval,fitcount]= CLPSO_new_func('f8',3500,200000,30,30,-5.12,5.12)
rand('state',sum(100*clock));
me=Max_Gen;
ps=Particle_Number;
D=Dimension;
cc=[1 1]; %acceleration constants
t=0:1/(ps-1):1;t=5.*t;
Pc=0.0+(0.5-0.0).*(exp(t)-exp(t(1)))./(exp(t(ps))-exp(t(1)));
% Pc=0.5.*ones(1,ps);
m=0.*ones(ps,1);
iwt=0.9-(1:me)*(0.7/me);
% iwt=0.729-(1:me)*(0.0/me);
cc=[1.49445 1.49445];
if length(VRmin)==1VRmin=repmat(VRmin,1,D);VRmax=repmat(VRmax,1,D);
end
mv=0.2*(VRmax-VRmin);
VRmin=repmat(VRmin,ps,1);
VRmax=repmat(VRmax,ps,1);
Vmin=repmat(-mv,ps,1);
Vmax=-Vmin;
pos=VRmin+(VRmax-VRmin).*rand(ps,D);for i=1:ps;e(i,1)=feval(fhd,pos(i,:),varargin{:});
endfitcount=ps;
vel=Vmin+2.*Vmax.*rand(ps,D);%initialize the velocity of the particles
pbest=pos;
pbestval=e; %initialize the pbest and the pbest's fitness value
[gbestval,gbestid]=min(pbestval);
gbest=pbest(gbestid,:);%initialize the gbest and the gbest's fitness value
gbestrep=repmat(gbest,ps,1);stay_num=zeros(ps,1);ai=zeros(ps,D);
f_pbest=1:ps;f_pbest=repmat(f_pbest',1,D);
for k&#61;1:psar&#61;randperm(D);ai(k,ar(1:m(k)))&#61;1;fi1&#61;ceil(ps*rand(1,D));fi2&#61;ceil(ps*rand(1,D));fi&#61;(pbestval(fi1)<pbestval(fi2))&#39;.*fi1&#43;(pbestval(fi1)>&#61;pbestval(fi2))&#39;.*fi2;bi&#61;ceil(rand(1,D)-1&#43;Pc(k));if bi&#61;&#61;zeros(1,D),rc&#61;randperm(D);bi(rc(1))&#61;1;endf_pbest(k,:)&#61;bi.*fi&#43;(1-bi).*f_pbest(k,:);
endstop_num&#61;0;
i&#61;1;while i<&#61;me&fitcount<&#61;Max_FESi&#61;i&#43;1;for k&#61;1:psif stay_num(k)>&#61;5% if round(i/10)&#61;&#61;i/10%|stay_num(k)>&#61;5stay_num(k)&#61;0;ai(k,:)&#61;zeros(1,D);f_pbest(k,:)&#61;k.*ones(1,D);ar&#61;randperm(D);ai(k,ar(1:m(k)))&#61;1;fi1&#61;ceil(ps*rand(1,D));fi2&#61;ceil(ps*rand(1,D));fi&#61;(pbestval(fi1)<pbestval(fi2))&#39;.*fi1&#43;(pbestval(fi1)>&#61;pbestval(fi2))&#39;.*fi2;bi&#61;ceil(rand(1,D)-1&#43;Pc(k));if bi&#61;&#61;zeros(1,D),rc&#61;randperm(D);bi(rc(1))&#61;1;endf_pbest(k,:)&#61;bi.*fi&#43;(1-bi).*f_pbest(k,:);endfor dimcnt&#61;1:Dpbest_f(k,dimcnt)&#61;pbest(f_pbest(k,dimcnt),dimcnt);endaa(k,:)&#61;cc(1).*(1-ai(k,:)).*rand(1,D).*(pbest_f(k,:)-pos(k,:))&#43;cc(2).*ai(k,:).*rand(1,D).*(gbestrep(k,:)-pos(k,:));%~~~~~~~~~~~~~~~~~~~~~~vel(k,:)&#61;iwt(i).*vel(k,:)&#43;aa(k,:);vel(k,:)&#61;(vel(k,:)>mv).*mv&#43;(vel(k,:)<&#61;mv).*vel(k,:);vel(k,:)&#61;(vel(k,:)<(-mv)).*(-mv)&#43;(vel(k,:)>&#61;(-mv)).*vel(k,:);pos(k,:)&#61;pos(k,:)&#43;vel(k,:);if (sum(pos(k,:)>VRmax(k,:))&#43;sum(pos(k,:)<VRmin(k,:)))&#61;&#61;0;e(k,1)&#61;feval(fhd,pos(k,:),varargin{:});fitcount&#61;fitcount&#43;1;tmp&#61;(pbestval(k)<&#61;e(k));if tmp&#61;&#61;1stay_num(k)&#61;stay_num(k)&#43;1;endtemp&#61;repmat(tmp,1,D);pbest(k,:)&#61;temp.*pbest(k,:)&#43;(1-temp).*pos(k,:);pbestval(k)&#61;tmp.*pbestval(k)&#43;(1-tmp).*e(k);%update the pbestif pbestval(k)<gbestvalgbest&#61;pbest(k,:);gbestval&#61;pbestval(k);gbestrep&#61;repmat(gbest,ps,1);%update the gbestendendend% if round(i/100)&#61;&#61;i/100% plot(pos(:,D-1),pos(:,D),&#39;b*&#39;);hold on;% for k&#61;1:floor(D/2)% plot(gbest(:,2*k-1),gbest(:,2*k),&#39;r*&#39;);% end% hold off% title([&#39;PSO: &#39;,num2str(i),&#39; generations, Gbestval&#61;&#39;,num2str(gbestval)]);% axis([VRmin(1,D-1),VRmax(1,D-1),VRmin(1,D),VRmax(1,D)])% drawnow% endif fitcount>&#61;Max_FESbreak;endif (i&#61;&#61;me)&(fitcount<Max_FES)i&#61;i-1;end
end
gbestval