【SVM回归预测】基于matlab布谷鸟算法优化SVM回归预测【含Matlab源码 1422期】
一、布谷鳥算法簡介
布谷鳥算法,英文叫做Cuckoo search (CS algorithm)。首先還是同樣,介紹一下這個(gè)算法的英文含義, Cuckoo是布谷鳥的意思,啥是布谷鳥呢,是一種叫做布谷的鳥,o(∩_∩)o ,這種鳥她媽很懶,自己生蛋自己不養(yǎng),一般把它的寶寶扔到別的種類鳥的鳥巢去。但是呢,當(dāng)孵化后,遇到聰明的鳥媽媽,一看就知道不是親生的,直接就被鳥媽媽給殺了。于是這群布谷鳥寶寶為了保命,它們就模仿別的種類的鳥叫,讓智商或者情商極低的鳥媽媽誤認(rèn)為是自己的親寶寶,這樣它就活下來了。
布谷鳥搜索算法(Cuckoo Search, CS)是2009年Xin-She Yang 與Suash Deb在《Cuckoo Search via Levy Flights》一文中提出的一種優(yōu)化算法。布谷鳥算法是一種集合了布谷鳥巢寄生性和萊維飛行(Levy Flights)模式的群體智能搜索技術(shù),通過隨機(jī)游走的方式搜索得到一個(gè)最優(yōu)的鳥巢來孵化自己的鳥蛋。這種方式可以達(dá)到一種高效的尋優(yōu)模式。
1 布谷鳥的巢寄生性
2 萊維飛行
圖1.模擬萊維飛行軌跡示意圖
3 布谷鳥搜索算法的實(shí)現(xiàn)過程
二、部分源代碼
%% 數(shù)據(jù)的提取和預(yù)處理 % 載入測試數(shù)據(jù)上證指數(shù)(1990.12.19-2009.08.19) % 數(shù)據(jù)是一個(gè)4579*6的double型的矩陣,每一行表示每一天的上證指數(shù) % 6列分別表示當(dāng)天上證指數(shù)的開盤指數(shù),指數(shù)最高值,指數(shù)最低值,收盤指數(shù),當(dāng)日交易量,當(dāng)日交易額. clear clc load chapter_sh.mat;% 提取數(shù)據(jù) [m,n] = size(sh); ts = sh(2:m,1); % 選取2到4579個(gè)交易日內(nèi)每日的開盤指數(shù)作為因變量 tsx =sh(1:m-1,:); %選取1到4578個(gè)交易日% 數(shù)據(jù)預(yù)處理,將原始數(shù)據(jù)進(jìn)行歸一化 ts = ts'; tsx = tsx';% mapminmax為matlab自帶的映射函數(shù) % 對ts進(jìn)行歸一化 [TS,TSps] = mapminmax(ts,1,2); %歸一化在區(qū)間[1 2] % 對TSX進(jìn)行轉(zhuǎn)置,以符合libsvm工具箱的數(shù)據(jù)格式要求 TS = TS';% mapminmax為matlab自帶的映射函數(shù) % 對tsx進(jìn)行歸一化 [TSX,TSXps] = mapminmax(tsx,1,2); %歸一化在區(qū)間[1 2] % 對TSX進(jìn)行轉(zhuǎn)置,以符合libsvm工具箱的數(shù)據(jù)格式要求 TSX = TSX';Tol=1.0e-5; n=25;%鳥巢個(gè)數(shù) % Discovery rate of alien eggs/solutions pa=0.25;%為最大迭代次數(shù)限制 %% Simple bounds of the search domain % Lower bounds nd=2; Lb=0.01*ones(1,nd); % Upper bounds Ub=100*ones(1,nd); %隨機(jī)產(chǎn)生初始解 % Random initial solutions for i=1:n, nest(i,:)=Lb+(Ub-Lb).*rand(size(Lb)); end %得到當(dāng)前的最優(yōu)解 % Get the current best for i=1:nfitness(i)=fun(nest(i,:)); endfitness=10^10*ones(n,1); [fmin,bestnest,nest,fitness]=get_best_nest(nest,nest,fitness,Ub,Lb); for i=1:nnest(i,find(nest(i,:)>Ub(1)))=Ub(1);nest(i,find(nest(i,:)<Lb(1)))=Lb(1); endN_iter=0; %開始迭代 %% Starting iterations for iter=1:1 %while (fmin>Tol),% 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,Ub,Lb);% Update the counterN_iter=N_iter+n; % Discovery and randomizationnew_nest=empty_nests(nest,Lb,Ub,pa) ;% Evaluate this set of solutions[fnew,best,nest,fitness]=get_best_nest(nest,new_nest,fitness,Ub,Lb);% Update the counter againN_iter=N_iter+n;% Find the best objective so far if fnew<fmin,fmin=fnew;bestnest=best;end end %% End of iterations(迭代)% ----------------------------------------------------------------- % Cuckoo Search (CS) algorithm by Xin-She Yang and Suash Deb % % Programmed by Xin-She Yang at Cambridge University % % Programming dates: Nov 2008 to June 2009 % % Last revised: Dec 2009 (simplified version for demo only) % % ----------------------------------------------------------------- % Papers -- Citation Details: % 1) X.-S. Yang, S. Deb, Cuckoo search via Levy flights, % in: Proc. of World Congress on Nature & Biologically Inspired % Computing (NaBIC 2009), December 2009, India, % IEEE Publications, USA, pp. 210-214 (2009). % http://arxiv.org/PS_cache/arxiv/pdf/1003/1003.1594v1.pdf % 2) X.-S. Yang, S. Deb, Engineering optimization by cuckoo search, % Int. J. Mathematical Modelling and Numerical Optimisation, % Vol. 1, No. 4, 330-343 (2010). % http://arxiv.org/PS_cache/arxiv/pdf/1005/1005.2908v2.pdf % ----------------------------------------------------------------% % This demo program only implements a standard version of % % Cuckoo Search (CS), as the Levy flights and generation of % % new solutions may use slightly different methods. % % The pseudo code was given sequentially (select a cuckoo etc), % % but the implementation here uses Matlab's vector capability, % % which results in neater/better codes and shorter running time. % % This implementation is different and more efficient than the % % the demo code provided in the book by % "Yang X. S., Nature-Inspired Metaheuristic Algoirthms, % % 2nd Edition, Luniver Press, (2010). " % % --------------------------------------------------------------- %% =============================================================== % % Notes: % % Different implementations may lead to slightly different % % behavour and/or results, but there is nothing wrong with it, % % as this is the nature of random walks and all metaheuristics. % % -----------------------------------------------------------------function [bestnest,fmin]=cuckoo_search(n) %n為鳥巢數(shù)目 if nargin<1, % nargin是用來判斷輸入變量個(gè)數(shù)的函數(shù) % Number of nests (or different solutions) n=25; end% Discovery rate of alien eggs/solutions pa=0.25;%% Change this if you want to get better results % Tolerance Tol=1.0e-5; %為最大迭代次數(shù)限制 %% Simple bounds of the search domain % Lower bounds nd=15; Lb=-5*ones(1,nd); % Upper bounds Ub=5*ones(1,nd);%隨機(jī)產(chǎn)生初始解 % Random initial solutions for i=1:n, nest(i,:)=Lb+(Ub-Lb).*rand(size(Lb)); end%得到當(dāng)前的最優(yōu)解 % 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 while (fmin>Tol),% 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 counterN_iter=N_iter+n; % Discovery and randomizationnew_nest=empty_nests(nest,Lb,Ub,pa) ;% Evaluate this set of solutions[fnew,best,nest,fitness]=get_best_nest(nest,new_nest,fitness);% Update the counter againN_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). 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 algorithmu=randn(size(s))*sigma;v=randn(size(s));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);% 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));% Apply simple bounds/limitsnest(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;% Application of simple constraints function s=simplebounds(s,Lb,Ub)% Apply the lower boundns_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); sum=0; global nd; for i=1:ndsum=sum+x(i)^2; end z=sum;三、運(yùn)行結(jié)果
四、matlab版本及參考文獻(xiàn)
1 matlab版本
2014a
2 參考文獻(xiàn)
[1] 包子陽,余繼周,楊杉.智能優(yōu)化算法及其MATLAB實(shí)例(第2版)[M].電子工業(yè)出版社,2016.
[2]張巖,吳水根.MATLAB優(yōu)化算法源代碼[M].清華大學(xué)出版社,2017.
[3]周品.MATLAB 神經(jīng)網(wǎng)絡(luò)設(shè)計(jì)與應(yīng)用[M].清華大學(xué)出版社,2013.
[4]陳明.MATLAB神經(jīng)網(wǎng)絡(luò)原理與實(shí)例精解[M].清華大學(xué)出版社,2013.
[5]方清城.MATLAB R2016a神經(jīng)網(wǎng)絡(luò)設(shè)計(jì)與應(yīng)用28個(gè)案例分析[M].清華大學(xué)出版社,2018.
總結(jié)
以上是生活随笔為你收集整理的【SVM回归预测】基于matlab布谷鸟算法优化SVM回归预测【含Matlab源码 1422期】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《快播》仿网易云音乐UI,在线视频,在线
- 下一篇: 数据分析:RFM模型