您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 经营企划 > 布谷鸟算法求函数最优值
主函数(cuckoo_search_improve.m)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%function[huatu,t]=cuckoo_search_improve(n)%t表示为寻优次数,huatu表示寻优次数为t时最优值即fminifnargin1,%判断输入变量个数的函数,当变量个数为0时默认取n=25%Numberofnests(ordifferentsolutions)n=25;end%Discoveryrateofalieneggs/solutionspa=0.25;%宿主鸟建造新鸟巢的概率,或者说发现外来鸟蛋的概率%%Changethisifyouwanttogetbetterresults%ToleranceTol=1.0e-5;%设置精度%%Simpleboundsofthesearchdomainnd=4;%函数参数(变量)个数%Lb=[0.35,500,0];%Lowerbounds变量下界%Ub=[1,850,40];%%Upperbounds变量上界%Lb=[500,0.35,0];%Lowerbounds变量下界%Ub=[850,1,40];%%Upperbounds变量上界Lb=[pi/6,pi/6,0.3,40];%Lowerbounds变量下界Ub=[pi/2,pi/3,1,80];%%Upperbounds变量上界%nd=2;%函数参数(变量)个数%Lb=[-5.12,-5.12];%Lowerbounds变量下界%Ub=[5.12,5.12];%%Upperbounds变量上界%%Randominitialsolutions(初始化鸟窝位置,n个鸟窝的初始位置)fori=1:n,nest(i,:)=Lb+(Ub-Lb).*rand(size(Lb));end%Getthecurrentbest%求解当前最优解fitness=10^10*ones(n,1);%函数初值,n个鸟巢的取值都为10^10,意味着取值比较大,因为我们这里是找最小值.[fmin,bestnest,nest,fitness]=get_best_nest(nest,nest,fitness);%两个nestN_iter=0;%%Startingiterations(开始的迭代次数为0)%iteration=input('请输入迭代次数(不输入则默认为25)iteration=');iteration=200;iflength(iteration)==0iteration=25;endhuatu=zeros(1,iteration);%迭代次数200,每次迭代取前面k*n个鸟窝里面最优的,k为迭代次数.初始最优函数值都为0,t=1;whilet=iteration%Generatenewsolutions(butkeepthecurrentbest)产生新解,但保留目前最优解new_nest=get_cuckoos(nest,bestnest,Lb,Ub);%cockoos随机走动函数(寻找鸟窝的点)(即nest)[fnew,best,nest,fitness]=get_best_nest(nest,new_nest,fitness);%%找到当前最优的鸟巢%Updatethecounter更新鸟窝的总数N_iter=N_iter+n;%Discoveryandrandomization寻找及随机化new_nest=empty_nests(nest,Lb,Ub,pa);%%构建新鸟巢代替某些鸟巢%Evaluatethissetofsolutions评估解集[fnew,best,nest,fitness]=get_best_nest(nest,new_nest,fitness);%%找到当前最优的鸟巢%Updatethecounteragain再次更新鸟窝的总数N_iter=N_iter+n;%Findthebestobjectivesofar找到当前最优目标iffnew=fminfmin=fnew;%更新最优目标函数值bestnest=best;%更新最优鸟窝endhuatu(t)=fmin;%第t次迭代的最优目标函数值t=t+1;%下一次迭代end%%Endofiterationsplot(1:iteration,huatu);%%Post-optimizationprocessing后优化处理%%Displayallthenestsdisp(strcat('Totalnumberofiterations=',num2str(N_iter)));%显示迭代总数'显示最优目标函数值',num2str(fmin)'迭代次数',num2str(bestnest)%------------------------------------------------------------------------------------------%------------------------------------------------------------------------------------------%%所有的子函数%------------------------------------------------------------------------------------------%------------------------------------------------------------------------------------------%%---------------Allsubfunctionsarelistbelow------------------%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%cockoos随机走动函数(寻找鸟窝的点),即鸟窝更新公式%%Getcuckoosbyramdomwalkfunctionnest=get_cuckoos(nest,best,Lb,Ub)%Levyflightsn=size(nest,1);%鸟巢个数,即矩阵行数%Levyexponentandcoefficient%Fordetails,seeequation(2.21),Page16(chapter2)ofthebook%X.S.Yang,Nature-InspiredMetaheuristicAlgorithms,2ndEdition,LuniverPress,(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.6996forj=1:n,s=nest(j,:);%ThisisasimplewayofimplementingLevyflights%Forstandardrandomwalks,usestep=1;%%LevyflightsbyMantegna'salgorithmu=randn(size(s))*sigma;%v=randn(size(s));step=u./abs(v).^(1/beta);%Inthenextequation,thedifferencefactor(s-best)meansthat%whenthesolutionisthebestsolution,itremainsunchanged.stepsize=0.01*step.*(s-best);%Herethefactor0.01comesfromthefactthatL/100shouldthetypical%stepsizeofwalks/flightswhereListhetypicallenghtscale;%otherwise,Levyflightsmaybecometooaggresive/efficient,%whichmakesnewsolutions(even)jumpoutsideofthedesigndomain%(andthuswastingevaluations).%Nowtheactualrandomwalksorflightss=s+stepsize.*randn(size(s));%更新另外一组鸟巢%Applysimplebounds/limits%s=s+s*tan((rand-0.5)*pi);nest(j,:)=simplebounds(s,Lb,Ub);%把落在边界外的点使用simplebounds使之落入定义域内end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%找到当前最优的鸟巢%%Findthecurrentbestnestfunction[fmin,best,nest,fitness]=get_best_nest(nest,newnest,fitness)%Evaluatingallnewsolutionsforj=1:size(nest,1),%size(nest,1)返回nest的行数fnew=fobj(newnest(j,:));%%%%%%%%%%%%%%%%%%引用到了最后的函数fobj(自己可以任意定义),输出为当前鸟巢的函数值iffnew=fitness(j)fitness(j)=fnew;nest(j,:)=newnest(j,:);endend%Findthecurrentbest[fmin,K]=min(fitness);%找到当前鸟巢最优函数值best=nest(K,:);%最优鸟巢位置,K代表第K行的鸟巢,也就是当前第K个鸟巢为最优鸟巢%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%构建新鸟巢代替某些鸟巢%%Replacesomenestsbyconstructingnewsolutions/nestsfunctionnew_nest=empty_nests(nest,Lb,Ub,pa)%Afractionofworsenestsarediscoveredwithaprobabilitypan=size(nest,1);%nest行数,2代表列数%Discoveredornot--astatusvectorK=rand(size(nest))pa;%pa表示随机改变鸟巢的位置,得到新的鸟巢位置%Intherealworld,ifacuckoo'seggisverysimilartoahost'seggs,then%thiscuckoo'seggislesslikelytobediscovered,thusthefitnessshould%berelatedtothedifferenceinsolu
本文标题:布谷鸟算法求函数最优值
链接地址:https://www.777doc.com/doc-2455316 .html