百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

优化算法matlab大杀器 —— 实现秃鹰算法

moboyou 2025-05-02 18:14 23 浏览

注意:此代码实现的是求目标函数最大值,求最小值可将适应度函数乘以-1(框架代码已实现)。

.代码实现

文件

名描述

..\optimization algorithm\frame\Unit.m

个体

..\optimization algorithm\frame\Algorithm_Impl.m

算法主体


文件名

描述

..\optimization algorithm\frame\Get_Functions_details.m

测试函数,求值用

..\optimization algorithm\frame\func_plot.m

函数图像,画图用

秃鹰算法的个体没有独有属性。
秃鹰算法个体
文件名:.. \optimization algorithm\
algorithm_bald_eagle_search\BES_Unit.m

% 秃鹰算法个体
classdef BES_Unit < Unit
    
    properties
    end
    
    methods
        function self = BES_Unit()
        end
    end
 
end

秃鹰算法算法主体
文件名:..\optimization algorithm\
algorithm_bald_eagle_search\BES_Base.m

% 秃鹰算法
classdef BES_Base  < Algorithm_Impl
    
    properties
        % 算法名称
        name = 'BES';
        c1 = 2;
        c2 = 2;
        alpha = 2;
        a = 10;
        R = 1.5;

    end
    
    % 外部可调用的方法
    methods
        function self = BES_Base(dim,size,iter_max,range_min_list,range_max_list)
            % 调用父类构造函数
            self@Algorithm_Impl(dim,size,iter_max,range_min_list,range_max_list);
            self.name ='BES';
        end
    end
    
    % 继承重写父类的方法
    methods (Access = protected)
        % 初始化种群
        function init(self)
            init@Algorithm_Impl(self)
            %初始化种群
            for i = 1:self.size
                unit = BES_Unit();
                % 随机初始化位置:rand(0,1).*(max-min)+min
                unit.position = unifrnd(self.range_min_list,self.range_max_list);
                % 计算适应度值
                unit.value = self.cal_fitfunction(unit.position);
                % 将个体加入群体数组
                self.unit_list = [self.unit_list,unit];
            end
        end
        
        % 每一代的更新
        function update(self,iter)
            update@Algorithm_Impl(self,iter)
            
            % 选择搜索区域
            self.select_space();
            %搜索
            self.search_in_space();
            %俯冲
            self.swoop();
            
        end
        
        % 选择搜索的区域
        function select_space(self)
            % 计算群体的平均位置
            pos_mean = self.get_mean_pos();
            
            % 遍历每一个个体
            for i = 1:self.size
                % 计算新位置
                new_pos = self.position_best + self.alpha*unifrnd(0,1,1,self.dim).*(pos_mean-self.unit_list(i).position);
                % 越界检查
                new_pos = self.get_out_bound_value(new_pos);
                new_value = self.cal_fitfunction(new_pos);
                % 贪心一下
                if new_value > self.unit_list(i).value
                     % 更新个体
                     self.unit_list(i).value = new_value;
                     self.unit_list(i).position = new_pos;
                     if new_value > self.value_best
                         % 更新全局最优
                         self.value_best = new_value;
                         self.position_best = new_pos;
                     end
                end
            end
        end
        
        % 在区域内搜索
        function search_in_space(self)
            % 计算群体的平均位置
            pos_mean = self.get_mean_pos();
            
            % 生成随机theta和r向量,向量长度为总群个数,即一个个体一个值
            theta = self.a*pi*unifrnd(0,1,1,self.size);
            r = theta + self.R*unifrnd(0,1,1,self.size);
            xr = get_xr(theta,r);
            yr = get_yr(theta,r);
            % 获取xr,yr中的最大绝对值
            xr_max = max(abs(xr));
            yr_max = max(abs(yr));
            
            % 遍历每一个个体
            for i = 1:self.size
                % 第一下一个个体,即最后一个的下一个是第一个
                if i == self.size
                    next_pos = self.unit_list(1).position;
                else
                    next_pos = self.unit_list(i+1).position;
                end
                
                % 计算新位置
                new_pos = self.unit_list(i).position +  xr(i)/xr_max.*(self.unit_list(i).position-pos_mean)+ yr(i)/yr_max.*(self.unit_list(i).position-next_pos) ;
                % 越界检查
                new_pos = self.get_out_bound_value(new_pos);
                new_value = self.cal_fitfunction(new_pos);
                % 贪心一下
                if new_value > self.unit_list(i).value
                     % 更新个体
                     self.unit_list(i).value = new_value;
                     self.unit_list(i).position = new_pos;
                     if new_value > self.value_best
                         % 更新全局最优
                         self.value_best = new_value;
                         self.position_best = new_pos;
                     end
                end
            end
        end
        
        %俯冲
        function swoop(self)
             % 计算群体的平均位置
            pos_mean = self.get_mean_pos();
            
            % 生成随机theta和r向量,向量长度为总群个数,即一个个体一个值
            theta = self.a*pi*unifrnd(0,1,1,self.size);
            r = theta + self.R*unifrnd(0,1,1,self.size);
            xrl = get_xrl(theta,r);
            yrl = get_yrl(theta,r);
            % 获取xrl,yrl中的最大绝对值
            xrl_max = max(abs(xrl));
            yrl_max = max(abs(yrl));
            
            % 遍历每一个个体
            for i = 1:self.size
                
                % 计算新位置
                new_pos = self.position_best.*unifrnd(0,1,1,self.dim) +  xrl(i)/xrl_max.*(self.unit_list(i).position-self.c1*pos_mean)+ yrl(i)/yrl_max.*(self.unit_list(i).position-self.c2*self.position_best) ;
                % 越界检查
                new_pos = self.get_out_bound_value(new_pos);
                new_value = self.cal_fitfunction(new_pos);
                % 贪心一下
                if new_value > self.unit_list(i).value
                     % 更新个体
                     self.unit_list(i).value = new_value;
                     self.unit_list(i).position = new_pos;
                     if new_value > self.value_best
                         % 更新全局最优
                         self.value_best = new_value;
                         self.position_best = new_pos;
                     end
                end
            end
        end
        
        % 获取种群平均位置
        function pos_mean = get_mean_pos(self)
            pos_mean = zeros(1,self.dim);
            for i=1:self.size
                pos_mean = pos_mean + self.unit_list(i).position/self.size;
            end
        end
        
        % 获取当前最优个体的id
        function best_id=get_best_id(self)
            % 求最大值则降序排列
            [value,index] = sort([self.unit_list.value],'descend');
            best_id = index(1);
        end

    end
