bartlett 算法 matlab,GWO(灰狼优化)算法MATLAB源码逐行中文注解(转载)
以優化SVM算法的參數c和g為例,對GWO算法MATLAB源碼進行了逐行中文注解。
tic % 計時器
%% 清空環境變量
close all
clear
clc
format compact
%% 數據提取
% 載入測試數據wine,其中包含的數據為classnumber = 3,wine:178*13的矩陣,wine_labes:178*1的列向量
load wine.mat
% 選定訓練集和測試集
% 將第一類的1-30,第二類的60-95,第三類的131-153做為訓練集
train_wine = [wine(1:30,:);wine(60:95,:);wine(131:153,:)];
% 相應的訓練集的標簽也要分離出來
train_wine_labels = [wine_labels(1:30);wine_labels(60:95);wine_labels(131:153)];
% 將第一類的31-59,第二類的96-130,第三類的154-178做為測試集
test_wine = [wine(31:59,:);wine(96:130,:);wine(154:178,:)];
% 相應的測試集的標簽也要分離出來
test_wine_labels = [wine_labels(31:59);wine_labels(96:130);wine_labels(154:178)];
%% 數據預處理
% 數據預處理,將訓練集和測試集歸一化到[0,1]區間
[mtrain,ntrain] = size(train_wine);
[mtest,ntest] = size(test_wine);
dataset = [train_wine;test_wine];
% mapminmax為MATLAB自帶的歸一化函數
[dataset_scale,ps] = mapminmax(dataset',0,1);
dataset_scale = dataset_scale';
train_wine = dataset_scale(1:mtrain,:);
test_wine = dataset_scale( (mtrain+1):(mtrain+mtest),: );
%% 利用灰狼算法選擇最佳的SVM參數c和g
SearchAgents_no=10; % 狼群數量,Number of search agents
Max_iteration=10; % 最大迭代次數,Maximum numbef of iterations
dim=2; % 此例需要優化兩個參數c和g,number of your variables
lb=[0.01,0.01]; % 參數取值下界
ub=[100,100]; % 參數取值上界
% v = 5; % SVM Cross Validation參數,默認為5
% initialize alpha, beta, and delta_pos
Alpha_pos=zeros(1,dim); % 初始化Alpha狼的位置
Alpha_score=inf; % 初始化Alpha狼的目標函數值,change this to -inf for maximization problems
Beta_pos=zeros(1,dim); % 初始化Beta狼的位置
Beta_score=inf; % 初始化Beta狼的目標函數值,change this to -inf for maximization problems
Delta_pos=zeros(1,dim); % 初始化Delta狼的位置
Delta_score=inf; % 初始化Delta狼的目標函數值,change this to -inf for maximization problems
%Initialize the positions of search agents
Positions=initialization(SearchAgents_no,dim,ub,lb);
Convergence_curve=zeros(1,Max_iteration);
l=0; % Loop counter循環計數器
% Main loop主循環
while l
for i=1:size(Positions,1) % 遍歷每個狼
% Return back the search agents that go beyond the boundaries of the search space
% 若搜索位置超過了搜索空間,需要重新回到搜索空間
Flag4ub=Positions(i,:)>ub;
Flag4lb=Positions(i,:)
% 若狼的位置在最大值和最小值之間,則位置不需要調整,若超出最大值,最回到最大值邊界;
% 若超出最小值,最回答最小值邊界
Positions(i,:)=(Positions(i,:).*(~(Flag4ub+Flag4lb)))+ub.*Flag4ub+lb.*Flag4lb; % ~表示取反
% 計算適應度函數值
cmd = [' -c ',num2str(Positions(i,1)),' -g ',num2str(Positions(i,2))];
model=svmtrain(train_wine_labels,train_wine,cmd); % SVM模型訓練
[~,fitness]=svmpredict(test_wine_labels,test_wine,model); % SVM模型預測及其精度
fitness=100-fitness(1); % 以錯誤率最小化為目標
% Update Alpha, Beta, and Delta
if fitness
Alpha_score=fitness; % 則將Alpha狼的目標函數值更新為最優目標函數值,Update alpha
Alpha_pos=Positions(i,:); % 同時將Alpha狼的位置更新為最優位置
end
if fitness>Alpha_score && fitness
Beta_score=fitness; % 則將Beta狼的目標函數值更新為最優目標函數值,Update beta
Beta_pos=Positions(i,:); % 同時更新Beta狼的位置
end
if fitness>Alpha_score && fitness>Beta_score && fitness
Delta_score=fitness; % 則將Delta狼的目標函數值更新為最優目標函數值,Update delta
Delta_pos=Positions(i,:); % 同時更新Delta狼的位置
end
end
a=2-l*((2)/Max_iteration); % 對每一次迭代,計算相應的a值,a decreases linearly fron 2 to 0
% Update the Position of search agents including omegas
for i=1:size(Positions,1) % 遍歷每個狼
for j=1:size(Positions,2) % 遍歷每個維度
% 包圍獵物,位置更新
r1=rand(); % r1 is a random number in [0,1]
r2=rand(); % r2 is a random number in [0,1]
A1=2*a*r1-a; % 計算系數A,Equation (3.3)
C1=2*r2; % 計算系數C,Equation (3.4)
% Alpha狼位置更新
D_alpha=abs(C1*Alpha_pos(j)-Positions(i,j)); % Equation (3.5)-part 1
X1=Alpha_pos(j)-A1*D_alpha; % Equation (3.6)-part 1
r1=rand();
r2=rand();
A2=2*a*r1-a; % 計算系數A,Equation (3.3)
C2=2*r2; % 計算系數C,Equation (3.4)
% Beta狼位置更新
D_beta=abs(C2*Beta_pos(j)-Positions(i,j)); % Equation (3.5)-part 2
X2=Beta_pos(j)-A2*D_beta; % Equation (3.6)-part 2
r1=rand();
r2=rand();
A3=2*a*r1-a; % 計算系數A,Equation (3.3)
C3=2*r2; % 計算系數C,Equation (3.4)
% Delta狼位置更新
D_delta=abs(C3*Delta_pos(j)-Positions(i,j)); % Equation (3.5)-part 3
X3=Delta_pos(j)-A3*D_delta; % Equation (3.5)-part 3
% 位置更新
Positions(i,j)=(X1+X2+X3)/3;% Equation (3.7)
end
end
l=l+1;
Convergence_curve(l)=Alpha_score;
end
bestc=Alpha_pos(1,1);
bestg=Alpha_pos(1,2);
bestGWOaccuarcy=Alpha_score;
%% 打印參數選擇結果
disp('打印選擇結果');
str=sprintf('Best Cross Validation Accuracy = %g%%,Best c = %g,Best g = %g',bestGWOaccuarcy*100,bestc,bestg);
disp(str)
%% 利用最佳的參數進行SVM網絡訓練
cmd_gwosvm = ['-c ',num2str(bestc),' -g ',num2str(bestg)];
model_gwosvm = svmtrain(train_wine_labels,train_wine,cmd_gwosvm);
%% SVM網絡預測
[predict_label,accuracy] = svmpredict(test_wine_labels,test_wine,model_gwosvm);
% 打印測試集分類準確率
total = length(test_wine_labels);
right = sum(predict_label == test_wine_labels);
disp('打印測試集分類準確率');
str = sprintf( 'Accuracy = %g%% (%d/%d)',accuracy(1),right,total);
disp(str);
%% 結果分析
% 測試集的實際分類和預測分類圖
figure;
hold on;
plot(test_wine_labels,'o');
plot(predict_label,'r*');
xlabel('測試集樣本','FontSize',12);
ylabel('類別標簽','FontSize',12);
legend('實際測試集分類','預測測試集分類');
title('測試集的實際分類和預測分類圖','FontSize',12);
grid on
snapnow
%% 顯示程序運行時間
toc
% This function initialize the first population of search agents
function Positions=initialization(SearchAgents_no,dim,ub,lb)
Boundary_no= size(ub,2); % numnber of boundaries
% If the boundaries of all variables are equal and user enter a signle
% number for both ub and lb
if Boundary_no==1
Positions=rand(SearchAgents_no,dim).*(ub-lb)+lb;
end
% If each variable has a different lb and ub
if Boundary_no>1
for i=1:dim
ub_i=ub(i);
lb_i=lb(i);
Positions(:,i)=rand(SearchAgents_no,1).*(ub_i-lb_i)+lb_i;
end
end
---------------------
作者:Genlovy_Hoo
來源:CSDN
原文:https://blog.csdn.net/u013337691/article/details/52468552
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!
總結
以上是生活随笔為你收集整理的bartlett 算法 matlab,GWO(灰狼优化)算法MATLAB源码逐行中文注解(转载)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用MATLAB函数绘制系统的,用matl
- 下一篇: php sha256hex,crypto