麻雀优化算法,附MATLAB代码,评价及其改进思路,直接复制

麻雀优化算法(Sparrow Search Algorithm, SSA)是2020年提出的智能优化算法,SSA算法受到庥雀觅食行为和反捕食行为的启发而提出,属于智能仿生算法中粒子群优化算法(PSO)的其中一种,用于模拟群体智能所产生的一种进化计算技术(Evolutionary Computation)。相较于其他智能优化算法,麻雀搜索算法是一种高效、灵活、内存占用低、易于实现的搜索算法,适用于大规模搜索问题。

麻雀搜索算法的基本思想是将搜索空间分成若干个子空间,然后对每个子空间进行搜索,直到找到目标或搜索空间为空为止。仿生算法是一种基于生物进化思想的算法,如粒子群算法PS0通过模拟生物进化过程,逐步优化解法,最终找到最优解。麻雀搜索算法更加注重局部最优解,在一定程度上克服了PS0算法的局限性。

关于麻雀算法的原理本篇文章就不再细讲,相关文献有很多,采用麻雀算法在CEC2005函数上进行测试,并将麻雀算法(SSA)与灰狼算法(GWO),粒子群(PSO)算法进行对比,结果如下:

d83d922719bd6858d831f6653e7937bb.png

789a2152f2d58f5ed7da16c6e0de62e7.png

351c52784ec0fe45feace136b8150baf.png

fba4102b9ba1f8298b7854040c56f658.png

5ce6c65b331b32d6d958ab60f8d38402.png

67b5d0546b4fef23605151849ecd44ec.png

89b81306374025be5f832834429d932b.png

541adba5215bdccc206e8c57578f4fdd.png

7efda9686da46a69ac92c1923185ef72.png

55e84ee1384dcea4244a0a6bc67ad818.png

评价:对前10个函数的测试中,可以看到麻雀算法无论是在寻优精度还是收敛速度上都优于GWO和PSO,不得不说,该算法的性能确实不错。如果在这个算法上再做一些改进,会得到更好的效果。

改进思路浅谈:以F1,F2,F3函数的结果为例进行观察,SSA算法在前期的收敛速度并不快,而到了中期的收敛速度却很快,此时大家可以考虑的改进方法可以在前期添加一些混沌映射,使粒子的多样性增加。而在后期的寻优速度再次下降,此时可以考虑引入莱维飞行或黄金正弦算法进行改进。

有关融合混沌映射、莱维飞行、黄金正弦的案例在之前的文章中也有提及,大家可以相互对比着来进行改进。文章链接如下:

融合黄金正弦,十种混沌映射,搞定!把把最优值,本文思路可用于所有智能算法的改进

基于改进莱维飞行和混沌映射(10种混沌映射随意切换)的粒子群优化算法,附matlab

接下来直接上SSA代码,大家直接复制即可:

function [fMin , bestX,Convergence_curve ] = SSA(pop, M,c,d,dim,fobj  )
P_percent = 0.2;    % The population size of producers accounts for "P_percent" percent of the total population size
pNum = round( pop *  P_percent );    % The population size of the producers
lb= c.*ones( 1,dim );    % Lower limit/bounds/     a vector
ub= d.*ones( 1,dim );    % Upper limit/bounds/     a vector
%Initialization
for i = 1 : pop
    x( i, : ) = lb + (ub - lb) .* rand( 1, dim );
    fit( i ) = fobj( x( i, : ) ) ;
end
pFit = fit;
pX = x;                            % The individual's best position corresponding to the pFit
[ fMin, bestI ] = min( fit );      % fMin denotes the global optimum fitness value
bestX = x( bestI, : );             % bestX denotes the global optimum position corresponding to fMin
% Start updating the solutions.
for t = 1 : M
    [ ans, sortIndex ] = sort( pFit );% Sort.
    [fmax,B]=max( pFit );
    worse= x(B,:);
    r2=rand(1);
    if(r2<0.8)
        for i = 1 : pNum                                                   % Equation (3)
            r1=rand(1);
            x( sortIndex( i ), : ) = pX( sortIndex( i ), : )*exp(-(i)/(r1*M));
            x( sortIndex( i ), : ) = Bounds( x( sortIndex( i ), : ), lb, ub );
            fit( sortIndex( i ) ) = fobj( x( sortIndex( i ), : ) );
        end
    else
        for i = 1 : pNum


            x( sortIndex( i ), : ) = pX( sortIndex( i ), : )+randn(1)*ones(1,dim);
            x( sortIndex( i ), : ) = Bounds( x( sortIndex( i ), : ), lb, ub );
            fit( sortIndex( i ) ) = fobj( x( sortIndex( i ), : ) );


        end
    end
    [ fMMin, bestII ] = min( fit );
    bestXX = x( bestII, : );
    for i = ( pNum + 1 ) : pop                     % Equation (4)
        A=floor(rand(1,dim)*2)*2-1;
        if( i>(pop/2))
            x( sortIndex(i ), : )=randn(1)*exp((worse-pX( sortIndex( i ), : ))/(i)^2);
        else
            x( sortIndex( i ), : )=bestXX+(abs(( pX( sortIndex( i ), : )-bestXX)))*(A'*(A*A')^(-1))*ones(1,dim);
        end
        x( sortIndex( i ), : ) = Bounds( x( sortIndex( i ), : ), lb, ub );
        fit( sortIndex( i ) ) = fobj( x( sortIndex( i ), : ) );
    end
    c=randperm(numel(sortIndex));
    b=sortIndex(c(1:20));
    for j =  1  : length(b)      % Equation (5)
        if( pFit( sortIndex( b(j) ) )>(fMin) )
            x( sortIndex( b(j) ), : )=bestX+(randn(1,dim)).*(abs(( pX( sortIndex( b(j) ), : ) -bestX)));
        else
            x( sortIndex( b(j) ), : ) =pX( sortIndex( b(j) ), : )+(2*rand(1)-1)*(abs(pX( sortIndex( b(j) ), : )-worse))/ ( pFit( sortIndex( b(j) ) )-fmax+1e-50);
        end
        x( sortIndex(b(j) ), : ) = Bounds( x( sortIndex(b(j) ), : ), lb, ub );
        fit( sortIndex( b(j) ) ) = fobj( x( sortIndex( b(j) ), : ) );
    end
    for i = 1 : pop
        if ( fit( i ) < pFit( i ) )
            pFit( i ) = fit( i );
            pX( i, : ) = x( i, : );
        end
        if( pFit( i ) < fMin )
            fMin= pFit( i );
            bestX = pX( i, : );
        end
    end
    Convergence_curve(t)=fMin;
end
% Application of simple limits/bounds
function s = Bounds( s, Lb, Ub)
% Apply the lower bound vector
temp = s;
I = temp < Lb;
temp(I) = Lb(I);
% Apply the upper bound vector
J = temp > Ub;
temp(J) = Ub(J);
% Update this new move
s = temp;
%---------------------------------------------------------------------------------------------------------------------------

需要的同学直接粘贴用于自己的实际问题即可。后续会对麻雀算法进行改进,敬请关注。

下方卡片回复关键词,获取完整代码,关键词:TGDM1209

猜你喜欢

转载自blog.csdn.net/woaipythonmeme/article/details/131630276
今日推荐