主函数(cuckoo_search_improve.m)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function [huatu,t]=cuckoo_search_improve(n) %t表示为寻优次数,huatu表示寻优次数为t时最优值即fmin
if nargin
n=25;
end
% Discovery rate of alien eggs/solutions
pa=0.25;%宿主鸟建造新鸟巢的概率,或者说发现外来鸟蛋的概率
%% Change this if you want to get better results
% Tolerance
Tol=1.0e-5;%设置精度
%% Simple bounds of the search domain
nd=4;%函数参数(变量)个数
% Lb=[0.35,500,0];% Lower bounds变量下界
% Ub=[1, 850,40];%%Upper bounds变量上界
% Lb=[500,0.35,0];% Lower bounds变量下界
% Ub=[850, 1,40];%%Upper bounds变量上界
Lb=[pi/6,pi/6,0.3,40];% Lower bounds变量下界
Ub=[pi/2,pi/3,1,80];%%Upper bounds变量上界
% nd=2;%函数参数(变量)个数
% Lb=[-5.12,-5.12];% Lower bounds变量下界
% Ub=[5.12,5.12];%%Upper bounds变量上界%
% Random initial solutions(初始化鸟窝位置,n个鸟窝的初始位置) for i=1:n,
nest(i,:)=Lb+(Ub-Lb).*rand(size(Lb));
end
% Get the current best%求解当前最优解
fitness=10^10*ones(n,1);%函数初值,n个鸟巢的取值都为10^10,意味着取值比较大,因为我们这里是找最小值.
[fmin,bestnest,nest,fitness]=get_best_nest(nest,nest,fitness);%两个nest
N_iter=0;
%% Starting iterations(开始的迭代次数为0)
% iteration=input('请输入迭代次数(不输入则默认为25)iteration='); iteration=200;
if length(iteration)==0
iteration=25; end
huatu=zeros(1,iteration);%迭代次数200,每次迭代取前面k*n个鸟窝里面最优的,k为迭代次数.初始最优函数值都为0,
t=1;
while t
% Generate new solutions (but keep the current best)产生新解,但保留目前最优解
new_nest=get_cuckoos(nest,bestnest,Lb,Ub); %cockoos随机走动函数(寻找鸟窝的点)(即nest)
[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=fnew;%更新最优目标函数值
bestnest=best;%更新最优鸟窝
end
huatu(t)=fmin;%第t次迭代的最优目标函数值
t=t+1;%下一次迭代
end %% End of iterations
plot(1:iteration,huatu);
%% Post-optimization processing后优化处理
%% Display all the nests
disp(strcat('Total number of iterations=',num2str(N_iter)));%显示迭代总数 '显示最优目标函数值',num2str(fmin)
'迭代次数',num2str(bestnest)
% ------------------------------------------------------------------------------------------
% ------------------------------------------------------------------------------------------
% %所有的子函数
% ------------------------------------------------------------------------------------------
% ------------------------------------------------------------------------------------------
%% --------------- All subfunctions are list below ------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%cockoos随机走动函数(寻找鸟窝的点),即鸟窝更新公式
%% 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);%levy过程,这里的结果为sigma=0.6996
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
% s=s+s*tan((rand-0.5)*pi);
nest(j,:)=simplebounds(s,Lb,Ub);%把落在边界外的点使用simplebounds使之落入定义域内
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),%size(nest,1)返回nest的行数
fnew=fobj(newnest(j,:));%%%%%%%%%%%%%%%%%%引用到了最后的函数fobj(自己可以任意定义),输出为当前鸟巢的函数值
if fnew
fitness(j)=fnew;
nest(j,:)=newnest(j,:);
end
end
% Find the current best
[fmin,K]=min(fitness) ;%找到当前鸟巢最优函数值
best=nest(K,:);%最优鸟巢位置,K代表第K行的鸟巢,也就是当前第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);%nest行数,2代表列数
% Discovered or not -- a status vector
K=rand(size(nest))>pa;%>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),:));%随机步长nx3; 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
ns_tmp(I)=Lb(I);%把小于参数的都设置为参数变量的下限
% Apply the upper bounds
J=ns_tmp>Ub;%找出参数变量比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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % %% 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);
%%目标求最优函数
function fobj_min=fobj(u)
x=u(1,1);y=u(1,2);g=u(1,3);w=u(1,4);
a=0.02.*sqrt(0.5.*g)*(8.*w+50.*((cos(x)./sin(y))+(sin(x)./cos(y))));
b=sqrt(((w+50.*cos(x))./sin(y)+(50*sin(x))./(2*cos(y)))/4);
fobj_min=a./b;
运行程序(run.m)
%% 环境初始化处理
clear all;
close all;
clc
n=25;
%%循环20次求均值
for i=1:20
[huatu,t]=cuckoo_search_improve(n);
x(i)=i;
y(i)=huatu(200);%默认为200次迭代,若要修改迭代次数,回到cuckoo_search_improve.m文件修改
n=i;
end
'输出20次求解结果'
[x;y]
'输出20次求解均值'
mean_y=mean(y)
主函数(cuckoo_search_improve.m)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function [huatu,t]=cuckoo_search_improve(n) %t表示为寻优次数,huatu表示寻优次数为t时最优值即fmin
if nargin
n=25;
end
% Discovery rate of alien eggs/solutions
pa=0.25;%宿主鸟建造新鸟巢的概率,或者说发现外来鸟蛋的概率
%% Change this if you want to get better results
% Tolerance
Tol=1.0e-5;%设置精度
%% Simple bounds of the search domain
nd=4;%函数参数(变量)个数
% Lb=[0.35,500,0];% Lower bounds变量下界
% Ub=[1, 850,40];%%Upper bounds变量上界
% Lb=[500,0.35,0];% Lower bounds变量下界
% Ub=[850, 1,40];%%Upper bounds变量上界
Lb=[pi/6,pi/6,0.3,40];% Lower bounds变量下界
Ub=[pi/2,pi/3,1,80];%%Upper bounds变量上界
% nd=2;%函数参数(变量)个数
% Lb=[-5.12,-5.12];% Lower bounds变量下界
% Ub=[5.12,5.12];%%Upper bounds变量上界%
% Random initial solutions(初始化鸟窝位置,n个鸟窝的初始位置) for i=1:n,
nest(i,:)=Lb+(Ub-Lb).*rand(size(Lb));
end
% Get the current best%求解当前最优解
fitness=10^10*ones(n,1);%函数初值,n个鸟巢的取值都为10^10,意味着取值比较大,因为我们这里是找最小值.
[fmin,bestnest,nest,fitness]=get_best_nest(nest,nest,fitness);%两个nest
N_iter=0;
%% Starting iterations(开始的迭代次数为0)
% iteration=input('请输入迭代次数(不输入则默认为25)iteration='); iteration=200;
if length(iteration)==0
iteration=25; end
huatu=zeros(1,iteration);%迭代次数200,每次迭代取前面k*n个鸟窝里面最优的,k为迭代次数.初始最优函数值都为0,
t=1;
while t
% Generate new solutions (but keep the current best)产生新解,但保留目前最优解
new_nest=get_cuckoos(nest,bestnest,Lb,Ub); %cockoos随机走动函数(寻找鸟窝的点)(即nest)
[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=fnew;%更新最优目标函数值
bestnest=best;%更新最优鸟窝
end
huatu(t)=fmin;%第t次迭代的最优目标函数值
t=t+1;%下一次迭代
end %% End of iterations
plot(1:iteration,huatu);
%% Post-optimization processing后优化处理
%% Display all the nests
disp(strcat('Total number of iterations=',num2str(N_iter)));%显示迭代总数 '显示最优目标函数值',num2str(fmin)
'迭代次数',num2str(bestnest)
% ------------------------------------------------------------------------------------------
% ------------------------------------------------------------------------------------------
% %所有的子函数
% ------------------------------------------------------------------------------------------
% ------------------------------------------------------------------------------------------
%% --------------- All subfunctions are list below ------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%cockoos随机走动函数(寻找鸟窝的点),即鸟窝更新公式
%% 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);%levy过程,这里的结果为sigma=0.6996
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
% s=s+s*tan((rand-0.5)*pi);
nest(j,:)=simplebounds(s,Lb,Ub);%把落在边界外的点使用simplebounds使之落入定义域内
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),%size(nest,1)返回nest的行数
fnew=fobj(newnest(j,:));%%%%%%%%%%%%%%%%%%引用到了最后的函数fobj(自己可以任意定义),输出为当前鸟巢的函数值
if fnew
fitness(j)=fnew;
nest(j,:)=newnest(j,:);
end
end
% Find the current best
[fmin,K]=min(fitness) ;%找到当前鸟巢最优函数值
best=nest(K,:);%最优鸟巢位置,K代表第K行的鸟巢,也就是当前第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);%nest行数,2代表列数
% Discovered or not -- a status vector
K=rand(size(nest))>pa;%>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),:));%随机步长nx3; 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
ns_tmp(I)=Lb(I);%把小于参数的都设置为参数变量的下限
% Apply the upper bounds
J=ns_tmp>Ub;%找出参数变量比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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % %% 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);
%%目标求最优函数
function fobj_min=fobj(u)
x=u(1,1);y=u(1,2);g=u(1,3);w=u(1,4);
a=0.02.*sqrt(0.5.*g)*(8.*w+50.*((cos(x)./sin(y))+(sin(x)./cos(y))));
b=sqrt(((w+50.*cos(x))./sin(y)+(50*sin(x))./(2*cos(y)))/4);
fobj_min=a./b;
运行程序(run.m)
%% 环境初始化处理
clear all;
close all;
clc
n=25;
%%循环20次求均值
for i=1:20
[huatu,t]=cuckoo_search_improve(n);
x(i)=i;
y(i)=huatu(200);%默认为200次迭代,若要修改迭代次数,回到cuckoo_search_improve.m文件修改
n=i;
end
'输出20次求解结果'
[x;y]
'输出20次求解均值'
mean_y=mean(y)