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

📄 smpso_f2.m

📁 改进型pso ,可以在matlab7.0环境下运行,其他版本的还未运行过,不能保证
💻 M
📖 第 1 页 / 共 2 页
字号:

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

    puopul=rand(2,PopSize0)*200.0-100.0;        %  位置初始化
    vell=rand(dim,PopSize0);                    %  速度初始化

    for i=1:PopSize0,              %   计算适应值
        fitt(i)=(sin(sqrt((puopul(1,i)-50.0)^2+(puopul(2,i)-50.0)^2)+exp(1)))/(sqrt((puopul(1,i)-50.0)^2+(puopul(2,i)-50.0)^2)+exp(1))+1.0;
    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   
            for j=1:dim
                if puopul(j,i)>100.0,                  %    限幅处理
                    puopul(j,i)=100.0;
                end
                if puopul(j,i)<-100.0,
                    puopul(j,i)=-100.0;
                end
            end
        end
    
        %  各微粒的适应值计算
        for i=1:PopSize0,                                     %  适应值计算
            fitt(i)=(sin(sqrt((puopul(1,i)-50.0)^2+(puopul(2,i)-50.0)^2)+exp(1)))/(sqrt((puopul(1,i)-50.0)^2+(puopul(2,i)-50.0)^2)+exp(1))+1.0;
        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=rand(2,3)*200.0-100.0;         %  位置初始化
    x1=x(:,1);                       %  选单纯形的顶点
    x2=x(:,2);
    x3=x(:,3);
    gbestfit=-600;                   %  附初始最好值
    
    for tempii=1:MaxIt                       %  进行单纯形法搜索
        %    按最优次序x1,x2,x3排列
        fx1=(sin(sqrt((x1(1)-50.0)^2+(x1(2)-50.0)^2)+exp(1)))/(sqrt((x1(1)-50.0)^2+(x1(2)-50.0)^2)+exp(1))+1.0;
        fx2=(sin(sqrt((x2(1)-50.0)^2+(x2(2)-50.0)^2)+exp(1)))/(sqrt((x2(1)-50.0)^2+(x2(2)-50.0)^2)+exp(1))+1.0;
        fx3=(sin(sqrt((x3(1)-50.0)^2+(x3(2)-50.0)^2)+exp(1)))/(sqrt((x3(1)-50.0)^2+(x3(2)-50.0)^2)+exp(1))+1.0;
        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=rand(2,1)*200-100.0;          %  位置初始化
        end
        error=sqrt((x1(1)-x3(1))^2+(x1(2)-x3(2))^2);
        if error<0.000001
            x3=rand(2,1)*200-100.0;          %  位置初始化
        end
        error=sqrt((x2(1)-x3(1))^2+(x2(2)-x3(2))^2);
        if error<0.000001
            x3=rand(2,1)*200-100.0;          %  位置初始化
        end
        clear error;       
        
        %    按最优次序x1,x2,x3再次排列
        fx1=(sin(sqrt((x1(1)-50.0)^2+(x1(2)-50.0)^2)+exp(1)))/(sqrt((x1(1)-50.0)^2+(x1(2)-50.0)^2)+exp(1))+1.0;
        fx2=(sin(sqrt((x2(1)-50.0)^2+(x2(2)-50.0)^2)+exp(1)))/(sqrt((x2(1)-50.0)^2+(x2(2)-50.0)^2)+exp(1))+1.0;
        fx3=(sin(sqrt((x3(1)-50.0)^2+(x3(2)-50.0)^2)+exp(1)))/(sqrt((x3(1)-50.0)^2+(x3(2)-50.0)^2)+exp(1))+1.0;
        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;                     %    反射
        for i=1:2
            if x4(i)>100.0,        
                x4(i)=100.0;
            end
            if x4(i)<-100.0,
                x4(i)=-100.0;
            end
        end
        fx4=(sin(sqrt((x4(1)-50.0)^2+(x4(2)-50.0)^2)+exp(1)))/(sqrt((x4(1)-50.0)^2+(x4(2)-50.0)^2)+exp(1))+1.0;

        %  扩张
        if fx4>fx1                       %    扩张
            x5=(x1+x2)/2+1.5*(x4-(x1+x2)/2);  
            for i=1:2
                if x5(i)>100.0,        
                    x5(i)=100.0;
                end
                if x5(i)<-100.0,
                    x5(i)=-100.0;
                end
            end
            fx5=(sin(sqrt((x5(1)-50.0)^2+(x5(2)-50.0)^2)+exp(1)))/(sqrt((x5(1)-50.0)^2+(x5(2)-50.0)^2)+exp(1))+1.0;
            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);
            for i=1:2
                if x6(i)>100.0,        
                    x6(i)=100.0;
                end
                if x6(i)<-100.0,
                    x6(i)=-100.0;
                end
            end
            fx6=(sin(sqrt((x6(1)-50.0)^2+(x6(2)-50.0)^2)+exp(1)))/(sqrt((x6(1)-50.0)^2+(x6(2)-50.0)^2)+exp(1))+1.0;
            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)=(sin(sqrt((x1(1)-50.0)^2+(x1(2)-50.0)^2)+exp(1)))/(sqrt((x1(1)-50.0)^2+(x1(2)-50.0)^2)+exp(1))+1.0;
        fx0(2)=(sin(sqrt((x2(1)-50.0)^2+(x2(2)-50.0)^2)+exp(1)))/(sqrt((x2(1)-50.0)^2+(x2(2)-50.0)^2)+exp(1))+1.0;
        fx0(3)=(sin(sqrt((x3(1)-50.0)^2+(x3(2)-50.0)^2)+exp(1)))/(sqrt((x3(1)-50.0)^2+(x3(2)-50.0)^2)+exp(1))+1.0;
        [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('测试函数为函数2时的优化结果图:');
    legend('SM-PSO','PSO','SM');

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

⌨️ 快捷键说明

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