function [bestsol,fval]=cuckoo_ori_with_chinese_note(time)
% 由CS算法源码添加中文注释,Genlovy Hoo,2016.09.05
clear
clc
close all
format long
if nargin<1,
% Number of iteraions 迭代次数
time=2000;
end
disp('Computing ... it may take a few minutes.');
% Number of nests (or different solutions)
n=25; % n为巢穴数量
% Discovery rate of alien eggs/solutions
pa=0.25; % 被宿主发现的概率
% Simple bounds of the search domain
% Lower bounds and upper bounds
dim = 3; % 需要寻优的参数个数
Lb=[0.05,0.25,2.0]; % 设置参数下界
Ub=[2.0,1.3,15.0]; % 设置参数上界
% Random initial solutions
nest=zeros(n,dim);
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 t=1:time
% 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 solution
[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).
% Levy flights参数准备
beta=3/2;
sigma=(gamma(1+beta)*sin(pi*beta/2)/(gamma((1+beta)/2)*beta*2^((beta-1)/2)))^(1/beta); % gamma(x)求gamma函数值
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; % 生成服从 N(0,sigma^2) 的随机数u,u为长度为参数个数的向量
v=randn(size(s)); % 生成服从 N(0,1) 的随机数v向量
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); % 巢穴位置变化量,如当前巢穴为最优解,则变化量将为0
% 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 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.
nestn1=nest(randperm(n),:); % 重新随机排列巢穴
nestn2=nest(randperm(n),:); % 重新随机排序巢穴
%% New solution by biased/selective random walks
stepsize=rand*(nestn1-nestn2); % 计算调整步长
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; % 更新参数
%% Spring desgn optimization -- objective function
function z=fobj(u)
% The well-known spring design problem
z=(2+u(3))*u(1)^2*u(2);
z=z+getnonlinear(u);
function Z=getnonlinear(u)
Z=0;
% Penalty constant
lam=10^15;
% Inequality constraints
g(1)=1-u(2)^3*u(3)/(71785*u(1)^4);
gtmp=(4*u(2)^2-u(1)*u(2))/(12566*(u(2)*u(1)^3-u(1)^4));
g(2)=gtmp+1/(5108*u(1)^2)-1;
g(3)=1-140.45*u(1)/(u(2)^2*u(3));
g(4)=(u(1)+u(2))/1.5-1;
% No equality constraint in this problem, so empty;
geq=[];
% Apply inequality constraints
for k=1:length(g),
Z=Z+ lam*g(k)^2*getH(g(k));
end
% Apply equality constraints
for k=1:length(geq),
Z=Z+lam*geq(k)^2*getHeq(geq(k));
end
% Test if inequalities hold
% Index function H(g) for inequalities
function H=getH(g)
if g<=0,
H=0;
else
H=1;
end
% Index function for equalities
function H=getHeq(geq)
if geq==0,
H=0;
else
H=1;
end
% ----------------- end ------------------------------

m0_64795180
- 粉丝: 23
最新资源
- 中小企业会计信息化过程中的问题与对策.docx
- 湖南首个互联网医院长沙开建.docx
- AI与大数据融合解决方案.pptx
- 中国大数据法治指数分析报告.docx
- 基于物联网技术的高校监考任务提醒系统的设计与开发.docx
- 摄影摄像课程网站设计制作.doc
- 《Linux系统》课程实验项目.doc
- 案例教学中计算机基础教学的运用.docx
- 商业银行运用计算机审计思考.docx
- 清华大学新闻与传播学院新媒体研究中心元宇宙文化实验室出品:全网疯传的 Deepseek 超详细从入门到精通 PDF 版本大揭秘
- 教育信息化背景下中小学数字教材规模化应用与展望.docx
- 深度学习理念下实施物理模型建构的创新实验设计.docx
- 网络安全检测与监控技术研究.docx
- AutoCAD-二次开发技术VBA在窗体绘图中的应用.doc
- Linux-Cgroups技术概述.docx
- 化妆品行业的网络营销策略.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



- 1
- 2
前往页