CS(布穀鳥搜尋)演算法MATLAB原始碼逐行中文註解

2021-07-22 10:40:57 字數 2745 閱讀 2163

tic % 計時

%% 清空環境匯入資料

clear

clcclose all

load wndspd % 示例資料為風速(時間序列)資料,共144個樣本

% 訓練/測試資料準備(用前3天**後一天),用前100天做測試資料

train_input(1,:)=wndspd(1

:97);

train_input(2,:)=wndspd(2

:98);

train_input(3,:)=wndspd(3

:99);

train_output=[wndspd(4

:100)]';

test_input(1,:)=wndspd(101:end-3);

test_input(2,:)=wndspd(102:end-2);

test_input(3,:)=wndspd(103:end-1);

test_output=[wndspd(104:end)]';

[input_train,rule1]=mapminmax(train_input);

[output_train,rule2]=mapminmax(train_output);

%% cs-svr

time=20;

n=20; % n為巢穴數量

pa=0.25; % 被宿主發現的概率

dim = 2; % 需要尋優的引數個數

lb=[0.01,0.01]; % 設定引數下界

ub=[100,100]; % 設定引數上界

% 隨機初始化巢穴

nest=zeros(n,dim);

for i=1

:n % 遍歷每個巢穴

nest(i,:)=lb+(ub-lb).*rand(size(lb)); % 對每個巢穴,隨機初始化引數

endfitness=ones(1,n); % 目標函式值初始化

[fmin,bestnest,nest,fitness]=get_best_nest(nest,nest,fitness,input_train,output_train,input_test,output_test); % 找出當前最佳巢穴和引數

%% 迭代開始

for t=1:time

new_nest=get_cuckoos(nest,bestnest,lb,ub); % 保留當前最優解,尋找新巢穴

[~,~,nest,fitness]=get_best_nest(nest,new_nest,fitness,input_train,output_train,input_test,output_test); % 找出當前最佳巢穴和引數

new_nest=empty_nests(nest,lb,ub,pa); % 發現並更新劣質巢穴

% 找出當前最佳巢穴和引數

[fnew,best,nest,fitness]=get_best_nest(nest,new_nest,fitness,input_train,output_train,input_test,output_test);

if fnewend

end%% 列印引數選擇結果

bestobjfun=fmin;

bestc=bestnest(1);

bestg=bestnest(2);

disp('列印引數選擇結果');

str=sprintf('best c = %g,best g = %g',bestc,bestg);

disp(str)

%% 利用回歸**分析最佳的引數進行svm網路訓練

cmd_cs_svr=['-s 3 -t 2

',' -c ',num2str(bestnest(1)),' -g ',num2str(bestnest(2))];

model_cs_svr=svmtrain(output_train',input_train',cmd_cs_svr); % svm模型訓練

%% svm網路回歸**

[output_test_pre,acc]=svmpredict(output_test',input_test',model_cs_svr); % svm模型**及其精度

test_pre=mapminmax('reverse',output_test_pre',rule2);

test_pre = test_pre';

err_pre=wndspd(104:end)-test_pre;

figure('

name

','測試資料殘差圖')

set(gcf,'unit','centimeters','position',[0.5,5,30,5])

plot(err_pre,'*-');

figure('

name

','原始-**圖')

plot(test_pre,'*r-');hold on;plot(wndspd(104:end),'bo-');

legend('**','原始')

set(gcf,'unit','centimeters','position',[0.5,13,30,5])

result=[wndspd(104:end),test_pre];

mae=mymae(wndspd(104:end),test_pre)

mse=mymse(wndspd(104:end),test_pre)

mape=mymape(wndspd(104:end),test_pre)

%% 顯示程式執行時間

toc

布穀鳥過濾器

布隆過濾器有exists方法通過對位陣列的hash計算判斷某元素是否在集合中,實現去重功能。但布隆過濾器有一下缺點 不支援反向刪除元素 一旦對位陣列進行了賦值,無法將其刪除。查詢效能弱 布隆過濾器使用多個hash函式計算位圖多個不同位點,由於多個位點在記憶體中不連續,cpu定址花銷較大。空間利用率低...

布穀鳥雜湊的學習筆記1

布穀鳥雜湊介面 public inte ce hashfamily布穀鳥雜湊的類的框架 public class cuckoohashtable public cuckoohashtable hashfamily hashfunctions,int size private void allocat...

redis快取穿透 布隆過濾器和布穀鳥過濾器

快取穿透就是查詢一次不存在的資料,因為不存在,所以也不會往redis裡寫值,這樣一直查不存在的資料就會導致一直查資料庫,redis並沒有起到作用。解決這個可以讓redis快取乙個空值或者快取乙個特殊的字串,但如果別人故意每次用不同的不存在的值惡意攻擊的話,即使快取了空值也還是沒有,這就可以用布隆過濾...