布谷鸟算法详细讲解

2023-05-16

今天我要讲的内容是布谷鸟算法,英文叫做Cuckoo search (CS algorithm)。首先还是同样,介绍一下这个算法的英文含义, Cuckoo是布谷鸟的意思,啥是布谷鸟呢,是一种叫做布谷的鸟,o(∩_∩)o ,这种鸟她妈很懒,自己生蛋自己不养,一般把它的宝宝扔到别的种类鸟的鸟巢去。但是呢,当孵化后,遇到聪明的鸟妈妈,一看就知道不是亲生的,直接就被鸟妈妈给杀了。于是这群布谷鸟宝宝为了保命,它们就模仿别的种类的鸟叫,让智商或者情商极低的鸟妈妈误认为是自己的亲宝宝,这样它就活下来了。 Search指的是搜索,这搜索可不是谷歌一下,你就知道。而是搜索最优值,举个简单的例子,y=(x-0.5)^2+1,它的最小值是1,位置是(0.5,1),我们要搜索的就是这个位置。

现在我们应该清楚它是干嘛的了吧,它就是为了寻找最小值而产生的一种算法,有些好装X的人会说,你傻X啊,最小值不是-2a/b吗,用你找啊? 说的不错,确实是,但是要是我们的函数变成 y=sin(x^3+x^2)+e^cos(x^3)+log(tan(x)+10,你怎么办吶?你解不了,就算你求导数,但是你知道怎么解导数等于0吗?所以我们就得引入先进的东西来求最小值。

为了使大家容易理解,我还是用y=(x-0.5)^2+1来举例子,例如我们有4个布谷鸟蛋(也就是4个x坐标),鸟妈妈发现不是自己的宝宝的概率是0.25,我们x的取值范围是[0,1]之间,于是我们就可以开始计算了。

目标:求x在[0,1]之内的函数y=(x-0.5)^2+1最小值

(1)初始化x的位置,随机生成4个x坐标,x1=0.4,x2=0.6,x3=0.8,x4=0.3 ——> X=[0.4, 0.6 ,0.8, 0.3]

(2)求出y1~y4,把x1~x4带入函数,求得Y=[1,31, 1.46, 1.69, 1.265],并选取当前最小值ymin= y4=1.265

(3)开始定出一个y的最大值为Y_global=INF(无穷大),然后与ymin比较,把Y中最小的位置和值保留,例如Y_global=INF>ymin=1.265,所以令Y_global=1.265

(4)记录Y_global的位置,(0.3,1.265)。

(5)按概率0.25,随机地把X中的值过塞子,选出被发现的蛋。例如第二个蛋被发现x2=0.6,那么他就要随机地变换位子,生成一个随机数,例如0.02,然后把x2=x2+0.02=0.62,之后求出y2=1.4794。那么X就变为了X=[0.4, 0.62 ,0.8, 0.3],Y=[1,31, 1.4794, 1.69, 1.265]。

(6)进行莱维飞行,这名字听起来挺高大上,说白了,就是把X的位置给随机地改变了。怎么变?有一个公式x=x+alpha*L。

L=S*(X-Y_global)*rand3

S=[rand1*sigma/|rand2|]^(1/beta)

sigma=0.6966

beta=1.5

alpha=0.01

rand1~rand3为正态分布的随机数

然后我们把X=[0.4, 0.6 ,0.8, 0.3]中的x1带入公式,首先随机生成rand1=-1.2371,rand2=-2.1935,rand3=-0.3209,接下来带入公式中,获得x1=0.3985

之后同理计算:

x2=0.6172

x3=0.7889 

x4=0.3030

(7)更新矩阵X,X=[0.3985, 0.6172, 0.7889, 0.3030]

(8)计算Y=[1.3092, 1.4766, 1.6751, 1.2661],并选取当前最小值ymin= y4=1.2661,然后与ymin比较,把Y中最小的位置和值保留,例如Y_global=1.265<ymin=1.2661,所以令Y_global=1.265

(9)返回步骤(5)用更新的X去循环执行,经过多次计算即可获得y的最优值和的最值位置(x,y)


最后附上别人写的代码:

% -----------------------------------------------------------------
% Cuckoo Search (CS) algorithm by Xin-She Yang and Suash Deb      %
% Programmed by Xin-She Yang at Cambridge University              %
% Programming dates: Nov 2008 to June 2009                        %
% Last revised: Dec  2009   (simplified version for demo only)    %
% -----------------------------------------------------------------
% Papers -- Citation Details:
% 1) X.-S. Yang, S. Deb, Cuckoo search via Levy flights,
% in: Proc. of World Congress on Nature & Biologically Inspired
% Computing (NaBIC 2009), December 2009, India,
% IEEE Publications, USA,  pp. 210-214 (2009).
% http://arxiv.org/PS_cache/arxiv/pdf/1003/1003.1594v1.pdf 
% 2) X.-S. Yang, S. Deb, Engineering optimization by cuckoo search,
% Int. J. Mathematical Modelling and Numerical Optimisation, 
% Vol. 1, No. 4, 330-343 (2010). 
% http://arxiv.org/PS_cache/arxiv/pdf/1005/1005.2908v2.pdf
% ----------------------------------------------------------------%
% This demo program only implements a standard version of         %
% Cuckoo Search (CS), as the Levy flights and generation of       %
% new solutions may use slightly different methods.               %
% The pseudo code was given sequentially (select a cuckoo etc),   %
% but the implementation here uses Matlab's vector capability,    %
% which results in neater/better codes and shorter running time.  % 
% This implementation is different and more efficient than the    %
% the demo code provided in the book by 
%    "Yang X. S., Nature-Inspired Metaheuristic Algoirthms,       % 
%     2nd Edition, Luniver Press, (2010).                 "       %
% --------------------------------------------------------------- %

