0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

鲸鱼优化算法MATLAB实战

冬至子 来源:matlab学习之家 作者:matlab学习之家 2023-06-02 16:17 次阅读

一、鲸鱼优化算法

鲸鱼优化算法(Whale Optimization Algorithm,WOA)是模仿自然界中鲸鱼捕食行为的新型群体智能优化算法。它通过对自然界中座头鲸群体狩猎行为的模拟,通过鲸鱼群体搜索、包围、追捕和攻击猎物等过程实现优时化搜索的目的。鲸鱼优化算法的工作原理如下:

1、初始化:首先,在算法开始时,需要为每个鲸鱼设定一个初始位置,并生成初始种群。

2、搜索:每个鲸鱼都会按照一定的规则探索空间。这个过程可以模拟鲸鱼包围、追捕和攻击猎物等过程。

3、评估:每当鲸鱼移动的时候,都会对当前的鲸鱼种群计算适应度值。如果当前的适应度值优于之前的适应度值,则将当前适应度值设为最优解。

4、更新:当所有的鲸鱼都完成了移动和评估后,算法会更新所有鲸鱼的位置,并重复以上步骤。

5、迭代:鲸鱼优化算法可以进行多次迭代,直到找到最优解为止。

鲸鱼优化算法的优势在于操作简单,调整的参数少以及跳出局部最优的能力强,它能够快速找到最优解,并且对于各种类型的优化问题都能有效地工作。对于基础的问题,它还具有很好的收敛性和稳定性。

鲸鱼优化算法主要包括三个过程:1)包围猎物;2)发泡网攻击;3)搜索捕食。

图片

图片

图片

鲸鱼优化算法的流程图如下图所示:

图片

二、代码实战

%_________________________________________________________________________%
%  Whale Optimization Algorithm (WOA) source codes demo 1.0               %
%                                                                         %
%  Developed in MATLAB R2011b(7.13)                                       %
%                                                                         %
%  Author and programmer: Seyedali Mirjalili                              %
%                                                                         %
%         e-Mail: ali.mirjalili@gmail.com                                 %
%                 seyedali.mirjalili@griffithuni.edu.au                   %
%                                                                         %
%       Homepage: http://www.alimirjalili.com                             %
%                                                                         %
%   Main paper: S. Mirjalili, A. Lewis                                    %
%               The Whale Optimization Algorithm,                         %
%               Advances in Engineering Software , in press,              %
%               DOI: http://dx.doi.org/10.1016/j.advengsoft.2016.01.008   %
%                                                                         %
%_________________________________________________________________________%


% You can simply define your cost in a seperate file and load its handle to fobj 
% The initial parameters that you need are:
%__________________________________________
% fobj = @YourCostFunction
% dim = number of your variables
% Max_iteration = maximum number of generations
% SearchAgents_no = number of search agents
% lb=[lb1,lb2,...,lbn] where lbn is the lower bound of variable n
% ub=[ub1,ub2,...,ubn] where ubn is the upper bound of variable n
% If all the variables have equal lower bound you can just
% define lb and ub as two single number numbers


% To run WOA: [Best_score,Best_pos,WOA_cg_curve]=WOA(SearchAgents_no,Max_iteration,lb,ub,dim,fobj)
%__________________________________________


clear all 
clc


SearchAgents_no=30; % Number of search agents


Function_name='F1'; % Name of the test function that can be from F1 to F23 (Table 1,2,3 in the paper)


Max_iteration=500; % Maximum numbef of iterations


% Load details of the selected benchmark function
[lb,ub,dim,fobj]=Get_Functions_details(Function_name);


[Best_score,Best_pos,WOA_cg_curve]=WOA(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);


figure('Position',[269   240   660   290])
%Draw search space
subplot(1,2,1);
func_plot(Function_name);
title('Parameter space')
xlabel('x_1');
ylabel('x_2');
zlabel([Function_name,'( x_1 , x_2 )'])


%Draw objective space
subplot(1,2,2);
semilogy(WOA_cg_curve,'Color','r')
title('Objective space')
xlabel('Iteration');
ylabel('Best score obtained so far');


axis tight
grid on
box on
legend('WOA')


