粒子群算法(PSO)优化支持向量机原理及Matlab代码

目录

0 引言

1 数学模型

2 优化方式

3 Matlab代码

3.1 伪代码

3.2 PSO主函数代码

3.3 PSO-SVM

4 视频讲解

0 引言

粒子群优化算法(Particle swarm optimization,PSO)是1995 年由Eberhart 博士和kennedy 博士提出进化群智能算法,源于对信息循环和鸟群捕食的行为研究 。该算法是对鸟类群体中的个体对信息的共享,使整个群体的运动在问题求解空间中产生从无序到有序的演化过程,从而获得最优解。

1 数学模型

PSO数学模型主要将鸟群抽象看成粒子形式,并赋予其位置和速度这俩个属性进行鸟群捕食时位置的产生,见其与猎物的差距赋予适应度表示,从而不断更新其自身位置。主要进行下式模型:

1)初始化:初始化粒子的速度、位置、个体最优适应度、全体最优适应度。

式(2)为种群位置初始值,其中i为种群数,D为数学维度,式(1)位置最优适应度值和目标适应度对比,选出个体和全局最有个体,速度初始值假设为一致数值=。

2)粒子位置更新:粒子的运动方程轨迹。

3)粒子速度更新:速度向量控制着粒子在搜索空间中移动的方式,其中该方程主要由记忆项、自身认知项和群体认知项组成。

式C1为个体学习因子,C2为群体的学习因子,P为个体最优位置,g为群体最优位置,R1R2为随机因子。

3)PSO速度标准形式:基于上述研究,可以法上速度更新容易出现速度爆炸形式,通过引入惯性因子调整速度动态形式,其式子如下:

4)边界处理:粒子移动到超过搜索空间的极限时需引入了一个速度阈值,其数学模型见下式:

2 优化方式

前篇对支持向量机(支持向量机原理及matlab代码讲解(分类SVM和回归SVR)-CSDN博客)原理讲解,从支持向量机模型运算过程中,可以了解到模型高维映射核函数参数g和处罚因子c对模型预测结果影响最为重要。因此结合上述PSO原理介绍,可以将支持向量机参数c和g作为鸟类种群,每一个种群对应支持向量机的预测值,将这个预测值作为适应度进行鸟类粒子的上述活动。

3 Matlab代码

3.1 伪代码

3.2 PSO主函数代码

%%
%SYD  适应度函数
%dim  问题维度
%nPop 种群
%MaxIt 最大迭代次数

% PSO参数
CostFunction=@(x) SYD(x);        
nVar=dim;           
VarSize=[1 nVar];   
VarMin=[0.001,5,0.0001];       
VarMax= [0.01,20,0.01];       

w=1;            % 惯性权重
wdamp=0.99;     % 惯性权重阻尼比
c1=1.5;         % 个体学习因子
c2=2.0;         % 全体学习因子

VelMax=0.1*(VarMax-VarMin);
VelMin=-VelMax;

%% 初始化
empty_particle.Position=[];
empty_particle.Cost=[];
empty_particle.Velocity=[];
empty_particle.Best.Position=[];
empty_particle.Best.Cost=[];
particle=repmat(empty_particle,nPop,1);
GlobalBest.Cost=inf;

for i=1:nPop
    
    % 初始化种群位置
    particle(i).Position=unifrnd(VarMin,VarMax,VarSize);
    
    % 初始化速度
    particle(i).Velocity=zeros(VarSize);
    
    % 初始化适应度
    particle(i).Cost=CostFunction(particle(i).Position);
    
    % 更新个体最优位置
    particle(i).Best.Position=particle(i).Position;
    particle(i).Best.Cost=particle(i).Cost;
    
    % 更新全局最优位置
    if particle(i).Best.Cost<GlobalBest.Cost
        
        GlobalBest=particle(i).Best;
        
    end
    
end
BestCost=zeros(MaxIt,1);

%% PSO主函数
for it=1:MaxIt
    
    for i=1:nPop
        
        % 更新速度
        particle(i).Velocity = w*particle(i).Velocity ...
            +c1*rand(VarSize).*(particle(i).Best.Position-particle(i).Position) ...
            +c2*rand(VarSize).*(GlobalBest.Position-particle(i).Position);
        
        % 速度边界
        particle(i).Velocity = max(particle(i).Velocity,VelMin);
        particle(i).Velocity = min(particle(i).Velocity,VelMax);
        
        % 更新位置
        particle(i).Position = particle(i).Position + particle(i).Velocity;
        
        % 超边界处理
        IsOutside=(particle(i).Position<VarMin | particle(i).Position>VarMax);
        particle(i).Velocity(IsOutside)=-particle(i).Velocity(IsOutside);
        
        % 位置限制
        particle(i).Position = max(particle(i).Position,VarMin);
        particle(i).Position = min(particle(i).Position,VarMax);
        
        % 适应度
        particle(i).Cost = CostFunction(particle(i).Position);
        
        % 更新个体最优位置
        if particle(i).Cost<particle(i).Best.Cost
            
            particle(i).Best.Position=particle(i).Position;
            particle(i).Best.Cost=particle(i).Cost;
            
            % 更新全局最优位置
            if particle(i).Best.Cost<GlobalBest.Cost
                
                GlobalBest=particle(i).Best;
                
            end
            
        end
        
    end
    
    BestCost(it)=GlobalBest.Cost;
    
    w=w*wdamp;
    
