首页 > 编程知识 正文

用matlab编写fibonacci数列,蚁群算法matlab代码

时间:2023-05-04 23:59:17 阅读:19596 作者:1208

以优化SVM算法的参数c和g为例,对萤火虫算法(FA ) MATLAB源代码逐行进行中文注释。

程序和示例文件的完整地址: http://download.csdn.net/detail/u 013337691/9626263

链接: http://pan.baidu.com/s/1kVbd5cV密码:7ym8

tic %计时器%%环境变量closeallclearclcformatcompact % %数据提取%将加载到测试数据wine中。 其中包含的数据是classnumber=3,wine:178*13的矩阵。 wine_labes:178*1列向量load wine.mat%选择训练集和测试集%训练第一类1-30、第二类60-95和第三类131-153 wine (131:153, ); % train _ wine _ labels=[ wine _ labels (1336030 ) ],同时分离相应训练集的标签; wine_labels(60:95; wine_labels(1313360153 ); %测试第一类31-59、第二类96-130、第三类154-178集test _ wine=[ wine (31336059, ) ]; wine(963360130、 wine (1543360178, ); %相应测试集的标签也必须分离test _ wine _ labels=[ wine _ labels (31336059 ); wine_labels(96:130; wine_labels(1543360178 ); %%数据预处理%将数据预处理、训练集和测试集规范化为[ 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: m train, ); test _ wine=dataset _ scale ((m train1) : ) mtrainmtest ), ); %% FA优化参数%参数向量parameters [ nn_iterationalphabetamingamma ] % n为种群规模,n _ iteration为迭代次数para=[ 10,50,0.5,] %待优化参数上下边界simple bounds/limits Ford-dimensional problems d=2; %要优化的参数lb=[ 0.01,0.01 ]; %下界ub=[ 100,100 ]; %上限%参数初始化initialrandomGuessu0=lb(UB-lb ).*rand(1,d ); %迭代搜索优Display results[bestsolutio,bestojb ]=FFA _ min con _ SVM (obj fun _ SVM,u0,Lb,Ub,para,train_wine () ) bestg=bestsolutio(2; disp (打印选择结果); str=sprintf('bestc=%g,Best g=%g ',bestc,bestg ); DISP(str ) % )利用最佳参数的SVM网络培训cmd_gwosvm=['-c ',num2str(bestc ),'-g ',num2str(bestc ) ]; model _ gwo SVM=SVM train (train_wine _ labels,train _ wine,cmd_gwosvm ); %% SVM网络预测[predict_label,accuracy ]=SVM predict (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 ); 磁盘(str; %%结果分析%测试集的实际分类和预测分类图figure; 保持接通; plot(test_wine_labels,' o '; plot(predict_label,' r* '; xlabel (“测试集样本”、“FontSize”、12 ); ylabel (“类别标签”、“字体”、12 ); legend (“实际测试集分类”、“预测测试集分类”); title ()测试集的实际分类和预测分类图)、(FontSize )、12 ); grid on%%显示程序执行时间toc %萤火虫算法主程序开始Start FAfunction [nbest,FBest]=FFA_mincon_SVM(c

ostfhandle,u0, Lb, Ub, para,train_wine_labels,train_wine,test_wine_labels,test_wine)% 检查输入参数 Check input parameters (otherwise set as default values)if nargin<5 para=[20 100 0.25 0.20 1];endif nargin<4 Ub=[];endif nargin<3 Lb=[];endif nargin<2 disp('Usuage: FA_mincon(@cost,u0,Lb,Ub,para)');end% n=number of fireflies% MaxGeneration=number of pseudo time steps% ------------------------------------------------% alpha=0.25; % Randomness 0--1 (highly random)% betamn=0.20; % minimum value of beta% gamma=1; % Absorption coefficient% ------------------------------------------------n=para(1);MaxGeneration=para(2);alpha=para(3);betamin=para(4);gamma=para(5);% 检查上界向量与下界向量长度是否相同 Check if the upper bound & lower bound are the same sizeif length(Lb) ~=length(Ub) disp('Simple bounds/limits are improper!') returnend% 计算待优化参数维度 Calcualte dimensiond=length(u0);% 初始化目标函数值 Initial values of an arrayzn=ones(n,1)*10^100;% ------------------------------------------------% 初始化萤火虫位置 generating the initial locations of n fireflies[ns,Lightn]=init_ffa(n,d,Lb,Ub,u0);for k=1:MaxGeneration % 迭代开始% 更新alpha(可选)This line of reducing alpha is optional alpha=alpha_new(alpha,MaxGeneration);% 对每个萤火虫计算目标函数值 Evaluate new solutions (for all n fireflies)for i=1:n zn(i)=costfhandle(ns(i,:),train_wine_labels,train_wine,test_wine_labels,test_wine); Lightn(i)=zn(i);end% 根据亮度排序 Ranking fireflies by their light intensity/objectives[Lightn,Index]=sort(zn);ns_tmp=ns;for i=1:n ns(i,:)=ns_tmp(Index(i),:);end%% 找出当前最优 Find the current bestnso=ns;Lighto=Lightn;nbest=ns(1,:);Lightbest=Lightn(1);% 另存最优值 For output onlyfbest=Lightbest;% 向较优方向移动 Move all fireflies to the better locations[ns]=ffa_move(n,d,ns,Lightn,nso,Lighto,alpha,betamin,gamma,Lb,Ub);end% ----- All the subfunctions are listed here ------------% 初始化萤火虫位置 The initial locations of n firefliesfunction [ns,Lightn]=init_ffa(n,d,Lb,Ub,u0)ns=zeros(n,d);if ~isempty(Lb) % 如果参数界限不为空 if there are bounds/limits for i=1:n ns(i,:)=Lb+(Ub-Lb).*rand(1,d); % 则在取值范围内随机取值 endelse % 如果没有设置参数界限 for i=1:n ns(i,:)=u0+randn(1,d); % 在原有参数上加白噪声 endend% 初始化目标函数 initial value before function evaluationsLightn=ones(n,1)*10^100;% Move all fireflies toward brighter onesfunction [ns]=ffa_move(n,d,ns,Lightn,nso,Lighto,alpha,betamin,gamma,Lb,Ub)% 参数取值范围绝对值 Scaling of the systemscale=abs(Ub-Lb);% 更新萤火虫 Updating firefliesfor i=1:n % The attractiveness parameter beta=exp(-gamma*r) for j=1:n r=sqrt(sum((ns(i,:)-ns(j,:)).^2)); % Update moves if Lightn(i)>Lighto(j) % 如果i比j亮度更强 Brighter and more attractive beta0=1; beta=(beta0-betamin)*exp(-gamma*r.^2)+betamin; tmpf=alpha.*(rand(1,d)-0.5).*scale; ns(i,:)=ns(i,:).*(1-beta)+nso(j,:).*beta+tmpf; end end % end for jend % end for i% 防止越界 Check if the updated solutions/locations are within limits[ns]=findlimits(n,ns,Lb,Ub);% This function is optional, as it is not in the original FA% The idea to reduce randomness is to increase the convergence,% however, if you reduce randomness too quickly, then premature% convergence can occur. So use with care.% alpha参数更新函数 function alpha=alpha_new(alpha,NGen)% alpha_n=alpha_0(1-delta)^NGen=10^(-4);% alpha_0=0.9delta=1-(10^(-4)/0.9)^(1/NGen);alpha=(1-delta)*alpha;% 防止越界 Make sure the fireflies are within the bounds/limitsfunction [ns]=findlimits(n,ns,Lb,Ub)for i=1:n % Apply the lower bound ns_tmp=ns(i,:); 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 ns(i,:)=ns_tmp;end %% SVM_Objective Functionfunction f=objfun_svm(cv,train_wine_labels,train_wine,test_wine_labels,test_wine)% cv为长度为2的横向量,即SVM中参数c和v的值cmd = [' -c ',num2str(cv(1)),' -g ',num2str(cv(2))];model=svmtrain(train_wine_labels,train_wine,cmd); % SVM模型训练[~,fitness]=svmpredict(test_wine_labels,test_wine,model); % SVM模型预测及其精度f=1-fitness(1)/100; % 以分类预测错误率作为优化的目标函数值

欢迎关注:一本正经d胡说

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。