日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

[转载]布谷鸟算法的程序(个人注释)

發(fā)布時(shí)間:2024/8/1 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [转载]布谷鸟算法的程序(个人注释) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
最近項(xiàng)目需要,所以一直在研究布谷鳥算法,在網(wǎng)上看了一些前輩的文章,原理講的都比較透徹,我就不再贅述。貼上一個(gè)自己注釋的程序吧(這個(gè)程序也是借鑒別的大佬的,小小的做了一些修改)我也是最近才接觸布谷鳥算法,肯定有欠妥之處,希望各位大佬多多批評(píng)指正,相互學(xué)習(xí) function [bestnest,fmin]=cuckoo_search(n) if nargin<1,%【如果變量的個(gè)數(shù)小于1】 % Number of nests (or different solutions) n=15;%【則把種群數(shù)量設(shè)為15,種群數(shù)量越多,最后算法的求解越精確,但程序的運(yùn)行時(shí)間也會(huì)大大拉長(zhǎng)】 end% Discovery rate of alien eggs/solutions pa=0.25;%【布谷鳥蛋被發(fā)現(xiàn)的概率】%% Change this if you want to get better results % Tolerance Tol=1.0e-5; %閾值【循環(huán)的精度設(shè)置】%% Simple bounds of the search domain nd=6; %【需要尋優(yōu)的參數(shù)個(gè)數(shù),即搜索空間維度為nd】 % Lower bounds【鳥窩范圍的下限】 Lb=-100*ones(1,nd); % Upper bounds【鳥窩范圍的上限】 Ub=100*ones(1,nd);% Random initial solutions【把鳥窩的初始值設(shè)為隨機(jī)值,即初始化所有鳥窩的位置】 nest=zeros(n,nd); %【nest為n行nd列的零矩陣,先把最初的鳥巢設(shè)為零矩陣】 for i=1:n,%【這是個(gè)1~n的循環(huán),把每個(gè)種群中的每個(gè)鳥窩初始化為隨機(jī)數(shù)】nest(i,:)=Lb+(Ub-Lb).*rand(size(Lb)); end% Get the current best【尋找當(dāng)前最優(yōu)的巢穴】 fitness=10^10*ones(n,1);%【尋優(yōu)域的范圍設(shè)置】 [fmin,bestnest,nest,fitness]=get_best_nest(nest,nest,fitness); N_iter=0;%【計(jì)數(shù)器初始化】%% Starting iterations【開始迭代】 %for kk = 1 : 500%可以通過for循環(huán)代替下面的while while (fmin>Tol),%【只要沒達(dá)到這個(gè)精度,循環(huán)就會(huì)一直進(jìn)行】% Generate new solutions (but keep the current best)%【產(chǎn)生一個(gè)最優(yōu)解,并保留原最優(yōu)解】new_nest=get_cuckoos(nest,bestnest,Lb,Ub); %【尋找當(dāng)前最優(yōu)的巢穴】[fnew,best,nest,fitness]=get_best_nest(nest,new_nest,fitness);%【找到最佳巢穴】% Update the counter【計(jì)數(shù)器更新】N_iter=N_iter+n; %【因?yàn)槊看味家闅v所有巢穴,所以在這里每次迭代都要+n次,表示搜尋了這么多次】% Discovery and randomization【布谷鳥蛋被發(fā)現(xiàn),并通過萊維飛行(即隨機(jī)游走)來替換這些被發(fā)現(xiàn)概率較大的劣質(zhì)巢穴】 new_nest=empty_nests(nest,Lb,Ub,pa) ;%【用萊維飛行更新其余鳥窩位置】% Evaluate this set of solutions%【在更新后的鳥窩數(shù)據(jù)中再次找到最佳巢穴】[fnew,best,nest,fitness]=get_best_nest(nest,new_nest,fitness);% Update the counter again【計(jì)數(shù)器再次更新】N_iter=N_iter+n;% Find the best objective so far【找到當(dāng)前的最優(yōu)值】 if fnew<fmin,%【如果新鳥窩與目標(biāo)鳥窩之間的的距離更近(即誤差精度更小更準(zhǔn)確)】fmin=fnew;%【則用新的鳥窩的值替換原fmin中的數(shù)據(jù)】bestnest=best;%【并且把此鳥窩下的各個(gè)參數(shù)的最優(yōu)解賦給bestnest】enddisp(strcat('Total number of iterations=【當(dāng)前已進(jìn)行的迭代尋優(yōu)次數(shù)】',num2str(N_iter))); fmin bestnest end %% End of iterations【結(jié)束布谷鳥尋優(yōu)進(jìn)程】%% Post-optimization processing %% Display all the nests【循環(huán)內(nèi)外各寫一遍就是為了看它不斷迭代的過程以及最終結(jié)果】 disp(strcat('Total number of iterations=【迭代尋優(yōu)的總次數(shù)】',num2str(N_iter))); fmin bestnest figure; %【出現(xiàn)最終n個(gè)曲線的圖(n就是鳥窩個(gè)數(shù))】 plot(nest); xlabel '鳥巢的個(gè)數(shù)' %【x軸的名稱】 ylabel '待定參數(shù)的估計(jì)值' %【y軸的名稱】%% --------------- All subfunctions are list below ------------------ %% Get cuckoos by ramdom walk【用隨機(jī)游走來加強(qiáng)布谷鳥算法】【萊維飛行】 function nest=get_cuckoos(nest,best,Lb,Ub) % Levy flights【萊維飛行,是個(gè)固定程式,固定的給出隨機(jī)數(shù)的公式模型,無須修改,了解即可】 %【當(dāng)然,如果要進(jìn)行算法改進(jìn)可以在這里做文章,比如用柯西來替代萊維飛行,就是基于柯西原理的改進(jìn)的布谷鳥算法】 %【萊維飛行本質(zhì)上是一個(gè)基于馬爾科夫鏈(下一狀態(tài)取決于當(dāng)前狀態(tài))的隨機(jī)行走公式】 %【萊維飛行可以使布谷鳥尋優(yōu)綜合局部尋優(yōu)和全局尋優(yōu)】 n=size(nest,1);%【n是一個(gè)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);%【sigma這個(gè)數(shù)約等于0.7】 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%【randn函數(shù)是產(chǎn)生均值為0,方差σ^2 = 1,標(biāo)準(zhǔn)差σ = 1的正態(tài)分布的隨機(jī)數(shù)或矩陣的函數(shù)。】u=randn(size(s))*sigma;%【產(chǎn)生一個(gè)sigma倍的隨機(jī)數(shù)行向量】v=randn(size(s));%【產(chǎn)生一個(gè)隨機(jī)數(shù)行向量】step=u./abs(v).^(1/beta);%【矩陣之間進(jìn)行元素上的點(diǎn)乘除運(yùn)算】% 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);%【依舊是數(shù)值運(yùn)算】% 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 flightss=s+stepsize.*randn(size(s));%【s又變了個(gè)數(shù)】% Apply simple bounds/limitsnest(j,:)=simplebounds(s,Lb,Ub);%【獲得交替比較上下限的值】 end %% Find the current best nest【找到當(dāng)前最佳巢穴】 function [fmin,best,nest,fitness]=get_best_nest(nest,newnest,fitness) % Evaluating all new solutions【計(jì)算所有的新解】 for j=1:size(nest,1),%【j從1循環(huán)到nest的行數(shù)】fnew=fobj(newnest(j,:));%【在目標(biāo)函數(shù)中進(jìn)行運(yùn)算 ,并把z的值賦值給fnew】if fnew<=fitness(j),%【這個(gè)if語句用來判斷是否滿足精度,若更好則替換】fitness(j)=fnew;nest(j,:)=newnest(j,:);end end % Find the current best [fmin,K]=min(fitness) ;%【fmin記錄列向量fitness的每列的最小值,K記錄每列最小值的行號(hào)】 best=nest(K,:);%【獲得當(dāng)前最好的巢穴的值】 %% Replace some nests by constructing new solutions/nests【通過構(gòu)建新巢更換一些容易被發(fā)現(xiàn)的劣質(zhì)巢】 function new_nest=empty_nests(nest,Lb,Ub,pa) % A fraction of worse nests are discovered with a probability pa【劣質(zhì)巢穴會(huì)以pa的概率被淘汰】 n=size(nest,1); % Discovered or not -- a status vector【發(fā)現(xiàn)與否——狀態(tài)向量】 K=rand(size(nest))>pa;%【如果產(chǎn)生的隨機(jī)數(shù)大于pa的值,就令K等于該值】 % 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【加入隨機(jī)行走的新解集】 stepsize=rand*(nest(randperm(n),:)-nest(randperm(n),:)); %randperm會(huì)將數(shù)字順序隨機(jī)打亂,將1~n個(gè)整數(shù)隨機(jī)打亂順序再返回 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【簡(jiǎn)單約束的應(yīng)用,這個(gè)函數(shù)是包含在萊維飛行中的】 function s=simplebounds(s,Lb,Ub)%【這個(gè)函數(shù)就是交替比較上下限】% Apply the lower bound【下限】ns_tmp=s;%【賦值】I=ns_tmp<Lb;%【先判斷,若ns_tmp的值小于Lb,則把臨時(shí)變量ns_tmp中存儲(chǔ)的s的值賦給I】ns_tmp(I)=Lb(I);% Apply the upper bounds J=ns_tmp>Ub;ns_tmp(J)=Ub(J);% Update this new move 【把s這個(gè)值更新】s=ns_tmp; %% You can replace the following by your own functions【目標(biāo)函數(shù),這是最重要的,也是自己要替換的東西】 % 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-4).^2);

總結(jié)

以上是生活随笔為你收集整理的[转载]布谷鸟算法的程序(个人注释)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。