⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smpso_f10.m

📁 改进型pso ,可以在matlab7.0环境下运行,其他版本的还未运行过,不能保证
💻 M
📖 第 1 页 / 共 2 页
字号:
            x(:,1)=x1;         x(:,2)=x2;         x(:,3)=x3;
            [OderF,IndexF]=sort(fx);
            gbestfittemp=OderF(3);
            if gbestfittemp>=gbestfit
                gbestfit=gbestfittemp;
                gbestpos=x(:,IndexF(3));
            end
            clear OderF;    clear IndexF;    clear fx;    clear x;    clear gbestfittemp;
            clear x1;    clear x2;    clear x3;    clear x4;    clear fx1;    clear fx2;    
            clear fx3;    clear fx4;
        end
    end
 
    smPSOgbestfit(iii)=gbestfit;            %  需要保留

    %    爬山微粒群算法求解结束
    %**********************************************************************
    clear gbestpos;    clear gbestfit;    clear ibestfit;    clear ibestpos;
    clear bestpart;    clear g;    clear popul;    clear vel;    clear fit;
    
    
    % *************************************************************************
    %              微粒群优化算法求解----基本PSO
    % *************************************************************************
    %  初始参数  
    PopSize0=PopSize;                           %  微粒个数

    puopul(1,:)=rand(1,PopSize0)*(12.1+3.0)-3.0;        % 位置初始化
    puopul(2,:)=rand(1,PopSize0)*(5.8-4.1)+4.1;         % 位置初始化
    vell=rand(dim,PopSize0);                            % 速度初始化

    for i=1:PopSize0,              %   计算适应值
        fitt(i)=21.5+puopul(1,i)*sin(4.0*pi*puopul(1,i))+puopul(2,i)*sin(20.0*pi*puopul(2,i));
    end

    %  将各微粒的位置设置为当前各微粒最好的位置
    ibestpos=puopul;               %  个体最好位置初始化
    ibestfit=fitt;                 %  各个体的适应值

    % 找出全局最好的初始微粒
    [bestpart,g]=max(fitt);        %  找全局最好的适应值
    gbestfit=bestpart;             %  全局最好的适应值
    gbestpos=puopul(:,g);          %  全局最好的适应值对应的个体

    %  迭代开始/主程序开始运行
    iter=0;
    while (iter<MaxIt),            %  迭代开始
        iter=iter+1;
        w=maxw-iter*(maxw-minw)/MaxIt;        % 当前步所用的惯性权值

        for i=1:PopSize0,                     % 将全局最好的适应值对应的个体展开(若不展开来,没法相减)
            A(:,i)=gbestpos;
        end
        R1=rand(dim,PopSize0);      R2=rand(dim,PopSize0);
        vell=0.8*(w*vell+c1*R1.*(ibestpos-puopul)+c2*R2.*(A-puopul));        %  速度迭代计算
        puopul=puopul+vell;                                                   %  位置迭代计算
        clear A;    clear R1;     clear R2;
        
        %  位置限幅处理
        for i=1:PopSize0    
            if puopul(1,i)>12.1,                  %    限幅处理
                puopul(1,i)=12.1;
            end
            if puopul(1,i)<-3.0,
                puopul(1,i)=-3.0;
            end
    
            if puopul(2,i)>5.8
                puopul(2,i)=5.8;
            end    
            if puopul(2,i)<4.1
                puopul(2,i)=4.1;
            end
        end
    
        %  各微粒的适应值计算
        for i=1:PopSize0,                                     %  适应值计算
            fitt(i)=21.5+puopul(1,i)*sin(4.0*pi*puopul(1,i))+puopul(2,i)*sin(20.0*pi*puopul(2,i));
        end
    
        %  更新个体历史最好位置
        for i=1:PopSize0,                     
            if fitt(i)>ibestfit(i),
                ibestfit(i)=fitt(i);
                ibestpos(:,i)=puopul(:,i);
            end
        end
    
        %  更新全局历史最好位置
        [bestpart,g]=max(fitt);           
        if bestpart>gbestfit;
            gbestfit=bestpart;
            gbestpos=puopul(:,g);
        end
    end

    PSOgbestfit(iii)=gbestfit;          %  输出全局历史最好适应值(最优f1值)

    %  基本PSO求解结束
    %**************************************************************************
    clear gbestpos;    clear gbestfit;    clear ibestfit;    clear ibestpos;   clear i;
    clear bestpart;    clear g;       clear puopul;     clear vell;    clear fitt;  

    
    %************************************************************************
    %           单纯形搜索----基本sm     
    %**********************************************************************
    x(1,:)=rand(1,3)*(12.1+3.0)-3.0;         %  变量位置初始化
    x(2,:)=rand(1,3)*(5.8-4.1)+4.1;          
    x1=x(:,1);                               %  选单纯形的顶点
    x2=x(:,2);
    x3=x(:,3);
    gbestfit=-100;                 %  附初始最好值
    
    for tempii=1:MaxIt                       %  进行单纯形法搜索
        %    按最优次序x1,x2,x3排列
        fx1=21.5+x1(1)*sin(4.0*pi*x1(1))+x1(2)*sin(20.0*pi*x1(2));
        fx2=21.5+x2(1)*sin(4.0*pi*x2(1))+x2(2)*sin(20.0*pi*x2(2));
        fx3=21.5+x3(1)*sin(4.0*pi*x3(1))+x3(2)*sin(20.0*pi*x3(2));
        fx(1)=fx1;           fx(2)=fx2;           fx(3)=fx3;
        x(:,1)=x1;           x(:,2)=x2;           x(:,3)=x3;
        [OrderF,IndexF]=sort(fx);
        x1=x(:,IndexF(3));  x2=x(:,IndexF(2));  x3=x(:,IndexF(1));  
        fx1=OrderF(3);      fx2=OrderF(2);      fx3=OrderF(1); 
        clear OrderF;       clear IndexF;       clear fx;  
        
        error=sqrt((x1(1)-x2(1))^2+(x1(2)-x2(2))^2);
        if error<0.000001
            x2(1)=rand(1,1)*(12.1+3.0)-3.0;          %  位置初始化
            x2(2)=rand(1,1)*(5.8-4.1)+4.1;
        end
        error=sqrt((x1(1)-x3(1))^2+(x1(2)-x3(2))^2);
        if error<0.000001
            x3(1)=rand(1,1)*(12.1+3.0)-3.0;          %  位置初始化
            x3(2)=rand(1,1)*(5.8-4.1)+4.1;
        end
        error=sqrt((x2(1)-x3(1))^2+(x2(2)-x3(2))^2);
        if error<0.000001
            x3(1)=rand(1,1)*(12.1+3.0)-3.0;          %  位置初始化
            x3(2)=rand(1,1)*(5.8-4.1)+4.1;
        end
        clear error;       
        
        %    按最优次序x1,x2,x3再次排列
        fx1=21.5+x1(1)*sin(4.0*pi*x1(1))+x1(2)*sin(20.0*pi*x1(2));
        fx2=21.5+x2(1)*sin(4.0*pi*x2(1))+x2(2)*sin(20.0*pi*x2(2));
        fx3=21.5+x3(1)*sin(4.0*pi*x3(1))+x3(2)*sin(20.0*pi*x3(2));
        fx(1)=fx1;           fx(2)=fx2;           fx(3)=fx3;
        x(:,1)=x1;           x(:,2)=x2;           x(:,3)=x3;
        [OrderF,IndexF]=sort(fx);
        x1=x(:,IndexF(3));  x2=x(:,IndexF(2));  x3=x(:,IndexF(1));  
        fx1=OrderF(3);      fx2=OrderF(2);      fx3=OrderF(1); 
        clear OrderF;       clear IndexF;       clear fx;  
        
        %   反射
        x4=x1+x2-x3;                     %    反射
        if x4(1)>12.1,        
            x4(1)=12.1;
        end
        if x4(1)<-3.0,
            x4(1)=-3.0;
        end
        if x4(2)>5.8
            x4(2)=5.8;
        end    
        if x4(2)<4.1
            x4(2)=4.1;
        end
        fx4=21.5+x4(1)*sin(4.0*pi*x4(1))+x4(2)*sin(20.0*pi*x4(2));

        %  扩张
        if fx4>fx1                       %    扩张
            x5=(x1+x2)/2+1.5*(x4-(x1+x2)/2);  
            if x5(1)>12.1,        
                x5(1)=12.1;
            end
            if x5(1)<-3.0,
                x5(1)=-3.0;
            end
            if x5(2)>5.8
                x5(2)=5.8;
            end    
            if x5(2)<4.1
                x5(2)=4.1;
            end
            fx5=21.5+x5(1)*sin(4.0*pi*x5(1))+x5(2)*sin(20.0*pi*x5(2));
            x3=x2;    x2=x1;
            if fx5>=fx4
                x1=x5;
            else
                x1=x4;
            end
        end
        clear x5;    clear fx5;
                
        %    替代操作
        if fx4<=fx1                      %    不压缩也不扩张
            if fx4>=fx2
                 x3=x2;     x2=x4;     x1=x1;   
            end
        end
                
        %    压缩与收缩
        if fx4<fx2                       %    压缩
            if fx4>fx3
                xa=x4;    fxa=fx4;
            else
                xa=x3;    fxa=fx3;
            end
            x6=(x1+x2)/2+0.7*(xa-(x1+x2)/2);
            if x6(1)>12.1,        
                x6(1)=12.1;
            end
            if x6(1)<-3.0,
                x6(1)=-3.0;
            end
            if x6(2)>5.8
                x6(2)=5.8;
            end    
            if x6(2)<4.1
                x6(2)=4.1;
            end
            fx6=21.5+x6(1)*sin(4.0*pi*x6(1))+x6(2)*sin(20.0*pi*x6(2));
            if fx6>=fxa
                x3=x6;      x2=x2;    x1=x1;        %    压缩
            else
                x7=x2+(x1-x2)/2;                    %    收缩
                x8=x3+(x1-x3)/2;
                x1=x1;    x2=x7;      x3=x8;
            end
        end
        clear xa;    clear fxa;    clear x6;    clear fx6;    clear x7;    clear x8;
                
        %    本次搜索的最佳值
        fx0(1)=21.5+x1(1)*sin(4.0*pi*x1(1))+x1(2)*sin(20.0*pi*x1(2));
        fx0(2)=21.5+x2(1)*sin(4.0*pi*x2(1))+x2(2)*sin(20.0*pi*x2(2));
        fx0(3)=21.5+x3(1)*sin(4.0*pi*x3(1))+x3(2)*sin(20.0*pi*x3(2));
        [smbestpart,smg]=max(fx0);
        if smbestpart>gbestfit
            gbestfit=smbestpart;
        end
        clear smg;
    end
    smgbestfit(iii)=gbestfit;
    clear x1;    clear x2;    clear x3;    clear fx0;     clear tempii;    clear gbestfit;
end
    
    %  显示过程轨迹
    figure;
    plot(1:iii,smPSOgbestfit(1:iii),'-r',1:iii,PSOgbestfit(1:iii),'-.b',1:iii,smgbestfit(1:iii),'--g')
    xlabel('迭代次数');                                %  坐标标注
    ylabel('每一步的最佳适应值');
    title('测试函数为f1的优化结果图:');
    legend('smPSO','PSO','SM');

    sortpso=sort(PSOgbestfit);
    sortsmpso=sort(smPSOgbestfit);
    sortsm=sort(smgbestfit);
    meansmpso=mean(smPSOgbestfit);
    meanpso=mean(PSOgbestfit);
    meansm=mean(smgbestfit);
    
 %   save smPSO_F1;
    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -