【优化算法】基于matlab三维粒子群算法【含Matlab源码 902期】

一、简介

基于matlab三维粒子群算法

二、源代码

SR=1000;%搜索范围Searching range
 RNS=1;%群体规模与搜索范围之比The ratio of the number of particles to the searching range
 M=RNS*SR;%群体规模Members
 W=0.5;%惯性权重Inertia weight
 C1=5;%加速常数Accelerated constant
 C2=5;%加速常数Accelerated constant
 RVR=0.02;%最大速度与搜索范围之比The ratio of the maximum velocity to the searching range
 RCS=0.01;%收敛区间与搜索范围之比The ratio of the convergent interval to  the searching range
 CI=RCS*SR;%收敛区间Convergent interval 
 PT=0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001;%暂停时间Pause time
 RNPA=0.5;%正负半轴标度之比The ratio of the negative half axis to the positive half axis
 x=SR*rand(1,M);
 y=SR*rand(1,M);
 z=SR*rand(1,M);
 px=x;
 py=y;
 pz=z;%初始化个体历史最优解数据
 vx=RVR*SR*rand(1,M);
 vxmax=RVR*SR*(zeros(1,M)+1);
 vy=RVR*SR*rand(1,M);
 vymax=RVR*SR*(zeros(1,M)+1);
 vz=RVR*SR*rand(1,M);
 vzmax=RVR*SR*(zeros(1,M)+1);
 ox=SR*rand*(zeros(1,M)+1);
 oy=SR*rand*(zeros(1,M)+1);
 oz=SR*rand*(zeros(1,M)+1);%随机生成谷地位置 以上为初始化过程
 sx=abs(x-ox);
 sy=abs(x-oy);
 sz=abs(x-oz);%评估每个微粒的适应度
 [r,c]=find(sx==min(min(sx)));
 gx=x(r,c)*(zeros(1,M)+1);
 [r,c]=find(sy==min(min(sy)));
 gy=y(r,c)*(zeros(1,M)+1);
 [r,c]=find(sz==min(min(sz)));
 gz=z(r,c)*(zeros(1,M)+1);
while (max(max(sx))>CI)||(max(max(sy))>CI)||(max(max(sz))>CI)%确定结果收敛区间
      ox=0.1*SR*rand*(zeros(1,M)+1);
      oy=0.1*SR*rand*(zeros(1,M)+1);
      oz=0.1*SR*rand*(zeros(1,M)+1);
      plot3(ox,oy,oz,'r*');
      title('三维粒子群算法收敛演示');
      text(ox,oy,oz,'随机目标点');
      xlabel('X'),
      ylabel('Y'),
      zlabel('Z');
      axis([-RNPA*SR SR -RNPA*SR SR -RNPA*SR SR]);
      grid on;
      hold on;
      plot3(x,y,z,'b*');
      hold off;
      pause(PT);
      if max(max(sx))>CI
         vx=W*vx+C1*rand*(px-x)+C2*rand*(gx-x);%根据算法方程给v一个改变量。因为区间长度较小,所以弱化微粒的自身惯性和自我认知能力,强化社会能力以尽快得到最优解
         while  max(max(vx))>RVR*SR||min(min(vx))<-RVR*SR
                if max(max(vx))>RVR*SR
                   [r,c]=find(vx==max(max(vx)));
                   vx(r,c)=RVR*SR;
                end
                if min(min(vx))<-RVR*SR
                   [r,c]=find(vx==min(min(vx)));
                   vx(r,c)=-RVR*SR;
                end%以上为限制v的大小,避免微粒飞过好解或陷入局部优值
         end
         x=x+vx;   
         for i=1:M
             if abs(x(1,i)-ox(1,i))<sx(1,i)
                px(1,i)=x(1,i);
             end
         end
         sx=abs(x-ox);
         [r,c]=find(sx==min(min(sx)));
         gx=x(r,c)*(zeros(1,M)+1);
      end
      if max(max(sy))>CI
         vy=W*vy+C1*rand*(py-y)+C2*rand*(gy-y);
         while  max(max(vy))>RVR*SR||min(min(vy))<-RVR*SR
                if max(max(vy))>RVR*SR
                   [r,c]=find(vy==max(max(vy)));
                   vy(r,c)=RVR*SR;          
                end
                if min(min(vy))<-RVR*SR
                   [r,c]=find(vy==min(min(vy)));
                   vy(r,c)=-RVR*SR;
                end%以上为限制v的大小,避免微粒飞过好解或陷入局部优值
         end
         y=y+vy;
         for i=1:M
             if abs(y(1,i)-oy(1,i))<sy(1,i)
                py(1,i)=y(1,i);
             end
         end
         sy=abs(y-oy);
         [r,c]=find(sy==min(min(sy)));
         gy=y(r,c)*(zeros(1,M)+1);
      end
      if max(max(sz))>CI
         vz=W*vz+C1*rand*(pz-z)+C2*rand*(gz-z);
         while  max(max(vz))>RVR*SR||min(min(vz))<-RVR*SR
                if max(max(vz))>RVR*SR
                   [r,c]=find(vz==max(max(vz)));
                   vz(r,c)=RVR*SR; 
                end
                if min(min(vz))<-RVR*SR
                   [r,c]=find(vz==min(min(vz)));
                   vz(r,c)=-RVR*SR;
                end%以上为限制v的大小,避免微粒飞过好解或陷入局部优值
         end

三、运行结果

【优化算法】基于matlab三维粒子群算法【含Matlab源码 902期】_第1张图片
【优化算法】基于matlab三维粒子群算法【含Matlab源码 902期】_第2张图片

四、备注

版本:2014a

你可能感兴趣的:(matlab,优化求解)