% =============================================================== %
% Notes:                                                          %
% Different implementations may lead to slightly different        %
% behavour and/or results, but there is nothing wrong with it,    %
% as this is the nature of random walks and all metaheuristics.   %
% -----------------------------------------------------------------

% Additional Note: This version uses a fixed number of generation %
% (not a given tolerance) because many readers asked me to add    %
%  or implement this option.                               Thanks.%                          
function [bestnest,fmin]=cuckoo_search_new(n)
if nargin<1,
% Number of nests (or different solutions)
n=25;
end

% Discovery rate of alien eggs/solutions
pa=0.25;

%% Change this if you want to get better results
N_IterTotal=1000;
%% Simple bounds of the search domain
% Lower bounds
nd=15; 
Lb=-5*ones(1,nd); 
% Upper bounds
Ub=5*ones(1,nd);

% Random initial solutions
for i=1:n,
nest(i,:)=Lb+(Ub-Lb).*rand(size(Lb));
end

% Get the current best
fitness=10^10*ones(n,1);
[fmin,bestnest,nest,fitness]=get_best_nest(nest,nest,fitness);

N_iter=0;
%% Starting iterations
for iter=1:N_IterTotal,
    % Generate new solutions (but keep the current best)
     new_nest=get_cuckoos(nest,bestnest,Lb,Ub);   
     [fnew,best,nest,fitness]=get_best_nest(nest,new_nest,fitness);
    % Update the counter
      N_iter=N_iter+n; 
    % Discovery and randomization
      new_nest=empty_nests(nest,Lb,Ub,pa) ;
    
    % Evaluate this set of solutions
      [fnew,best,nest,fitness]=get_best_nest(nest,new_nest,fitness);
    % Update the counter again
      N_iter=N_iter+n;
    % Find the best objective so far  
    if fnew<fmin,
        fmin=fnew;
        bestnest=best;
    end
end %% End of iterations

%% Post-optimization processing
%% Display all the nests
disp(strcat('Total number of iterations=',num2str(N_iter)));
fmin
bestnest

%% --------------- All subfunctions are list below ------------------
%% Get cuckoos by ramdom walk
function nest=get_cuckoos(nest,best,Lb,Ub)
% Levy flights
n=size(nest,1);
% Levy exponent and coefficient
% For details, see equation (2.21), Page 16 (chapter 2) of the book
% X. S. Yang, Nature-Inspired Metaheuristic Algorithms, 2nd Edition, Luniver Press, (2010).
beta=3/2;
sigma=(gamma(1+beta)*sin(pi*beta/2)/(gamma((1+beta)/2)*beta*2^((beta-1)/2)))^(1/beta);

