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) Convergence_curve(l)=Alpha_score; 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); %% 结果分析
% 测试集的实际分类和预测分类图 plot(test_wine_labels,'o'); plot(predict_label,'r*');
xlabel('测试集样本','FontSize',12); ylabel('类别标签','FontSize',12);
legend('实际测试集分类','预测测试集分类');
title('测试集的实际分类和预测分类图','FontSize',12); %% 显示程序运行时间
% 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; % If each variable has a different lb and ub if Boundary_no1 for i=1:dim ub_i=ub(i); lb_i=lb(i);
Positions(:,i)=rand(SearchAgents_no,1).*(ub_i-lb_i)+lb_i; beta_max=para(4); % 缩放因子上界 Upper Bound of Scaling Factor
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
探狼:将解空间中除了头狼之外的最佳的S_num头人工狼,在猎物的活动范围内游动,根据空气中猎物留下的气味进行自主决策,气味越浓表明狼距离猎物越近,探狼始终朝着气味最浓的方向搜寻 test_wine
[wine(31:59,:);wine(96:130,:);wine(154:178,:)];
semilogy(GWO_cg_curve,'Color','r') %y轴以10为底的对数形式与x对应
钟一文.?智能优化方法及其应用研究[D].浙江大学,2005. Positions(i,j)=(X1+X2+X3)-3;% Equation (3.7)
Max_iteration=10; % 最大迭代次数,Maximum numbef of iterations
=
(1)归一化:The fuzzy method has been implemented to normalize the objective functions which are not in the same range.
v[\+ str(l+1)] = beta * v[\+ str(l + 1)] + (1 - beta) * grads[\