小学僧cuckoo求函数优化问题
来源:互联网 发布:阿里云国际注册方法 编辑:程序博客网 时间:2024/05/17 06:50
% -----------------------------------------------------------------
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;
0 0
- 小学僧cuckoo求函数优化问题
- Cuckoo Hashing :二分匹配问题
- 小学题目:求4位数
- 小学期 电话号码问题
- 小学奥数题--饮料问题
- cuckoo的配置以及启动场景问题分析
- 安装cuckoo的requirements.txt出现的问题和解决
- 一道小学奥数题,不会做,求解答
- 小学期 比赛计分问题
- 函数指针问题,求解答
- 多元函数求极值问题
- 函数指针问题,求解答
- 多元函数求极值问题
- 多元函数求极值问题
- cuckoo framework
- Cuckoo Hashing
- Cuckoo Search
- Cuckoo Sandbox
- jdbc学习:操作Oracle的CLOB和BLOB数据
- qt中如何使用ActiveX读写word
- MySQL数据库中的5种数据类型简介
- 299. Bulls and Cows
- 链表面试题总结(一)
- 小学僧cuckoo求函数优化问题
- java判断一个链表是否是回文结构
- java前后端技术栈
- 判断鼠标进入/离开某块的方向
- C++ 函数重载 详解
- 【39.29%】【codeforces 552E】Vanya and Brackets
- Mongodb初次安装过程
- 【map】 C++中map容器的说明和使用技巧
- jdbc学习:建立数据库表以及存储过程等