for j=1:n,
    s=nest(j,:);
    % This is a simple way of implementing Levy flights
    % For standard random walks, use step=1;
    %% Levy flights by Mantegna's algorithm
    u=randn(size(s))*sigma;
    v=randn(size(s));
    step=u./abs(v).^(1/beta);
  
    % In the next equation, the difference factor (s-best) means that 
    % when the solution is the best solution, it remains unchanged.     
    stepsize=0.01*step.*(s-best);
    % Here the factor 0.01 comes from the fact that L/100 should the typical
    % step size of walks/flights where L is the typical lenghtscale; 
    % otherwise, Levy flights may become too aggresive/efficient, 
    % which makes new solutions (even) jump out side of the design domain 
    % (and thus wasting evaluations).
    % Now the actual random walks or flights
    s=s+stepsize.*randn(size(s));
   % Apply simple bounds/limits
   nest(j,:)=simplebounds(s,Lb,Ub);
end

%% Find the current best nest
function [fmin,best,nest,fitness]=get_best_nest(nest,newnest,fitness)
% Evaluating all new solutions
for j=1:size(nest,1),
    fnew=fobj(newnest(j,:));
    if fnew<=fitness(j),
       fitness(j)=fnew;
       nest(j,:)=newnest(j,:);
    end
end
% Find the current best
[fmin,K]=min(fitness) ;
best=nest(K,:);

%% Replace some nests by constructing new solutions/nests
function new_nest=empty_nests(nest,Lb,Ub,pa)
% A fraction of worse nests are discovered with a probability pa
n=size(nest,1);
% Discovered or not -- a status vector
K=rand(size(nest))>pa;

% In the real world, if a cuckoo's egg is very similar to a host's eggs, then 
% this cuckoo's egg is less likely to be discovered, thus the fitness should 
% be related to the difference in solutions.  Therefore, it is a good idea 
% to do a random walk in a biased way with some random step sizes.  
%% New solution by biased/selective random walks
stepsize=rand*(nest(randperm(n),:)-nest(randperm(n),:));
new_nest=nest+stepsize.*K;
for j=1:size(new_nest,1)
    s=new_nest(j,:);
  new_nest(j,:)=simplebounds(s,Lb,Ub);  
end

% Application of simple constraints
function s=simplebounds(s,Lb,Ub)
  % Apply the lower bound
  ns_tmp=s;
  I=ns_tmp<Lb;
  ns_tmp(I)=Lb(I);
  
  % Apply the upper bounds 
  J=ns_tmp>Ub;
  ns_tmp(J)=Ub(J);
  % Update this new move 
  s=ns_tmp;

%% You can replace the following by your own functions
% A d-dimensional objective function
function z=fobj(u)
%% d-dimensional sphere function sum_j=1^d (u_j-1)^2. 
%  with a minimum at (1,1, ...., 1); 
z=sum((u-1).^2);


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

布谷鸟算法详细讲解 的相关文章

