首页 > 编程语言 >海马算法(SHO)优化支持向量机原理及Matlab代码

海马算法(SHO)优化支持向量机原理及Matlab代码

时间:2024-09-03 10:26:20浏览次数:7  
标签:horses 海马 pop Matlab Sea new1 向量 SHO

目录

0 引言

1 数学模型

2 优化方式

3 Maltab代码

3.1 伪代码

3.2 SHO主函数代码

3.3 SHO-SVM

4 视频讲解

0 引言

海马算法(Sea Horse Optimizer,SHO)是Shijie Zhao于2023年基于自然界中海马的运动、捕食和繁殖行为提出的群体智能算法。SHO算法两个阶段,SHO分别模拟了海马的不同运动模式和概率捕食机制。此外算法设计为在保持雌雄较好信息的同时繁殖后代,有利于增加种群多样性。

1 数学模型

SOS数学模型主要对海马的运动、捕食和繁殖进行构建模型。而海马的运动模式分为受海洋涡旋作用影响的浮动螺旋模式和沿当前波的漂移模式。对于捕食策略模拟了海马以一定的概率捕获猎物的成败。繁殖阶段保持正信息的情况下进行后代繁殖。

1)初始化:SHO作为群智能算法,也是从总体初始化开始,采用随机参数进行产生种群位置。

式中Xi为海马种群位置,pop为种群数量,Dim为问题维度,LB,UB为问题维度边界,Xelite为精英海马个体,为适应度最小海马,f(*)为海马*对应的适应度。

2)海马的运动行为:海马运动行为主要有俩种:受海洋涡旋作用影响的浮动螺旋模式和沿当前波的漂移模式,数学模型分别以加入莱维飞行策略和布朗运动来模拟,其数学模型如下:

2.1)受海洋涡旋作用影响的浮动螺旋模式:式中Levy为莱维飞行分布函数,s为固定常数0.001,w,k为0到1的随机数,入为0到2的随机数,Γ为阶乘,xyz为三维搜索向量,分别为x=ρ×cos(θ), y=ρ×sin(θ) 和z=ρ×θ。

2.2)沿当前波的漂移模式:式中l为常数系数0.05,β为布朗运动的随机游走系数,符合正态分布。

下图是二者运动趋势:

3)海马捕食行为:海马捕食浮游动物和小型甲壳类动物有两种结果:成功和失败。考虑到海马成功捕获食物的概率超过90%,我们设计了SHO的随机数r2来区分这两个结果,如下:

式中α随迭代而线性减少,以调整海马的移动步长,t为当前迭代,T为最大迭代,r2为0到1的随机数

4)海马的繁殖行为:海马通过适合度值将其分为男性和女性组。雄性和雌性被随机交配以产生新的后代,如下:

2 优化方式

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

3 Maltab代码

3.1 伪代码

3.2 SHO主函数代码

%% 参数
% initialization 初始化函数
% pop 种群大小
% Dim 问题维度
% UB,LB 问题维度边界
% Max_iter 最大迭代次数
% levy 飞行策略函数
% SYD 适应度函数


% 初始化
Sea_horses=initialization(pop,Dim,UB,LB);%海马种群
Sea_horsesFitness = zeros(1,pop);

fitness_history=zeros(pop,Max_iter);
population_history=zeros(pop,Dim,Max_iter);

Convergence_curve=zeros(1,Max_iter);
Trajectories=zeros(pop,Max_iter);

%计算初始海马的体能(适应度)
for i=1:pop
    Sea_horsesFitness(1,i)=SYD(Sea_horses(i,:),net);
    net.trainParam.showWindow = 0;

    fitness_history(i,1)=Sea_horsesFitness(1,i);
    population_history(i,:,1)=Sea_horses(i,:);

    Trajectories(:,1)=Sea_horses(:,1);
end

%找出初始种群中最优秀的海马
[~,sorted_indexes]=sort(Sea_horsesFitness);
Position=Sea_horses(sorted_indexes(1),:);
Fitness = Sea_horsesFitness(sorted_indexes(1));
Convergence_curve(1)=Fitness;

% 参数
t=1;
u=0.05;%螺旋常数 
v=0.05;%螺旋常数 
l=0.05;%常数系数