display(['The best solution obtained by WOA is : ', num2str(Best_pos)]);
display(['The best optimal value of the objective funciton found by WOA is : ', num2str(Best_score)]);
%_________________________________________________________________________%
%  Whale Optimization Algorithm (WOA) source codes demo 1.0               %
%                                                                         %
%  Developed in MATLAB R2011b(7.13)                                       %
%                                                                         %
%  Author and programmer: Seyedali Mirjalili                              %
%                                                                         %
%         e-Mail: ali.mirjalili@gmail.com                                 %
%                 seyedali.mirjalili@griffithuni.edu.au                   %
%                                                                         %
%       Homepage: http://www.alimirjalili.com                             %
%                                                                         %
%   Main paper: S. Mirjalili, A. Lewis                                    %
%               The Whale Optimization Algorithm,                         %
%               Advances in Engineering Software , in press,              %
%               DOI: http://dx.doi.org/10.1016/j.advengsoft.2016.01.008   %
%                                                                         
%                                                                         %
%_________________________________________________________________________%




% 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);


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

实验结果:

图片

图片

图片

图片

图片

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • MATLAB仿真
    +关注

    关注

    4

    文章

    174

    浏览量

    19638
  • WOA
    WOA
    +关注

    关注

    0

    文章

    3

    浏览量

    9740
收藏 人收藏

    评论

    相关推荐

    MATLAB的各种优化算法介绍

    ,并介绍了最优化问题辅助计算软件MATLAB6.5产生的背景、优化工具篇及其工程应用 [hide] [/hide]
    发表于 03-06 14:53

    MATLAB优化算法案例分析与应用》

    MATLAB优化算法案例分析与应用》清华大学出版社《MATLAB优化算法案例分析与应用》这本书
    发表于 10-10 12:34

    【下载】MATLAB R2014a完全自学一本通+MATLAB智能算法30个案例分析

    环境的基础上,对MATLAB 使用中常用的知识和工具进行了详细的介绍,书中各章均提供了大量有针对性的算例,供读者实战练习。根据内容的侧重点不同,全书分为4 部分共20 章:第1~5 章为基础部分,讲解
    发表于 06-01 18:01

    果蝇优化算法MATLAB实现

    果蝇优化算法MATLAB实现发布时间:2018-10-12 23:28,浏览次数:1183, 标签:MATLAB果蝇优化
    发表于 08-17 07:28

    MATLAB编程求解优化设计

    优化设计-复合型法-MATLAB编程求解优化设计-有约束复合型法-MATLAB编程求解有约束复合型法迭代步骤(计算流程图)MATLAB主程序
    发表于 08-17 09:31

    FOA优化算法整定PID控制器参数

    【Simulink】FOA优化算法整定PID控制器参数(五)—— 一阶带时延的被控对象目录【Simulink】FOA优化算法整定PID控制器参数(五)—— 一阶带时延的被控对象0研究背
    发表于 08-30 06:46

    果蝇优化算法MATLAB实现过程是怎样的?

    果蝇优化算法MATLAB实现过程是怎样的?
    发表于 11-22 07:48

    MATLAB的简单函数优化的遗传算法程序免费下载

    本文档的主要内容详细介绍的是MATLAB的简单函数优化的遗传算法程序免费下载。
    发表于 10-24 08:00 2次下载
    <b class='flag-5'>MATLAB</b>的简单函数<b class='flag-5'>优化</b>的遗传<b class='flag-5'>算法</b>程序免费下载

    用于MATLAB遗传优化算法谢菲尔德大学遗传算法工具箱免费下载

    本文档的主要内容详细介绍的是用于MATLAB遗传优化算法谢菲尔德大学遗传算法工具箱免费下载
    发表于 05-27 08:00 11次下载
    用于<b class='flag-5'>MATLAB</b>遗传<b class='flag-5'>优化</b><b class='flag-5'>算法</b>谢菲尔德大学遗传<b class='flag-5'>算法</b>工具箱免费下载

    使用MATLAB遗传算法工具

    ,GADS)。使用遗传算法与直接搜索工具箱,可以扩展 MATLAB 及其优化工具箱在处理优化问题方面的能力,可以处理传统的优化技术难以解决的
    发表于 04-19 15:22 11次下载

    改进鲸鱼优化算法在路径规划的应用综述

    改进鲸鱼优化算法在路径规划的应用综述
    发表于 06-23 17:02 15次下载

    MATLAB优化算法汇总01

    MATLAB优化算法汇总01
    发表于 10-08 10:57 0次下载

    MATLAB优化算法汇总02

    MATLAB优化算法汇总02
    发表于 10-08 10:59 0次下载

    MATLAB优化算法汇总03

    MATLAB优化算法汇总03
    发表于 10-08 11:01 0次下载

    Dijkstra和A*算法及其Matlab实现

    写在前面的话:只是对两种路径优化算法进行简单的理解和尝试,为后续使用做准备。如果用到,请再次好好理解原理和Matlab源码。
    的头像 发表于 12-07 15:04 994次阅读