以优化SVM算法的参数c和g为例,对FA(萤火虫算法)MATLAB源码进行了逐行中文注解。
完整程序和示例文件地址:http://download.csdn.net/detail/u013337691/9626263
链接:http://pan.baidu.com/s/1kVbd5cV 密码:7ym8

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),: );
%% FA优化参数
% 参数向量 parameters [n N_iteration alpha betamin gamma]
% n为种群规模,N_iteration为迭代次数
para=[10,50,0.5,0.2,1];% 待优化参数上下界 Simple bounds/limits for d-dimensional problems
d=2; % 待优化参数个数
Lb=[0.01,0.01]; % 下界
Ub=[100,100]; % 上界% 参数初始化 Initial random guess
u0=Lb+(Ub-Lb).*rand(1,d);% 迭代寻优 Display results
[bestsolutio,bestojb]=ffa_mincon_svm(@objfun_svm,u0,Lb,Ub,para,train_wine_labels,train_wine,test_wine_labels,test_wine);
%% 打印参数选择结果
bestc=bestsolutio(1);
bestg=bestsolutio(2);disp('打印选择结果');
str=sprintf('Best c = %g,Best g = %g',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
%% 显示程序运行时间
toc
% 萤火虫算法主程序开始 Start FA
function [nbest,fbest]=ffa_mincon_svm(costfhandle,u0, Lb, Ub, para,train_wine_labels,train_wine,test_wine_labels,test_wine)
% 检查输入参数 Check input parameters (otherwise set as default values)
if nargin<5para=[20 100 0.25 0.20 1];
end
if nargin<4Ub=[];
end
if nargin<3Lb=[];
end
if nargin<2disp('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 size
if length(Lb) ~=length(Ub)disp('Simple bounds/limits are improper!')return
end% 计算待优化参数维度 Calcualte dimension
d=length(u0);% 初始化目标函数值 Initial values of an array
zn=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 optionalalpha=alpha_new(alpha,MaxGeneration);% 对每个萤火虫计算目标函数值 Evaluate new solutions (for all n fireflies)
for i=1:nzn(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:nns(i,:)=ns_tmp(Index(i),:);
end%% 找出当前最优 Find the current best
nso=ns;
Lighto=Lightn;
nbest=ns(1,:);
Lightbest=Lightn(1);% 另存最优值 For output only
fbest=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 fireflies
function [ns,Lightn]=init_ffa(n,d,Lb,Ub,u0)
ns=zeros(n,d);
if ~isempty(Lb) % 如果参数界限不为空 if there are bounds/limitsfor i=1:nns(i,:)=Lb+(Ub-Lb).*rand(1,d); % 则在取值范围内随机取值end
else % 如果没有设置参数界限for i=1:nns(i,:)=u0+randn(1,d); % 在原有参数上加白噪声end
end
% 初始化目标函数 initial value before function evaluations
Lightn=ones(n,1)*10^100;% Move all fireflies toward brighter ones
function [ns]=ffa_move(n,d,ns,Lightn,nso,Lighto,alpha,betamin,gamma,Lb,Ub)
% 参数取值范围绝对值 Scaling of the system
scale=abs(Ub-Lb);% 更新萤火虫 Updating fireflies
for i=1:n% The attractiveness parameter beta=exp(-gamma*r)for j=1:nr=sqrt(sum((ns(i,:)-ns(j,:)).^2));% Update movesif Lightn(i)>Lighto(j) % 如果i比j亮度更强 Brighter and more attractivebeta0=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;endend % end for j
end % 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.9
delta=1-(10^(-4)/0.9)^(1/NGen);
alpha=(1-delta)*alpha;% 防止越界 Make sure the fireflies are within the bounds/limits
function [ns]=findlimits(n,ns,Lb,Ub)
for i=1:n% Apply the lower boundns_tmp=ns(i,:);I=ns_tmp<Lb;ns_tmp(I)=Lb(I);% Apply the upper boundsJ=ns_tmp>Ub;ns_tmp(J)=Ub(J);% Update this new movens(i,:)=ns_tmp;
end
%% SVM_Objective Function
function 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胡说

FA(萤火虫算法)MATLAB源码详细中文注解相关推荐

  1. bartlett 算法 matlab,GWO(灰狼优化)算法MATLAB源码逐行中文注解(转载)

    以优化SVM算法的参数c和g为例,对GWO算法MATLAB源码进行了逐行中文注解. tic % 计时器 %% 清空环境变量 close all clear clc format compact %% ...

  2. GWO(灰狼优化)算法MATLAB源码逐行中文注解

    以优化SVM算法的参数c和g为例,对GWO算法MATLAB源码进行了逐行中文注解. 完整程序和示例文件地址:http://download.csdn.net/detail/u013337691/962 ...

  3. GWO(灰狼优化)算法MATLAB源码逐行中文注解()

    以优化SVM算法的参数c和g为例,对GWO算法MATLAB源码进行了逐行中文注解. tic % 计时器 %% 清空环境变量 close all clear clc format compact %% ...

  4. CS(布谷鸟搜索)算法MATLAB源码逐行中文注解

    以优化SVM算法的参数c和g为例,对CS算法MATLAB源码进行了逐行中文注解. 完整程序和示例文件地址:http://download.csdn.net/detail/u013337691/9622 ...

  5. 灰狼优化算法求函数最优值matlab,GWO(灰狼优化)算法MATLAB源码逐行中文注解(转载)...

    2-l*((2)/Max_iteration); % 对每一次迭代,计算相应的a值,a decreases linearly fron 2 to 0% Update the Position of s ...

  6. 分裂层次聚类matlab实现,凝聚层次聚类算法matlab源码

    <凝聚层次聚类算法matlab源码>由会员分享,可在线阅读,更多相关<凝聚层次聚类算法matlab源码(3页珍藏版)>请在人人文库网上搜索. 1.共享一个在数据挖掘课程中作为示 ...

  7. mean shift聚类matlab,meanshift目标跟踪源程序 meanshift跟踪算法MATLAB源码 - 下载 - 搜珍网...

    压缩包 : MeanShift+深入详细(MatLab源码).rar 列表 MeanShift+深入详细(MatLab源码)/meanshift文章.PPT.word文档.基于meanshift的跟踪 ...

  8. ABC(智能蜂群算法)优化SVM_源码逐行中文注解

    ​最近发现要彻底.快速地弄懂一个算法,最好的办法就是找源码来,静下心,一行一行的学习.所以我把ABC算法的源码找来逐行做了中文注释,并以优化SVM参数为例,进行学习. 废话不多说,直接上MATLAB代 ...

  9. Metropolis-Hasting算法Matlab源码学习

    Part1:Matlab源码: clc;clear all;close all; X(1)=0; N=1e7; p=@(x) 5*normpdf(x,1)+4*normpdf(x,5)+5*normp ...

最新文章

  1. html地图自动适合窗口,【整理】用html和javascript实现类似百度地图的画布
  2. 【转】用例结构优化心得
  3. Wowza Media Server媒体服务器应用
  4. Linux命令工具基础02 文件及目录管理
  5. C#比较dynamic和Dictionary性能
  6. CSV文件的转义处理
  7. linux Wi-Fi信号放大,wifi信号增强器
  8. Lesson 3 Part 1 Locally weighted regression
  9. MPLS virtual private network 地址重叠实验(华为设备)
  10. 决定了 [2007-10-11]
  11. 【C语言】输出100内素数
  12. Trime同文输入法
  13. 汇总一下那几个常用定理 高斯定理 泊松方程 亚阈值电流 跨导
  14. AC2100 OpenWrt 多拨
  15. 使用 Visual Studio 2005中的ASP.NET 移动控件创建电子书浏览器应用程序
  16. java环信后端接口
  17. jquery中的mouseover、mouseout 和 mouseenter 、 mouseleave的区别
  18. 大姐夫再冲世界首富,亚马逊HQ2的赢家已经初现。。。
  19. JAVAWEB常用测试浏览器
  20. Lua 报错 PANIC: unprotected error in call to Lua API (no calling environment) 解决办法

热门文章

  1. php 多核cpu,paip.提升性能--多核cpu中的java/.net/php/c++编程
  2. 电脑连不上网,浏览器网页打不开,但qq微信能发消息
  3. Flask教程(二十)flask-apscheduler
  4. 常用室内定位技术总结
  5. 创建XTP图表的方法
  6. Nouveau源码分析(三):NVIDIA设备初始化之nouveau_drm_probe
  7. VVC帧内预测(六)MIP
  8. 平时你都用什么软件做笔记?
  9. 测试人生 | 为了娃的奶粉钱,测试媛妈妈拿出考研的拼劲,半年终圆大厂梦
  10. egg mysql 模糊搜索_使用egg-mysql操作mysql数据库