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

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

wptr33 2025-05-28 20:48 13 浏览

注意:此代码实现的是求目标函数最大值,求最小值可将适应度函数乘以-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)]);

相关推荐

redis的八种使用场景

前言:redis是我们工作开发中,经常要打交道的,下面对redis的使用场景做总结介绍也是对redis举报的功能做梳理。缓存Redis最常见的用途是作为缓存,用于加速应用程序的响应速度。...

基于Redis的3种分布式ID生成策略

在分布式系统设计中,全局唯一ID是一个基础而关键的组件。随着业务规模扩大和系统架构向微服务演进,传统的单机自增ID已无法满足需求。高并发、高可用的分布式ID生成方案成为构建可靠分布式系统的必要条件。R...

基于OpenWrt系统路由器的模式切换与网页设计

摘要:目前商用WiFi路由器已应用到多个领域,商家通过给用户提供一个稳定免费WiFi热点达到吸引客户、提升服务的目标。传统路由器自带的Luci界面提供了工厂模式的Web界面,用户可通过该界面配置路...

这篇文章教你看明白 nginx-ingress 控制器

主机nginx一般nginx做主机反向代理(网关)有以下配置...

如何用redis实现注册中心

一句话总结使用Redis实现注册中心:服务注册...

爱可可老师24小时热门分享(2020.5.10)

No1.看自己以前写的代码是种什么体验?No2.DooM-chip!国外网友SylvainLefebvre自制的无CPU、无操作码、无指令计数器...No3.我认为CS学位可以更好,如...

Apportable:拯救程序员,IOS一秒变安卓

摘要:还在为了跨平台使用cocos2d-x吗,拯救objc程序员的奇葩来了,ApportableSDK:FreeAndroidsupportforcocos2d-iPhone。App...

JAVA实现超买超卖方案汇总,那个最适合你,一篇文章彻底讲透

以下是几种Java实现超买超卖问题的核心解决方案及代码示例,针对高并发场景下的库存扣减问题:方案一:Redis原子操作+Lua脚本(推荐)//使用Redis+Lua保证原子性publicbo...

3月26日更新 快速施法自动施法可独立设置

2016年3月26日DOTA2有一个79.6MB的更新主要是针对自动施法和快速施法的调整本来内容不多不少朋友都有自动施法和快速施法的困扰英文更新日志一些视觉BUG修复就不翻译了主要翻译自动施...

Redis 是如何提供服务的

在刚刚接触Redis的时候,最想要知道的是一个’setnameJhon’命令到达Redis服务器的时候,它是如何返回’OK’的?里面命令处理的流程如何,具体细节怎么样?你一定有问过自己...

lua _G、_VERSION使用

到这里我们已经把lua基础库中的函数介绍完了,除了函数外基础库中还有两个常量,一个是_G,另一个是_VERSION。_G是基础库本身,指向自己,这个变量很有意思,可以无限引用自己,最后得到的还是自己,...

China&#39;s top diplomat to chair third China-Pacific Island countries foreign ministers&#39; meeting

BEIJING,May21(Xinhua)--ChineseForeignMinisterWangYi,alsoamemberofthePoliticalBureau...

移动工作交流工具Lua推出Insights数据分析产品

Lua是一个适用于各种职业人士的移动交流平台,它在今天推出了一项叫做Insights的全新功能。Insights是一个数据平台,客户可以在上面实时看到员工之间的交流情况,并分析这些情况对公司发展的影响...

Redis 7新武器:用Redis Stack实现向量搜索的极限压测

当传统关系型数据库还在为向量相似度搜索的性能挣扎时,Redis7的RedisStack...

Nginx/OpenResty详解,Nginx Lua编程,重定向与内部子请求

重定向与内部子请求Nginx的rewrite指令不仅可以在Nginx内部的server、location之间进行跳转,还可以进行外部链接的重定向。通过ngx_lua模块的Lua函数除了能实现Nginx...