while t<Max_iter+1
    beta=randn(pop,Dim);%布朗运动的随机游走系数
    Elite=repmat(Position,pop,1);% 精英海马

    %海马的运动行为
    r1=randn(1,pop);
    omega = 1.5; 
    Step_length=levy(pop,Dim,omega);%levy飞行模拟海马的运动步长
    for i=1:pop
        for j=1:Dim
            if r1(i)>0  
                r=rand();
                theta=r*2*pi;%[0,2π]之间的随机值
                row=u*exp(theta*v);%螺旋常数u,v定义的长度
                x=row*cos(theta);%
                y=row*sin(theta);%
                z=row*theta;%三维分量
                %螺旋运动模式
                Sea_horses_new1(i,j)=Sea_horses(i,j)+Step_length(i,j)*((Elite(i,j)-Sea_horses(i,j)).*x.*y.*z+Elite(i,j));%Eq.(4)
            else
                %布朗运动模式
                Sea_horses_new1(i,j)=Sea_horses(i,j)+rand()*l*beta(i,j)*(Sea_horses(i,j)-beta(i,j)* Elite(i,j));%Eq.(7)
            end
        end
    end

     for i=1:pop

        %边界检测
        Tp=Sea_horses_new1(i,:)>UB;
        Tm=Sea_horses_new1(i,:)<LB;
        Sea_horses_new1(i,:)=(Sea_horses_new1(i,:).*(~(Tp+Tm)))+UB.*Tp+LB.*Tm;    
     end

    % 海马的捕食行为,90%成功率
    for i=1:pop
        for j=1:Dim
            r1=rand();
            r2(i)=rand();
            alpha=(1-t/Max_iter)^(2*t/Max_iter);%Eq(11)随着迭代而线性减小

            if r2(i)>0.1
                Sea_horses_new2(i,j)=alpha*(Elite(i,j)-rand()*Sea_horses_new1(i,j))+(1-alpha)*Elite(i,j);  %Eq.(10)
            else
                Sea_horses_new2(i,j)=(1-alpha)*(Sea_horses_new1(i,j)-rand()*Elite(i,j))+alpha*Sea_horses_new1(i,j);  %Eq.(10)
            end
        end
    end

    for i=1:pop
        %边界检查
        Tp=Sea_horses_new2(i,:)>UB;
        Tm=Sea_horses_new2(i,:)<LB;
        Sea_horses_new2(i,:)=(Sea_horses_new2(i,:).*(~(Tp+Tm)))+UB.*Tp+LB.*Tm;
        %计算所有海马的体能
        Sea_horsesFitness1(1,i)=SYD(Sea_horses_new2(i,:),net);
        net.trainParam.showWindow = 0;
    end

    [~,index]=sort(Sea_horsesFitness1);
    
    %海马的繁殖行为
    Sea_horses_father=Sea_horses_new2(index(1:pop/2),:);    %Eq.(12)雄性
    Sea_horses_mother=Sea_horses_new2(index(pop/2+1:pop),:);  %Eq.(12)雌性

    for k=1:pop/2
        r3=rand();
        Si(k,:)=r3*Sea_horses_father(k,:)+(1-r3)*Sea_horses_mother(k,:);  %Eq.(13)
    end

    Sea_horses_offspring=Si;%后代

    for i=1:pop*0.5
        %边界检查
        Tp=Sea_horses_offspring(i,:)>UB;
        Tm=Sea_horses_offspring(i,:)<LB;
        Sea_horses_offspring(i,:)=(Sea_horses_offspring(i,:).*(~(Tp+Tm)))+UB.*Tp+LB.*Tm;
        
        Sea_horsesFitness2(1,i)=SYD(Sea_horses_offspring(i,:),net);
        net.trainParam.showWindow = 0;
    end

    %海马对比
    Sea_horsesFitness=[Sea_horsesFitness1,Sea_horsesFitness2];
    Sea_horses_new=[Sea_horses_new2;Sea_horses_offspring];
    
    [~,sorted_indexes]=sort(Sea_horsesFitness);
    
    Sea_horses=Sea_horses_new(sorted_indexes(1:pop),:);
    
    SortfitbestN = Sea_horsesFitness(sorted_indexes(1:pop));
    fitness_history(:,t)=SortfitbestN';
    population_history(:,:,t)=Sea_horses;
    Trajectories(:,t)=Sea_horses(:,1);

    %更新最优解
    if SortfitbestN(1)<Fitness
        Position=Sea_horses(1,:);
        Fitness=SortfitbestN(1);
    end
    
    Convergence_curve(t)=Fitness;
    t = t + 1;
    
    
end

3.3 SHO-SVM

1)回归模型:私信

2)分类模型:分类模型:海马算法优化支持向量机模型(SHO-SVM)

4 视频讲解

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

标签:horses,海马,pop,Matlab,Sea,new1,向量,SHO
From: https://blog.csdn.net/m0_74389201/article/details/141528705

相关文章