• 融合柯西变异和自适应莱维飞行的布谷鸟优化算法,改进布谷鸟,MATLAB代码


    经常有小伙伴后台留言问:

    作者改进的算法可不可以用来写论文呀?

    回答是:当然可以!且不用加引用!

    如果我的文章能帮助到大家写论文,那是作者的荣幸呀!

    布谷鸟优化算法是一个非常经典的优化算法,直到今天还有不少人研究对其改进。今天为大家带来一期由小淘自行改进的布谷鸟优化算法---融合柯西变异和自适应莱维飞行的布谷鸟优化算法(Cauchy-Adaptive-Levy-Cuckoo search,CALCS),与往期改进算法相同,该算法不会增加原始算法的复杂度。运行时间不会像某些加入了反向学习、贪婪策略等改进算法一样陡然上升。

    原理详解

    基本布谷鸟算法原理介绍:

    基本布谷鸟算法(Cuckoo Search algorithm,CS)是通过模拟布谷鸟寻窝产卵的行为来求解最优解,其求解过程为:

    1)设置种群数目、弃巢率、问题边界及最大迭代次数。

    2)在问题领域内随机产生一定数目的种群并计算每一个个体的目标函数值,求出当前最优值和最优解。

    3)判断迭代次数是否达到最大迭代次数,如果是则退出循环,输出最优值和最优解;否则进入步骤4)。

    4)根据下式更新鸟巢。

    cac3d4df0dc0556d57f2e7f19d27489a.png

    式中: nest(t+1)i ,nestti分别表示鸟巢i 在第t+1和t 代的位置; α=0.01R,R∈N(0,1) ; best表示当前鸟巢中最佳位置; step表示Levy飞行产生的步长。

    bf7b323937e31764418a894676608b50.png

    式中: β 取值为1.5; ν,μ∈N(0,1) ; φ 按下式计算:

    16b6f3fc0ec678c0a9c8dbf7e781c591.png

    5) 计算新鸟巢的目标函数值,若较之前的优越则替换函数值和相应的鸟巢,并记录最优解。

    6) 随机生成[0,1]之间的数并与弃巢率比较,若小于则保留该鸟巢,否则按下式产生新鸟巢。

    cbacc4d485c6f90040f445207c617e06.png

    式中: a、c 为第t 次迭代中不重复的随机整数; r∈[0,1]的随机数。

    7) 重新计算新鸟巢的目标函数值,若较之前的优越则替换函数值和相应的鸟巢,并记录最优解。

    8) 判断函数值与最优值,如果小于则替换最优值与最优解; 算法转入步骤3) 进行下一次迭代。

    改进布谷鸟算法原理介绍:

    改进点1:在基本布谷鸟算法基础上采用Tent混沌映射,初始化种群的多样性。

    改进点2:采用自适应步长的Levy飞行来改进更新鸟巢的公式。

    针对标准布谷鸟搜索算法收敛速度慢、寻优精度低的缺点,通过自适应莱维飞行机制进行全局搜索:莱维飞行步长随迭代的进行不断减小。改进的算法在寻优初期拥有较大的步长因子,从而扩大算法前期的搜索空间,提高全局搜索能力;在寻优过程中,步长减小,提高算法局部搜索性能。由标准算法的0.01改为下式:α=0.5*exp(-t/tmax)。当然关于此类的公式,网上还有很多,大家如果不想用这个公式还可以自行改进。

    改进点3:在随机游走之前,先采用柯西变异来更新鸟巢位置。柯西分布与标准的正态分布相似,为连续的概率分布,在原点处值较小,两端较为扁长,逼近零速率较慢, 因而相比于之前的随机游走策略能产生更大的扰动。因此,利用柯西变异对鸟巢位置进行扰动,从而扩大布谷鸟算法的搜索规模,进而提升算法跳出局部最优能力。

    结果展示

    在CEC2005函数集上进行测试,结果如下:其中CALCS为本文所提改进布谷鸟算法,CS是原始的布谷鸟优化算法,GWO是灰狼优化算法,PSO是粒子群优化算法。

    算法迭代1000次,每种算法的粒子数设置为30。

    c7c081addd3da968d6a5d5f3d7ffc976.png

    bd27e2548c967452ef19551f30506b9a.png

    e64d51663aec42c0d200459c5dac63f8.png

    d79f2ca5f60d01fcdb5e6b51f4b46ff0.png

    fc98862a689bab5029dce03b5740d447.png

    8e184861c5ab58422e4c9c1b957d9375.png

    e4cece6302a76a9e1c553bc93d44ee1b.png

    ec2bbf28f6c43b3281cbdc052140201d.png

    302d593344a8247a666174eddfae031c.png

    7fa6e95ad4dbd8f421bc514bc01f0016.png

    结果分析:在单峰值函数与多峰值函数的测试中可以看到,融合柯西变异和自适应莱维飞行的布谷鸟优化算法寻优效果确实不错!

    代码展示

    1. %%
    2. clear
    3. clc
    4. close all
    5. number='F1'; %选定优化函数,自行替换:F1~F23
    6. [lower_bound,upper_bound,variables_no,fobj]=CEC2005(number); % [lb,ub,D,y]:下界、上界、维度、目标函数表达式
    7. pop_size=30; % population members
    8. max_iter=1000; % maximum number of iteration
    9. %% GWO
    10. [fMin , bestX,GWO_convergence_curve ] =GWO(pop_size,max_iter,lower_bound,upper_bound,variables_no,fobj);
    11. display(['The best optimal value of the objective funciton found by GWO for ' [num2str(number)],' is : ', num2str(fMin)]);
    12. fprintf ('Best solution obtained by GWO: %s\n', num2str(bestX,'%e '));
    13. %% PSO
    14. [Best_pos,Best_score, PSO_convergence_curve ] = PSO(pop_size,max_iter,lower_bound,upper_bound,variables_no,fobj); % Call PSO
    15. fprintf ('Best solution obtained by PSO: %s\n', num2str(Best_score,'%e '));
    16. display(['The best optimal value of the objective funciton found by PSO for ' [num2str(number)],' is : ', num2str(Best_pos)]);
    17. %% CS
    18. [CS_Score,CSbestx,CS_convergence_curve]=CS(pop_size,max_iter,lower_bound,upper_bound,variables_no,fobj);
    19. display(['The best optimal value of the objective funciton found by CS for ' [num2str(number)],' is : ', num2str(CSbestx)]);
    20. fprintf ('Best solution obtained by CS: %s\n', num2str(CS_Score,'%e '));
    21. %% CALCS
    22. [CALCS_Score,CALCSbestx,CALCS_convergence_curve]=CALCS(pop_size,max_iter,lower_bound,upper_bound,variables_no,fobj);
    23. display(['The best optimal value of the objective funciton found by CALCS for ' [num2str(number)],' is : ', num2str(CALCSbestx)]);
    24. fprintf ('Best solution obtained by CALCS: %s\n', num2str(CALCS_Score,'%e '));
    25. %% Figure
    26. figure1 = figure('Color',[1 1 1]);
    27. G1=subplot(1,2,1,'Parent',figure1);
    28. func_plot(number)
    29. title(number)
    30. xlabel('x')
    31. ylabel('y')
    32. zlabel('z')
    33. subplot(1,2,2)
    34. G2=subplot(1,2,2,'Parent',figure1);
    35. CNT=20;
    36. k=round(linspace(1,max_iter,CNT)); %随机选CNT个点
    37. % 注意:如果收敛曲线画出来的点很少,随机点很稀疏,说明点取少了,这时应增加取点的数量,100200300等,逐渐增加
    38. % 相反,如果收敛曲线上的随机点非常密集,说明点取多了,此时要减少取点数量
    39. iter=1:1:max_iter;
    40. if ~strcmp(number,'F16')&&~strcmp(number,'F9')&&~strcmp(number,'F11') %这里是因为这几个函数收敛太快,不适用于semilogy,直接plot
    41. semilogy(iter(k),CS_convergence_curve(k),'m-^','linewidth',1);
    42. hold on
    43. semilogy(iter(k),GWO_convergence_curve(k),'b-*','linewidth',1);
    44. hold on
    45. semilogy(iter(k),PSO_convergence_curve(k),'y-o','linewidth',1);
    46. hold on
    47. semilogy(iter(k),CALCS_convergence_curve(k),'g-p','linewidth',1);
    48. else
    49. plot(iter(k),CS_convergence_curve(k),'m-^','linewidth',1);
    50. hold on
    51. plot(iter(k),GWO_convergence_curve(k),'b-*','linewidth',1);
    52. hold on
    53. plot(iter(k),PSO_convergence_curve(k),'r-o','linewidth',1);
    54. hold on
    55. plot(iter(k),CALCS_convergence_curve(k),'g-p','linewidth',1);
    56. end
    57. grid on;
    58. title('收敛曲线')
    59. xlabel('迭代次数');
    60. ylabel('适应度值');
    61. box on
    62. legend('CS','GWO','PSO','CALCS')
    63. set (gcf,'position', [300,300,800,330]

    基本布谷鸟算法:

    1. function [bestnest,fmin,lhy]=CS(n,N_IterTotal,lb,ub,nd,fobj)
    2. pa=0.25;              % Discovery rate of alien eggs/solutions
    3. %% Simple bounds of the search domain
    4. Lb=lb.*ones(1,nd); % Lower bounds
    5. Ub=ub.*ones(1,nd); % Upper bounds
    6. % Random initial solutions
    7. nest=initialization(n,nd,Ub,Lb);
    8. % Get the current best of the initial population
    9. fitness=10^10*ones(n,1);
    10. [fmin,bestnest,nest,fitness]=get_best_nest(nest,nest,fitness,fobj);
    11. %% Starting iterations
    12. for iter=1:N_IterTotal
    13. % Generate new solutions (but keep the current best)
    14. new_nest=get_cuckoos(nest,bestnest,Lb,Ub);
    15. [fnew,best,nest,fitness]=get_best_nest(nest,new_nest,fitness,fobj);
    16. % Discovery and randomization
    17. new_nest=empty_nests(nest,Lb,Ub,pa) ;
    18. % Evaluate this set of solutions
    19. [fnew,best,nest,fitness]=get_best_nest(nest,new_nest,fitness,fobj);
    20. % Find the best objective so far
    21. if fnew
    22. fmin=fnew;
    23. bestnest=best;
    24. end
    25. lhy(iter) = fmin;
    26. end %% End of iterations
    27. %% --------------- All subfunctions are list below ------------------
    28. %% Get cuckoos by ramdom walk
    29. function nest=get_cuckoos(nest,best,Lb,Ub)
    30. % Levy flights
    31. n=size(nest,1);
    32. % For details about Levy flights, please read Chapter 3 of the book:
    33. % X. S. Yang, Nature-Inspired Optimization Algorithms, Elesevier, (2014).
    34. beta=3/2;
    35. sigma=(gamma(1+beta)*sin(pi*beta/2)/(gamma((1+beta)/2)*beta*2^((beta-1)/2)))^(1/beta);
    36. for j=1:n
    37. s=nest(j,:);
    38. % This is a simple way of implementing Levy flights
    39. % For standard random walks, use step=1;
    40. %% Levy flights by Mantegna's algorithm
    41. u=randn(size(s))*sigma;
    42. v=randn(size(s));
    43. step=u./abs(v).^(1/beta);
    44. % In the next equation, the difference factor (s-best) means that
    45. % when the solution is the best solution, it remains unchanged.
    46. stepsize=0.01*step.*(s-best);
    47. % Here the factor 0.01 comes from the fact that L/100 should be the
    48. % typical step size for walks/flights where L is the problem scale;
    49. % otherwise, Levy flights may become too aggresive/efficient,
    50. % which makes new solutions (even) jump out side of the design domain
    51. % (and thus wasting evaluations).
    52. % Now the actual random walks or flights
    53. s=s+stepsize.*randn(size(s));
    54. % Apply simple bounds/limits
    55. nest(j,:)=simplebounds(s,Lb,Ub);
    56. end
    57. %% Find the current best solution/nest among the population
    58. function [fmin,best,nest,fitness]=get_best_nest(nest,newnest,fitness,fobj)
    59. % Evaluating all new solutions
    60. for j=1:size(nest,1)
    61. fnew=fobj(newnest(j,:));
    62. if fnew<=fitness(j)
    63. fitness(j)=fnew;
    64. nest(j,:)=newnest(j,:);
    65. end
    66. end
    67. % Find the current best
    68. [fmin,K]=min(fitness) ;
    69. best=nest(K,:);
    70. %% Replace some not-so-good nests by constructing new solutions/nests
    71. function new_nest=empty_nests(nest,Lb,Ub,pa)
    72. % A fraction of worse nests are discovered with a probability pa
    73. n=size(nest,1);
    74. % Discovered or not -- a status vector
    75. K=rand(size(nest))>pa;
    76. % Notes: In the real world, if a cuckoo's egg is very similar to
    77. % a host's eggs, then this cuckoo's egg is less likely to be discovered.
    78. % so the fitness should be related to the difference in solutions.
    79. % Therefore, it is a good idea to do a random walk in a biased way
    80. % with some random step sizes.
    81. %% New solution by biased/selective random walks
    82. stepsize=rand*(nest(randperm(n),:)-nest(randperm(n),:));
    83. new_nest=nest+stepsize.*K;
    84. for j=1:size(new_nest,1)
    85. s=new_nest(j,:);
    86. new_nest(j,:)=simplebounds(s,Lb,Ub);
    87. end
    88. % Application of simple bounds/constraints
    89. function s=simplebounds(s,Lb,Ub)
    90. % Apply the lower bound
    91. ns_tmp=s;
    92. I=ns_tmp
    93. ns_tmp(I)=Lb(I);
    94. % Apply the upper bounds
    95. J=ns_tmp>Ub;
    96. ns_tmp(J)=Ub(J);
    97. % Update this new move
    98. s=ns_tmp;
    99. %% You can replace the following objective function
    100. %% by your own functions (also update the Lb and Ub)

    点击下方卡片获取更多代码!

  • 相关阅读:
    IDEA日常使用
    第1章 打造深度学习工具箱
    vue3中使用better-scroll
    自动驾驶多任务框架Hybridnets——同时处理车辆检测、可驾驶区域分割、车道线分割模型部署(C++/Python)
    apisix~集成服务发现注册中心
    【java零基础入门到就业】第四天:Notepad++软件的下载和安装
    SpringBoot保姆级教程(五)SpringBoot注册web组件
    SpringBoot3 Actuator使用如何以及自定义端点
    入职字节外包一个月,我离职了
    DOSBox解决CPU从内存单元中要读取数据
  • 原文地址:https://blog.csdn.net/woaipythonmeme/article/details/132893596