随机推荐

  • ROS学习笔记-1: 构建工作空间-创建catkin包-编写发布器与订阅器

    1 ROS文件系统介绍 http wiki ros org cn ROS Tutorials NavigatingTheFilesystem 2 Installing catkin http wiki ros org catkin Inst
  • 信息安全重点知识

    一 信息安全概述 网络空间安全的重要性 xff1a 没有网络安全就没有国家安全信息安全 xff1a 防止数据未授权的访问 xff0c 数据有意和物一的威胁 网络安全是信息安全的子集 信息安全的三要素 xff08 CIA xff09 xff1
  • 海天注塑机KEBA系统数据采集

    本文章只针对海天注塑机的KEBA系统 xff0c 因为其他注塑机厂家也用KEBA系统 xff0c 他们的采集方式可能不太一样 xff0c 所以后续有时间我将写其他文章来解释 xff08 默认你已经向海天采购了OPC组件 xff09 一 采集
  • Axure基础:事件和动态面板

    这一篇文章我们主要是将如何做系统左侧的导航 xff0c 并且告诉大家如何动态的切换各个页面 一 事件 1 事件基础 事件的核心就是什么时候做什么事 其中的什么时候可以是如下 xff1a 能做的事情如下 xff1a 2 远程监控云中的事件 监
  • 设备联网调试三板斧

    在实际的工业互联网项目中 xff0c 设备联网所占的比重越来越大 有的一期项目为了简单快速上线 xff0c 让客户直观体会到工业互联网的效果 xff0c 直接会把设备联网放在一期项目的重点 那么在做此类项目时 xff0c 设备联网调试就显得
  • 光立方完全解析

    转载请注明出处 xff1a http blog csdn net ruoyunliufeng article details 37903899 这个4 4 4的三色光立方是我在初学单片机的时候做的一个小项目 很适合给初学单片机和C语言的同学
  • 远程视频监控之应用篇(mjpg-streamer)

    转载请注明出处 xff1a http blog csdn net ruoyunliufeng article details 38515311 这篇文章将主要结合源码介绍mjpg streamer xff0c 使小伙伴们了解视频监控的实现
  • Matplotlib 入门(三):多图合并

    一 多合一显示 1 subplot方法 xff1a 设置行 列和起始点 plt subplot 2 1 1 分成两行一列 xff0c 起始点为1 2 代码 coding utf 8 34 34 34 Created on Sun Sep 2
  • python毫秒级延时

    一 毫秒延时 近期有一个ms级别延时的需求 xff0c 实际测试了一下 xff0c 环境 xff1a win7 64位 xff0c python2 7 13 结果 xff1a 毫秒级别的延时是能够支持的 xff0c 微妙是不支持的 二 de
  • 数据分析之乳腺癌预测

    零 定义问题 1 1 数据介绍 http archive ics uci edu ml machine learning databases breast cancer wisconsin breast cancer wisconsin n
  • px4通过mavros+wifi+板载计算机连接地面站

    px4通过mavros 43 wifi 43 板载计算机连接地面站 https blog csdn net zouxu634866 article details 106835967
  • 关于mongodb占用内存过大的问题

    一 现象 最近发现自己服务器内存越来越少 xff0c 查了一下原来是部署的mongodb使用内存在线性增加 不查不知道 xff0c 一查吓一跳 xff0c 竟然占用了3G的内存 二 分析 1 内存增加的原因 mongo为了优化他的读写效率
  • 软件开发生命周期-3-每个阶段的输入输出

    记录下一点自己的心得体会 xff0c 分享给大家 xff0c 有不足之处 xff0c 望指教 第一阶段 xff1a 假想阶段 本阶段是整个软件开发的开始阶段 xff0c 输入可以是为了提高工作效率的某个好的想法或者是公司领导为了帮助管理发出
  • 【HP Cloud Recovery+暗影精灵4】 换固态+恢复惠普原厂系统

    软件工程专业 xff0c 电脑里自然会安装各种软件 用了一年零几个月 xff0c 不插电开机后进入桌面后得等一会儿才能打开应用程序 xff1b 插电开机稍微快一点 xff0c 但是文件管理有时会崩溃 趁着处于假期 xff0c 于是决定换个固
  • Jetson TX2 安装 Realsense D435 SDK

    Jetson TX2 安装 Realsense D435 SDK 一 主要内容 Jetson TX2 重装系统重建内核并安装SDK 二 Jetson TX2 重装系统 三 重建内核并安装SDK realsense官方提供的SDK安装方法无法
  • Nuxt 状态保持

    最近工地没啥砖头搬 找找可以研究的地方 业务需求在Nuxt的SSR状态下 需要记录用户的登录状态 状态保持有两种方案 1 使用vuex persistedstate将本地缓存的数据和vuex的数据合并 只能在客户端内拿到token状态 2
  • C++:类和对象: 友元

    概念 1 xff1a 程序中 xff0c 某个类的私有属性 可以让类外 一些特殊函数或者类进行访问 xff0c 这个就是友元技术 2 有元的关键字是 xff1a friend 3 xff1a 友元的三种实现 xff1a 全局函数做友元 类做
  • C++笔试题目大全

    1 new delete malloc free 关系 delete 会调用对象的析构函数 和 new 对应 free 只会释放内存 xff0c new 调用构造函数 malloc 与 free 是 C 43 43 C 语言的标准库函数 x
  • 视觉里程计(VisualOdometry)原理及实现

    一 视觉里程计 VisualOdometry 介绍 目前 xff0c 有不止一种方式可以确定移动机器人的轨迹 xff0c 这里将重点强调 视觉里程计 这种方法 在这种方法中 xff0c 单个相机或者双目相机被用到 xff0c 其目的是为了重
  • 布谷鸟算法详细讲解

    今天我要讲的内容是布谷鸟算法 xff0c 英文叫做Cuckoo search CS algorithm 首先还是同样 xff0c 介绍一下这个算法的英文含义 xff0c Cuckoo是布谷鸟的意思 xff0c 啥是布谷鸟呢 xff0c 是一