【優化預測】基於matlab布穀鳥演算法優化SVM預測【含Matlab原始碼 1422期】

語言: CN / TW / HK

一、布穀鳥演算法簡介

布穀鳥演算法,英文叫做Cuckoo search (CS algorithm)。首先還是同樣,介紹一下這個演算法的英文含義, Cuckoo是布穀鳥的意思,啥是布穀鳥呢,是一種叫做布穀的鳥,o(∩_∩)o ,這種鳥她媽很懶,自己生蛋自己不養,一般把它的寶寶扔到別的種類鳥的鳥巢去。但是呢,當孵化後,遇到聰明的鳥媽媽,一看就知道不是親生的,直接就被鳥媽媽給殺了。於是這群布穀鳥寶寶為了保命,它們就模仿別的種類的鳥叫,讓智商或者情商極低的鳥媽媽誤認為是自己的親寶寶,這樣它就活下來了。 布穀鳥搜尋演算法(Cuckoo Search, CS)是2009年Xin-She Yang 與Suash Deb在《Cuckoo Search via Levy Flights》一文中提出的一種優化演算法。布穀鳥演算法是一種集合了布穀鳥巢寄生性和萊維飛行(Levy Flights)模式的群體智慧搜尋技術,通過隨機遊走的方式搜尋得到一個最優的鳥巢來孵化自己的鳥蛋。這種方式可以達到一種高效的尋優模式。

1 布穀鳥的巢寄生性 在這裡插入圖片描述 2 萊維飛行 在這裡插入圖片描述 圖1.模擬萊維飛行軌跡示意圖

3 布穀鳥搜尋演算法的實現過程 在這裡插入圖片描述

二、部分原始碼

```c

%% 資料的提取和預處理

% 載入測試資料上證指數(1990.12.19-2009.08.19) % 資料是一個4579*6的double型的矩陣,每一行表示每一天的上證指數 % 6列分別表示當天上證指數的開盤指數,指數最高值,指數最低值,收盤指數,當日交易量,當日交易額. clear clc load chapter_sh.mat;

% 提取資料 [m,n] = size(sh); ts = sh(2:m,1); % 選取2到4579個交易日內每日的開盤指數作為因變數 tsx =sh(1:m-1,:); %選取1到4578個交易日

% 資料預處理,將原始資料進行歸一化 ts = ts'; tsx = tsx';

% mapminmax為matlab自帶的對映函式
% 對ts進行歸一化 [TS,TSps] = mapminmax(ts,1,2); %歸一化在區間[1 2] % 對TSX進行轉置,以符合libsvm工具箱的資料格式要求 TS = TS';

% mapminmax為matlab自帶的對映函式 % 對tsx進行歸一化 [TSX,TSXps] = mapminmax(tsx,1,2); %歸一化在區間[1 2] % 對TSX進行轉置,以符合libsvm工具箱的資料格式要求 TSX = TSX';

Tol=1.0e-5;
n=25;%鳥巢個數 % Discovery rate of alien eggs/solutions pa=0.25;

                                                      %為最大迭代次數限制

%% Simple bounds of the search domain % Lower bounds nd=2; Lb=0.01ones(1,nd); % Upper bounds Ub=100ones(1,nd); %隨機產生初始解 % Random initial solutions for i=1:n,
nest(i,:)=Lb+(Ub-Lb).*rand(size(Lb)); end %得到當前的最優解 % Get the current best for i=1:n fitness(i)=fun(nest(i,:)); end

fitness=10^10*ones(n,1); [fmin,bestnest,nest,fitness]=get_best_nest(nest,nest,fitness,Ub,Lb); for i=1:n nest(i,find(nest(i,:)>Ub(1)))=Ub(1); nest(i,find(nest(i,:)<Lb(1)))=Lb(1); end

N_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 counter
  N_iter=N_iter+n; 
% 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,Ub,Lb);
% 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(迭代)

% ----------------------------------------------------------------- % 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為鳥巢數目 if nargin<1, % nargin是用來判斷輸入變數個數的函式 % 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; %為最大迭代次數限制 %% Simple bounds of the search domain % Lower bounds nd=15; Lb=-5ones(1,nd); % Upper bounds Ub=5ones(1,nd); %隨機產生初始解 % Random initial solutions for i=1:n,
nest(i,:)=Lb+(Ub-Lb).rand(size(Lb)); end %得到當前的最優解 % Get the current best fitness=10^10ones(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 counter
  N_iter=N_iter+n; 
% 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);
% 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). beta=3/2; sigma=(gamma(1+beta)sin(pibeta/2)/(gamma((1+beta)/2)beta2^((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);

% 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 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 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 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); sum=0; global nd; for i=1:nd sum=sum+x(i)^2; end z=sum;

```

三、執行結果

在這裡插入圖片描述

四、matlab版本及參考文獻

1 matlab版本 2014a

2 參考文獻 [1] 包子陽,餘繼周,楊杉.智慧優化演算法及其MATLAB例項(第2版)[M].電子工業出版社,2016. [2]張巖,吳水根.MATLAB優化演算法原始碼[M].清華大學出版社,2017. [3]周品.MATLAB 神經網路設計與應用[M].清華大學出版社,2013. [4]陳明.MATLAB神經網路原理與例項精解[M].清華大學出版社,2013. [5]方清城.MATLAB R2016a神經網路設計與應用28個案例分析[M].清華大學出版社,2018.

「其他文章」