end

function xr = get_xr(theta,r)
xr = r.*sin(theta);
end

function yr = get_yr(theta,r)
yr = r.*cos(theta);
end

function xrl = get_xrl(theta,r)
xrl = r.*sinh(theta);
end

function yrl = get_yrl(theta,r)
yrl = r.*cosh(theta);
end

文件名:..\optimization algorithm\
algorithm_bald_eagle_search\BES_Impl.m

算法实现,继承于Base,图方便也可不写,直接用BES_Base,这里为了命名一致。

% 秃鹰算法实现
classdef BES_Impl < BES_Base
   
    % 外部可调用的方法
    methods
        function self = BES_Impl(dim,size,iter_max,range_min_list,range_max_list)
            % 调用父类构造函数设置参数
             self@BES_Base(dim,size,iter_max,range_min_list,range_max_list);
        end
    end 
end

2.测试

测试F1
文件名:..\optimization algorithm\
algorithm_bald_eagle_search\Test.m

%% 清理之前的数据
% 清除所有数据
clear all;
% 清除窗口输出
clc;

%% 添加目录
% 将上级目录中的frame文件夹加入路径
addpath('../frame')


%% 选择测试函数
Function_name='F1';
%[最小值,最大值,维度,测试函数]
[lb,ub,dim,fobj]=Get_Functions_details(Function_name);

%% 算法实例
% 种群数量
size = 50;
% 最大迭代次数
iter_max = 300;
% 取值范围上界
range_max_list = ones(1,dim).*ub;
% 取值范围下界
range_min_list = ones(1,dim).*lb;

% 实例化秃鹰算法类
base = BES_Impl(dim,size,iter_max,range_min_list,range_max_list);
base.is_cal_max = false;
% 确定适应度函数
base.fitfunction = fobj;
% 运行
base.run();
disp(base.cal_fit_num);

