Java教程

【SVM分类】基于狮群算法优化实现SVM数据分类matlab源码

本文主要是介绍【SVM分类】基于狮群算法优化实现SVM数据分类matlab源码,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一、神经网络-支持向量机

支持向量机(Support Vector Machine)是Cortes和Vapnik于1995年首先提出的,它在解决小样本、非线性及高维模式识别中表现出许多特有的优势,并能够推广应用到函数拟合等其他机器学习问题中。 1 数学部分 1.1 二维空间 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ 2 算法部分 ​​ ​​ ​​ 

二、狮群算法

​三、代码

%_________________________________________________________________________%
%狮群算法             %
%_________________________________________________________________________%
function [Best_pos,Best_score,curve]=LSO(pop,Max_iter,lb,ub,dim,fobj)

beta = 0.5;%成年狮所占比列
Nc = round(pop*beta);%成年狮数量
Np = pop-Nc;%幼师数量


if(max(size(ub)) == 1)
   ub = ub.*ones(1,dim);
   lb = lb.*ones(1,dim);  
end

%种群初始化
X0=initialization(pop,dim,ub,lb);
X = X0;
%计算初始适应度值
fitness = zeros(1,pop);
for i = 1:pop
   fitness(i) =  fobj(X(i,:));
end
 [value, index]= min(fitness);%找最小值
GBestF = value;%全局最优适应度值
GBestX = X(index,:);%全局最优位置
curve=zeros(1,Max_iter);
XhisBest = X;
fithisBest = fitness;
indexBest = index; 
gbest = GBestX;
for t = 1: Max_iter
    
    %母狮移动范围扰动因子计算
    stepf = 0.1*(mean(ub) - mean(lb));
    alphaf = stepf*exp(-30*t/Max_iter)^10;
    %幼狮移动范围扰动因子计算
    alpha = (Max_iter - t)/Max_iter;
    %母狮位置更新
   for i = 1:Nc
       index = i;
       while(index == i)
        index = randi(Nc);%随机挑选一只母狮
       end
       X(i,:) = (X(i,:) + X(index,:)).*(1 + alphaf.*randn())./2;
   end
   %幼师位置更新
   for i = Nc+1:pop
       q=rand;
       if q<=1/3
           X(i,:) = (gbest + XhisBest(i,:)).*( 1 + alpha.*randn())/2;
       elseif q>1/3&&q<2/3
           indexT = i;
           while indexT == i
            indexT = randi(Nc) + pop - Nc;%随机位置
           end
           
            X(i,:) = (X(indexT,:) + XhisBest(i,:)).*( 1 + alpha.*randn())/2;          
       else
           gbestT = ub + lb - gbest;
            X(i,:) = (gbestT + XhisBest(i,:)).*( 1 + alpha.*randn())/2; 
       end     
   end 
   %边界控制
   for j = 1:pop
       for a = 1: dim
           if(X(j,a)>ub)
               X(j,a) =ub(a);
           end
           if(X(j,a)<lb)
               X(j,a) =lb(a);
           end
       end
   end 
   %计算适应度值
   for j=1:pop
    fitness(j) = fobj(X(j,:));
   end
   for j = 1:pop
    if(fitness(j)<fithisBest(j))
        XhisBest(j,:) = X(j,:);
        fithisBest(j) = fitness(j);
    end
    if(fitness(j) < GBestF)
        GBestF = fitness(j);
        GBestX = X(j,:);   
        indexBest = j;
    end
   end
   %% 狮王更新
   Temp = gbest.*(1 + randn().*abs(XhisBest(indexBest,:) - gbest));
   Temp(Temp>ub)=ub(Temp>ub);
   Temp(Temp<lb) = lb(Temp<lb);
   fitTemp = fobj(Temp);
   if(fitTemp<GBestF)
       GBestF =fitTemp;
       GBestX = Temp;
       X(indexBest,:)=Temp;
       fitness(indexBest) = fitTemp;
   end
   [value, index]= min(fitness);%找最小值
   gbest = X(index,:);%当前代,种群最优值
   curve(t) = GBestF;
end
Best_pos = GBestX;
Best_score = curve(end);
end



在这里插入图片描述

5.参考文献:

书籍《MATLAB神经网络43个案例分析》

 

这篇关于【SVM分类】基于狮群算法优化实现SVM数据分类matlab源码的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!