一、布谷鸟算法简介
布谷鸟算法,英文叫做Cuckoo search (CS algorithm)。首先还是同样,介绍一下这个算法的英文含义, Cuckoo是布谷鸟的意思,啥是布谷鸟呢,是一种叫做布谷的鸟,o(∩_∩)o ,这种鸟她妈很懒,自己生蛋自己不养,一般把它的宝宝扔到别的种类鸟的鸟巢去。但是呢,当孵化后,遇到聪明的鸟妈妈,一看就知道不是亲生的,直接就被鸟妈妈给杀了。于是这群布谷鸟宝宝为了保命,它们就模仿别的种类的鸟叫,让智商或者情商极低的鸟妈妈误认为是自己的亲宝宝,这样它就活下来了。 布谷鸟搜索算法(Cuckoo Search, CS)是2009年Xin-She Yang 与Suash Deb在《Cuckoo Search via Levy Flights》一文中提出的一种优化算法。布谷鸟算法是一种集合了布谷鸟巢寄生性和莱维飞行(Levy Flights)模式的群体智能搜索技术,通过随机游走的方式搜索得到一个最优的鸟巢来孵化自己的鸟蛋。这种方式可以达到一种高效的寻优模式。
1 布谷鸟的巢寄生性
2 莱维飞行
图1.模拟莱维飞行轨迹示意图
3 布谷鸟搜索算法的实现过程
二、部分源代码
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;
end
function errorn = fun(x)
%该函数用来计算适应度值
load data X
[n,m]=size(X);
for i=1:n
y(i,1)=sum(X(1:i,1));
y(i,2)=sum(X(1:i,2));
y(i,3)=sum(X(1:i,3));
y(i,4)=sum(X(1:i,4));
y(i,5)=sum(X(1:i,5));
y(i,6)=sum(X(1:i,6));
end
%% 网络参数初始化
a=0.2+x(1)/2;
b1=0.2+x(2)/2;
b2=0.2+x(3)/2;
b3=0.2+x(4)/2;
b4=0.2+x(5)/2;
b5=0.2+x(6)/2;
%% 学习速率初始化
u1=0.0015;
u2=0.0015;
u3=0.0015;
u4=0.0015;
u5=0.0015;
%% 权值阀值初始化
t=1;
w11=a;
w21=-y(1,1);
w22=2*b1/a;
w23=2*b2/a;
w24=2*b3/a;
w25=2*b4/a;
w26=2*b5/a;
w31=1+exp(-a*t);
w32=1+exp(-a*t);
w33=1+exp(-a*t);
w34=1+exp(-a*t);
w35=1+exp(-a*t);
w36=1+exp(-a*t);
theta=(1+exp(-a*t))*(b1*y(1,2)/a+b2*y(1,3)/a+b3*y(1,4)/a+b4*y(1,5)/a+b5*y(1,6)/a-y(1,1));
kk=1;
%% 循环迭代
for j=1:10
%循环迭代
E(j)=0;
for i=1:30
%% 网络输出计算
t=i;
LB_b=1/(1+exp(-w11*t)); %LB层输出
LC_c1=LB_b*w21; %LC层输出
LC_c2=y(i,2)*LB_b*w22; %LC层输出
LC_c3=y(i,3)*LB_b*w23; %LC层输出
LC_c4=y(i,4)*LB_b*w24; %LC层输出
LC_c5=y(i,5)*LB_b*w25; %LC层输出
LC_c6=y(i,6)*LB_b*w26; %LC层输出
LD_d=w31*LC_c1+w32*LC_c2+w33*LC_c3+w34*LC_c4+w35*LC_c5+w36*LC_c6; %LD层输出
theta=(1+exp(-w11*t))*(w22*y(i,2)/2+w23*y(i,3)/2+w24*y(i,4)/2+w25*y(i,5)/2+w26*y(i,6)/2-y(1,1)); %阀值
ym=LD_d-theta; %网络输出值
yc(i)=ym; %拟合值
%% 权值修正
error=ym-y(i,1); %计算误差
E(j)=E(j)+abs(error); %误差求和
error1=error*(1+exp(-w11*t)); %计算误差
error2=error*(1+exp(-w11*t)); %计算误差
error3=error*(1+exp(-w11*t));
error4=error*(1+exp(-w11*t));
error5=error*(1+exp(-w11*t));
error6=error*(1+exp(-w11*t));
error7=(1/(1+exp(-w11*t)))*(1-1/(1+exp(-w11*t)))*(w21*error1+w22*error2+w23*error3+w24*error4+w25*error5+w26*error6);
%修改权值
w22=w22-u1*error2*LB_b;
w23=w23-u2*error3*LB_b;
w24=w24-u3*error4*LB_b;
w25=w25-u4*error5*LB_b;
w26=w26-u5*error6*LB_b;
w11=w11+a*t*error7;
end
end
%根据训出的灰色神经网络进行预测
for i=1:10
t=i;
LB_b=1/(1+exp(-w11*t)); %LB层输出
LC_c1=LB_b*w21; %LC层输出
LC_c2=y(i,2)*LB_b*w22; %LC层输出
LC_c3=y(i,3)*LB_b*w23; %LC层输出
LC_c4=y(i,4)*LB_b*w24; %LC层输出
LC_c5=y(i,5)*LB_b*w25;
LC_c6=y(i,6)*LB_b*w26;
LD_d=w31*LC_c1+w32*LC_c2+w33*LC_c3+w34*LC_c4+w35*LC_c5+w36*LC_c6; %LD层输出
theta=(1+exp(-w11*t))*(w22*y(i,2)/2+w23*y(i,3)/2+w24*y(i,4)/2+w25*y(i,5)/2+w26*y(i,6)/2-y(1,1)); %阀值
ym=LD_d-theta; %网络输出值
yc(i)=ym;
end
yc=yc*10000;
y(:,1)=y(:,1)*10000;
%计算预测的每月需求量
for j=30:-1:2
ys(j)=(yc(j)-yc(j-1));
end
errorn=sum(abs(ys(2:30)-X(2:30,1)'*10000));
end
function nest=get_cuckoos(nest,best,Lb,Ub)
% Levy flights
n=size(nest,1);
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);
fitness(j)=fun(nest(j,:));
end
% Find the current best
[fmin,K]=min(fitness) ;
best=nest(K,:);
三、运行结果
四、matlab版本及参考文献
1 matlab版本 2014a
2 参考文献 [1] 包子阳,余继周,杨杉.智能优化算法及其MATLAB实例(第2版)[M].电子工业出版社,2016. [2]张岩,吴水根.MATLAB优化算法源代码[M].清华大学出版社,2017. [3]周品.MATLAB 神经网络设计与应用[M].清华大学出版社,2013. [4]陈明.MATLAB神经网络原理与实例精解[M].清华大学出版社,2013. [5]方清城.MATLAB R2016a神经网络设计与应用28个案例分析[M].清华大学出版社,2018.