%[gbest,NPRule] = GetGlobalBest(NP,NPRule,Dnp); %end
for i=1:N
%-------------------更新粒子的位置和速度----------////第6步////////////////// %v(i,:)=w*v(i,:)+cmin*rand*(pbest(i,:)-x(i,:))+cmax*rand*(gbest-x(i,:)); %[gbest,NPRule] = GetGlobalBest2(x(i,:),NP,NPRule);%%%%-17 [gbest,NPRule] = GetGlobalBest(NP,NPRule,Dnp); %w1=Inf;
%while(w1>1.2 || w1<0) % w1=0.8+randn*0.8; %end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%模糊向导粒子群算法 if(params.isfmopso) nploc=x(i,:);
npruleloc=[0,0,0]; dnploc(1,1)=0;
tempv=zeros(10,D); tempx=zeros(10,D); for k=1:10
tempv(k,:)=w1*v(i,:)+c*rand*(pbest(i,:)-x(i,:))+c*rand*(gbest-x(i,:)); for j=1:D
if tempv(k,j)>vmax(j) tempv(k,j)=vmax(j); elseif tempv(k,j) tempx(k,:)=x(i,:)+tempv(k,:); faix = GetFai(x(i,:),funcname,params); if faix<=unfitx [nploc,npruleloc,dnploc] = compare(tempx(k,:),nploc,npruleloc,dnploc,10,funcname,params); end end nnx=size(nploc,1); seltemp=randi([1,nnx]); ttv=nploc(seltemp,:)-x(i,:); x(i,:)=nploc(seltemp,:); if sum(abs(ttv))>0 v(i,:)=ttv; end else %%%%%%%%%%%%%%%%%%% v(i,:)=w1*v(i,:)+c*rand*(pbest(i,:)-x(i,:))+c*rand*(gbest-x(i,:)); for j=1:D if v(i,j)>vmax(j) v(i,j)=vmax(j); elseif v(i,j) x(i,:)=x(i,:)+v(i,:); funcname=upper(funcname); if(strcmp(funcname(1:3),'CTP')) x(i,:)=x(i,:)+v(i,:); end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %-------------------采取措施,避免粒子飞出空间----------////第7步///////////// %速度位置钳制 if(params.stopatborder)%粒子随机停留在边界 for j=1:D if x(i,j)>ub(j) if(randi([0,0],1)==0) x(i,j)=ub(j); v(i,j)=-v(i,j); else x(i,j)=lb(j)+(ub(j)-lb(j))*rand; %随机初始化位置 v(i,j)=(ub(j)-lb(j))*rand*0.5; end end if x(i,j) if(randi([0,0],1)==0) x(i,j)=lb(j); v(i,j)=-v(i,j); else x(i,j)=lb(j)+(ub(j)-lb(j))*rand; %随机初始化位置 v(i,j)=(ub(j)-lb(j))*rand*0.5; end end end else %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% for j=1:D if x(i,j)>ub(j)%粒子飞出上边界 x(i,j)=x(i,j)-v(i,j); v(i,j)=rand*(ub(j)-x(i,j)); x(i,j)=x(i,j)+v(i,j); elseif x(i,j) v(i,j)=rand*(x(i,j)-lb(j)); x(i,j)=x(i,j)-v(i,j); end end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %------------------每个粒子的目标向量-----------------//第8步/////////////// goals(t,i,:)=fitness(x(i,:),funcname,params); %----------------调整自身---------------------------//第10步///////////////// domiRel = DominateRel(pbest(i,:),x(i,:),funcname,params);%x,y的支配关系 if domiRel==1%pbest支配新解 continue; else if domiRel==-1%新解支配pbest pbest(i,:) = x(i,:); elseif(randi([0,1],1)==0)%新解与pbest互相不支配 pbest(i,:) = x(i,:); end %-----------------对NP进行更新和维护-----------------//第9步//////////////// faix = GetFai(x(i,:),funcname,params); if faix<=unfitx [NP,NPRule,Dnp] = compare(x(i,:),NP,NPRule,Dnp,Nnp,funcname,params); end end end end np = NP;%非劣解 nprule=NPRule; dnp = Dnp;%非劣解之间的距离 r=size(np,1); fv=zeros(r,T); for i=1:r fv(i,:)=fitness(np(i,:),funcname,params); end end %%%%%%%%%%%%%%%--------------主函数结束 --------------%%%%%%%%%%%%%%%%% function [np_out,nprule_out,dnp_out] = compare(x,np,nprule,dnp,nnp,funcname,params) %np:现有非劣解 %x:需要比较的量 Nnp = nnp;%非劣解集空间 r=size(np,1);%非劣解的个数 np_out=np;%非劣解复本 nprule_out = nprule; dnp_out = dnp;%非劣解集点之间距离 if r==0 return; end for i=r:-1:1 domiRel=DominateRel(x,np(i,:),funcname,params); if domiRel==1 %NP(i)被x支配 np_out(i,:)=[];%非劣解剔除该解 nprule_out(i,:)=[]; dnp_out(i,:)=[]; if ~isempty(dnp_out) dnp_out(:,i)=[]; end elseif domiRel==-1 %x被NP(i)支配,返回不再比较 return; end end r1=size(np_out,1);%现有非劣解的行列 np_out(r1+1,:)=x;%与所有非支配集粒子比较均占优或不可比较,则NP中加入x faix = GetFai(x,funcname,params); if faix > 0 nprule_out(r1+1,:)=[0,faix,0]; else nprule_out(r1+1,:)=[0,0,0]; end if r1==0 dnp_out=0; end for j=1:r1 dnp_out(r1+1,j)=GetDistance(np_out(j,:),x,funcname,params); dnp_out(j,r1+1)=dnp_out(r1+1,j); end if r1>=Nnp %未达到非劣解种群极限 %---------移除密集距离最小的一个------- densedis = GetDenseDis(dnp_out); n_min = find(min(densedis)==densedis);%找出密度距离最小的一个 tempIndex = randi([1,length(n_min)],1); np_out(n_min(tempIndex),:)=[];%非劣解剔除该解 nprule_out(n_min(tempIndex),:)=[]; dnp_out(n_min(tempIndex),:)=[]; if ~isempty(dnp_out) dnp_out(:,n_min(tempIndex))=[]; end end end %%%%%%%%%%%%%%-----------将粒子维护到----------%%%%%%%%%%%%%%%%% function dis=GetDistance(x,y,funcname,params) %求两向量之间的距离 if(params.istargetdis) gx=fitness(x,funcname); gy=fitness(y,funcname); gxy=(gx-gy).^2; dis=sqrt(sum(gxy(:))); else g=x-y; dis=sum(sum(g.^2)); end end function densedis = GetDenseDis(dnp) %密集距离 [r,c] = size(dnp); densedis=zeros(1,r); for i=1:r firstmin=Inf; %secondmin=Inf; for j=1:c if dnp(i,j)~=0 && dnp(i,j) % if dnp(i,j)~=0 && dnp(i,j)~=firstmin && dnp(i,j) % densedis(i)=(firstmin+secondmin)/2; densedis(i)=firstmin; end end 外部种群