编程语言
首页 > 编程语言> > 【SVM预测】基于萤火虫算法改进SVM算法实现数据分类matlab源码

【SVM预测】基于萤火虫算法改进SVM算法实现数据分类matlab源码

作者:互联网

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

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

二、萤火虫算法

算法基本思想描述如下:在群体中,每个萤火虫个体被随机分布在目标函数定义的空间中,初始阶段,所有的萤火虫都具有相同的荧光素值和动态决策半径。其中,每个萤火虫个体根据来自动态决策半径内所有邻居萤火虫信号的强弱来决定其移动的方向。萤火虫的动态决策半径会随着在它范围内萤火虫个体的数目而变化,每个萤火虫的荧光素也会随着决策半径内萤火虫个体的数目而改变。萤火虫群优化算法是无记忆的,无需目标函数的全局信息和梯度信息,具有计算速度快,调节参数少,易于实现等特点。萤火虫进化过程中,每次迭代都由萤火虫的部署(初始化)荧光素更新阶段移动概率计算阶段位置更新阶段邻域范围更新阶段五个部分组成,现分别介绍如下:

1、萤火虫的部署(初始化)

40686c1eb5e0a1fc209544f863628ad1.png9afbb9c3ac46f7f33a7729ae0f8d23a9.gifc5f7e55c1f0c1895e68bb866d4202c58.pnge0de5ba2a8e0e856910160a60d54645d.gif​2、荧光素更新阶段

f060aca115de4ff02a41538b3e084fde.png9afbb9c3ac46f7f33a7729ae0f8d23a9.gif​​8fb9ab8600001dd1c52a15c2f2f578ec.gif​3、移动概率计算阶段

259200f1e4c81127d97a1a05e7b03334.png8fb9ab8600001dd1c52a15c2f2f578ec.gif7644bfe0f214d8498b8a0f730f16feca.png9afbb9c3ac46f7f33a7729ae0f8d23a9.gif​4、位置更新阶段

9cefa37435dd01bd39545d49916dddee.pnge0de5ba2a8e0e856910160a60d54645d.gif​​e0de5ba2a8e0e856910160a60d54645d.gif​5、邻域范围更新阶段

2b15f5507897eacf3a60439369ef7197.png

​三、代码


% The Whale Optimization Algorithm
function [Leader_score,Leader_pos,Convergence_curve]=WOA(SearchAgents_no,Max_iter,lb,ub,dim,fobj)

% initialize position vector and score for the leader
Leader_pos=zeros(1,dim);
Leader_score=inf; %change this to -inf for maximization problems


%Initialize the positions of search agents
% Positions=initialization(SearchAgents_no,dim,ub,lb);
Positions=ceil(rand(SearchAgents_no,dim).*(ub-lb)+lb);

Convergence_curve=zeros(1,Max_iter);

t=0;% Loop counter

% Main loop
while t<Max_iter
    for i=1:size(Positions,1)
        
        % Return back the search agents that go beyond the boundaries of the search space
        Flag4ub=Positions(i,:)>ub;
        Flag4lb=Positions(i,:)<lb;
        Positions(i,:)=(Positions(i,:).*(~(Flag4ub+Flag4lb)))+ub.*Flag4ub+lb.*Flag4lb;
        
        % Calculate objective function for each search agent
        fitness=fobj(Positions(i,:));
        
        % Update the leader
        if fitness<Leader_score % Change this to > for maximization problem
            Leader_score=fitness; % Update alpha
            Leader_pos=Positions(i,:);
        end
        
    end
    
    a=2-t*((2)/Max_iter); % a decreases linearly fron 2 to 0 in Eq. (2.3)
    
    % a2 linearly dicreases from -1 to -2 to calculate t in Eq. (3.12)
    a2=-1+t*((-1)/Max_iter);
    
    % Update the Position of search agents 
    for i=1:size(Positions,1)
        r1=rand(); % r1 is a random number in [0,1]
        r2=rand(); % r2 is a random number in [0,1]
        
        A=2*a*r1-a;  % Eq. (2.3) in the paper
        C=2*r2;      % Eq. (2.4) in the paper
        
        
        b=1;               %  parameters in Eq. (2.5)
        l=(a2-1)*rand+1;   %  parameters in Eq. (2.5)
        
        p = rand();        % p in Eq. (2.6)
        
        for j=1:size(Positions,2)
            
            if p<0.5   
                if abs(A)>=1
                    rand_leader_index = floor(SearchAgents_no*rand()+1);
                    X_rand = Positions(rand_leader_index, :);
                    D_X_rand=abs(C*X_rand(j)-Positions(i,j)); % Eq. (2.7)
                    Positions(i,j)=X_rand(j)-A*D_X_rand;      % Eq. (2.8)
                    
                elseif abs(A)<1
                    D_Leader=abs(C*Leader_pos(j)-Positions(i,j)); % Eq. (2.1)
                    Positions(i,j)=Leader_pos(j)-A*D_Leader;      % Eq. (2.2)
                end
                
            elseif p>=0.5
              
                distance2Leader=abs(Leader_pos(j)-Positions(i,j));
                % Eq. (2.5)
                Positions(i,j)=distance2Leader*exp(b.*l).*cos(l.*2*pi)+Leader_pos(j);
                
            end
            
        end
    end
    t=t+1;
    Convergence_curve(t)=Leader_score;
%     [t Leader_score]
end



7e0023dc75fdf39db3e73fd1bb926983.png

5.参考文献:

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

标签:rand,SVM,score,Positions,Leader,算法,源码,Eq,萤火虫
来源: https://blog.51cto.com/u_15287693/3034537