% -----------------------------------------------------------------
function [bestnest,fmin]=cuckoo_search(n)
clc
clear all
close
%以上三条语句:清空命令窗口,环境变量
if nargin<1
% number of nests (or different solutions)
n=10;%鸟巢
end
% discovery rate of alien eggs/solutions
pa=0.25;
%% change this if you want to get better results
% tolerance
maxiter=3;
%% simple bounds of the search domain
% lower bounds
nd=1;%维数
a=-1;%区间[a,b]
b=5;
lb=a*ones(1,nd);
% upper bounds
ub=b*ones(1,nd);
% random initial solutions
for i=1:n,
nest(i,:)=lb+(ub-lb).*rand(size(lb));%在a-b之间产生多个随机数
fitness(i,:)=fobj(nest(i,:));
end
nest;
fitness;
[fmin,index]=min(fitness);
bestnest=nest(index,:);
%% starting iterations
for t=1:maxiter
% 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);
% 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);
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);
stepsize=0.01*step.*(s-best);
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 i=1:n
new_nest(i,:)=simplebounds(new_nest(i,:),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(x)
%% d-dimensional sphere function sum_j=1^d (u_j-1)^2.
% with a minimum at (1,1, ...., 1);
% z=sum((u-1).^2);
z=x^2+4*x+5;
function [bestnest,fmin]=cuckoo_search(n)
clc
clear all
close
%以上三条语句:清空命令窗口,环境变量
if nargin<1
% number of nests (or different solutions)
n=10;%鸟巢
end
% discovery rate of alien eggs/solutions
pa=0.25;
%% change this if you want to get better results
% tolerance
maxiter=3;
%% simple bounds of the search domain
% lower bounds
nd=1;%维数
a=-1;%区间[a,b]
b=5;
lb=a*ones(1,nd);
% upper bounds
ub=b*ones(1,nd);
% random initial solutions
for i=1:n,
nest(i,:)=lb+(ub-lb).*rand(size(lb));%在a-b之间产生多个随机数
fitness(i,:)=fobj(nest(i,:));
end
nest;
fitness;
[fmin,index]=min(fitness);
bestnest=nest(index,:);
%% starting iterations
for t=1:maxiter
% 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);
% 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);
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);
stepsize=0.01*step.*(s-best);
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 i=1:n
new_nest(i,:)=simplebounds(new_nest(i,:),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(x)
%% d-dimensional sphere function sum_j=1^d (u_j-1)^2.
% with a minimum at (1,1, ...., 1);
% z=sum((u-1).^2);
z=x^2+4*x+5;