%% 绘制图像
figure('Position',[500 500 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);
% 绘制曲线,由于算法是求最大值,适应度函数为求最小值,故乘了-1,此时去掉-1
semilogy((base.value_best_history),'Color','r')
title('Objective space')
xlabel('Iteration');
ylabel('Best score obtained so far');
% 将坐标轴调整为紧凑型
axis tight
% 添加网格
grid on
% 四边都显示刻度
box off
legend(base.name)
display(['The best solution obtained by ',base.name ,' is ', num2str(base.value_best)]);
display(['The best optimal value of the objective funciton found by ',base.name ,' is ', num2str(base.position_best)]);

相关推荐

Excel技巧:SHEETSNA函数一键提取所有工作表名称批量生产目录

首先介绍一下此函数:SHEETSNAME函数用于获取工作表的名称,有三个可选参数。语法:=SHEETSNAME([参照区域],[结果方向],[工作表范围])(参照区域,可选。给出参照,只返回参照单元格...

Excel HOUR函数:“小时”提取器_excel+hour函数提取器怎么用

一、函数概述HOUR函数是Excel中用于提取时间值小时部分的日期时间函数,返回0(12:00AM)到23(11:00PM)之间的整数。该函数在时间数据分析、考勤统计、日程安排等场景中应用广泛。语...

Filter+Search信息管理不再难|多条件|模糊查找|Excel函数应用

原创版权所有介绍一个信息管理系统,要求可以实现:多条件、模糊查找,手动输入的内容能去空格。先看效果,如下图动画演示这样的一个效果要怎样实现呢?本文所用函数有Filter和Search。先用filter...

FILTER函数介绍及经典用法12:FILTER+切片器的应用

EXCEL函数技巧:FILTER经典用法12。FILTER+切片器制作筛选按钮。FILTER的函数的经典用法12是用FILTER的函数和切片器制作一个筛选按钮。像左边的原始数据,右边想要制作一...

office办公应用网站推荐_office办公软件大全

以下是针对Office办公应用(Word/Excel/PPT等)的免费学习网站推荐,涵盖官方教程、综合平台及垂直领域资源,适合不同学习需求:一、官方权威资源1.微软Office官方培训...

WPS/Excel职场办公最常用的60个函数大全(含卡片),效率翻倍!

办公最常用的60个函数大全:从入门到精通,效率翻倍!在职场中,WPS/Excel几乎是每个人都离不开的工具,而函数则是其灵魂。掌握常用的函数,不仅能大幅提升工作效率,还能让你在数据处理、报表分析、自动...

收藏|查找神器Xlookup全集|一篇就够|Excel函数|图解教程

原创版权所有全程图解,方便阅读,内容比较多,请先收藏!Xlookup是Vlookup的升级函数,解决了Vlookup的所有缺点,可以完全取代Vlookup,学完本文后你将可以应对所有的查找难题,内容...

批量查询快递总耗时?用Excel这个公式,自动计算揽收到签收天数

批量查询快递总耗时?用Excel这个公式,自动计算揽收到签收天数在电商运营、物流对账等工作中,经常需要统计快递“揽收到签收”的耗时——比如判断某快递公司是否符合“3天内送达”的服务承...

Excel函数公式教程(490个实例详解)

Excel函数公式教程(490个实例详解)管理层的财务人员为什么那么厉害?就是因为他们精通excel技能!财务人员在日常工作中,经常会用到Excel财务函数公式,比如财务报表分析、工资核算、库存管理等...

Excel(WPS表格)Tocol函数应用技巧案例解读,建议收藏备用!

工作中,经常需要从多个单元格区域中提取唯一值,如体育赛事报名信息中提取唯一的参赛者信息等,此时如果复制粘贴然后去重,效率就会很低。如果能合理利用Tocol函数,将会极大地提高工作效率。一、功能及语法结...

Excel中的SCAN函数公式,把计算过程理清,你就会了

Excel新版本里面,除了出现非常好用的xlookup,Filter公式之外,还更新一批自定义函数,可以像写代码一样写公式其中SCAN函数公式,也非常强大,它是一个循环函数,今天来了解这个函数公式的计...

Excel(WPS表格)中多列去重就用Tocol+Unique组合函数,简单高效

在数据的分析和处理中,“去重”一直是绕不开的话题,如果单列去重,可以使用Unique函数完成,如果多列去重,如下图:从数据信息中可以看到,每位参赛者参加了多项运动,如果想知道去重后的参赛者有多少人,该...

Excel(WPS表格)函数Groupby,聚合统计,快速提高效率!

在前期的内容中,我们讲了很多的统计函数,如Sum系列、Average系列、Count系列、Rank系列等等……但如果用一个函数实现类似数据透视表的功能,就必须用Groupby函数,按指定字段进行聚合汇...

Excel新版本,IFS函数公式,太强大了!

我们举一个工作实例,现在需要计算业务员的奖励数据,右边是公司的奖励标准:在新版本的函数公式出来之前,我们需要使用IF函数公式来解决1、IF函数公式IF函数公式由三个参数组成,IF(判断条件,对的时候返...

Excel不用函数公式数据透视表,1秒完成多列项目汇总统计

如何将这里的多组数据进行汇总统计?每组数据当中一列是不同菜品,另一列就是该菜品的销售数量。如何进行汇总统计得到所有的菜品销售数量的求和、技术、平均、最大、最小值等数据?不用函数公式和数据透视表,一秒就...