end

3.3 PSO-SVM

1)回归模型:回归模型:粒子群算法优化支持向量机模型(PSO-SVR)

2)分类模型:分类模型:粒子群算法优化支持向量机模型(PSO-SVM)

4 视频讲解

B站搜索:‘不想学习的陈成’

  • 7
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
This add-in to the PSO Research toolbox (Evers 2009) aims to allow an artificial neural network (ANN or simply NN) to be trained using the Particle Swarm Optimization (PSO) technique (Kennedy, Eberhart et al. 2001). This add-in acts like a bridge or interface between MATLAB’s NN toolbox and the PSO Research Toolbox. In this way, MATLAB’s NN functions can call the NN add-in, which in turn calls the PSO Research toolbox for NN training. This approach to training a NN by PSO treats each PSO particle as one possible solution of weight and bias combinations for the NN (Settles and Rylander ; Rui Mendes 2002; Venayagamoorthy 2003). The PSO particles therefore move about in the search space aiming to minimise the output of the NN performance function. The author acknowledges that there already exists code for PSO training of a NN (Birge 2005), however that code was found to work only with MATLAB version 2005 and older. This NN-addin works with newer versions of MATLAB till versions 2010a. HELPFUL LINKS: 1. This NN add-in only works when used with the PSORT found at, http://www.mathworks.com/matlabcentral/fileexchange/28291-particle-swarm-optimization-research-toolbox. 2. The author acknowledges the modification of code used in an old PSO toolbox for NN training found at http://www.mathworks.com.au/matlabcentral/fileexchange/7506. 3. User support and contact information for the author of this NN add-in can be found at http://www.tricia-rambharose.com/ ACKNOWLEDGEMENTS The author acknowledges the support of advisors and fellow researchers who supported in various ways to better her understanding of PSO and NN which lead to the creation of this add-in for PSO training of NNs. The acknowledged are as follows: * Dr. Alexander Nikov - Senior lecturer and Head of Usaility Lab, UWI, St. Augustine, Trinidad, W.I. http://www2.sta.uwi.edu/~anikov/ * Dr. Sabine Graf - Assistant Professor, Athabasca University, Alberta, Canada. http://scis.athabascau.ca/scis/staff/faculty.jsp?id=sabineg * Dr. Kinshuk - Professor, Athabasca University, Alberta, Canada. http://scis.athabascau.ca/scis/staff/faculty.jsp?id=kinshuk * Members of the iCore group at Athabasca University, Edmonton, Alberta, Canada.
以下是粒子群算法优化支持向量机Matlab 代码示例: 首先,我们需要准备一些数据集并将其划分为训练集和测试集: ```matlab % 准备数据 load fisheriris X = meas(:,1:2); Y = species; Y = grp2idx(Y); trainRatio = 0.7; c = cvpartition(Y,'HoldOut',trainRatio); Xtrain = X(c.training,:); Ytrain = Y(c.training,:); Xtest = X(c.test,:); Ytest = Y(c.test,:); ``` 然后,我们可以使用 PSO 优化 SVM 的参数: ```matlab % PSO 优化 SVM 参数 options = optimoptions('particleswarm','MaxIterations',50); lb = [1e-6,1e-6]; ub = [1e6,1e6]; fun = @(c)svmtrain(Ytrain,Xtrain,sprintf('-c %f -g %f -q',c(1),c(2))); [c_opt,~] = particleswarm(fun,2,lb,ub,options); ``` 最后,我们可以使用优化得到的参数训练 SVM 模型并进行预测: ```matlab % 训练 SVM 模型 model = svmtrain(Ytrain,Xtrain,sprintf('-c %f -g %f -q',c_opt(1),c_opt(2))); % 预测测试集并计算准确率 [predict_label, accuracy, prob_estimates] = svmpredict(Ytest, Xtest, model); disp(accuracy(1)) ``` 完整的代码示例如下: ```matlab % 准备数据 load fisheriris X = meas(:,1:2); Y = species; Y = grp2idx(Y); trainRatio = 0.7; c = cvpartition(Y,'HoldOut',trainRatio); Xtrain = X(c.training,:); Ytrain = Y(c.training,:); Xtest = X(c.test,:); Ytest = Y(c.test,:); % PSO 优化 SVM 参数 options = optimoptions('particleswarm','MaxIterations',50); lb = [1e-6,1e-6]; ub = [1e6,1e6]; fun = @(c)svmtrain(Ytrain,Xtrain,sprintf('-c %f -g %f -q',c(1),c(2))); [c_opt,~] = particleswarm(fun,2,lb,ub,options); % 训练 SVM 模型 model = svmtrain(Ytrain,Xtrain,sprintf('-c %f -g %f -q',c_opt(1),c_opt(2))); % 预测测试集并计算准确率 [predict_label, accuracy, prob_estimates] = svmpredict(Ytest, Xtest, model); disp(accuracy(1)) ``` 注意:以上代码仅为示例,实际应用中需要根据具体问题进行调整和优化
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值