优化算法matlab大杀器 —— 实现秃鹰算法
wptr33 2025-05-28 20:48 5 浏览
注意:此代码实现的是求目标函数最大值,求最小值可将适应度函数乘以-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)]);
相关推荐
- IIR数字滤波器原理及实现方法
-
欢迎大家关注转发...
- MATLAB机器学习工具箱
-
MATLAB机器学习工具箱MATLAB包含统计和机器学习工具箱(StatisticsandMachineLearningToolbox),其包含如下一些子模块:探索性数据分析、数据降维、机器学...
- MATLAB二分法求方程的根(实例加程序)
-
零点的存在性定理早在高中阶段,我们就学习过函数的零点存在性定理。简单地说,对于区间[a,b]上的连续函数f(x),如果满足f(a)f(b)<=0,那么函数在[a,b]上至少存在一个零点。...
- 优化算法matlab大杀器 —— 实现秃鹰算法
-
注意:此代码实现的是求目标函数最大值,求最小值可将适应度函数乘以-1(框架代码已实现)。.代码实现...
- 实例讲解MATLAB绘图坐标轴标签旋转
-
在我们进行绘图时,需要在图片上添加上做标轴的标签,但是当数据量比较多时,例如一天24小时的数据,这时把每个小时显示在左边轴的标签上,文字内容放不下,因此需要将坐标轴标签旋转一定的角度,这样可以更好在图...
- MATLAB的quiver函数绘制箭头向量图
-
MATLAB的quiver函数绘制箭头向量图,quiver函数的基本语法如下:基本语法...
- 「高中物理」电场和电势怎么理解?利用matlab工具完成可视化
-
高中物理给出了电场力和场强的计算方法,但是没给出电势的计算方法。下面先给出利用高中物理可接受的方法推导电势的公式可以看出电势就是场强的积分,也就是说场强是电势对距离的导数matlab做等量异种、同种...
- Matlab绘制图像,imagesc和pcolor的区别【工作记录4】
-
注意:此文章主要针对本人在工作中遇到的问题进行记录和经验上的总结,难免存在错误和遗漏,如有任何问题,请留言,我会尽快回复。最近用Matlab绘制图像时,发现用pcolor和imagesc画图区别挺大...
- 硬核浪漫!圣诞节给对象送礼的5个最没用小技巧
-
《圣诞节里吃饺子》...
- MATLAB画一阶系统单位阶跃响应、单位脉冲响应、单位斜坡响应曲线
-
在确定系统的数学模型后,便可以用几种不同的方法去分析控制系统的动态性能和稳态性能。在经典控制理论中,常用时域分析法、根轨迹法或频域分析法来分析线性控制系统的性能。显然,不同的方法有不同的特点和适用范围...
- 奇怪的Matlab画图技巧系列2–不一样的线条标记
-
毕业季马上要到了,给大家总结一些我平常用到的奇特但是好用的Matlab画图技巧不一样的线条标记论文画图通过需要通过添加不同标记或者线形,区别不同类型的参数曲线,比较土豪的同学可以选择用颜色区别外加全部...
- MATLAB画等高线的源程序和实例
-
源程序clearclcx=[60555951443937362944370121419191712323271415182429363937...
- 奇怪的Matlab画图技巧系列4–动图生成
-
毕业季马上要到了,给大家总结一些我平常用到的奇特但是好用的Matlab画图技巧MatlabGif动图保存毕业论文除了论文本身以外,在毕业论文的交流和最后的答辩过程中,通常需要制作PPT跟老师进行汇报...
- Spring Boot 打包与部署全面指南:从基础到高级实践
-
前言作为Java开发者,掌握SpringBoot应用的打包与部署是必备技能。本文将全面系统地介绍SpringBoot应用的打包与部署方式,从基础到高级,涵盖各种场景和需求。...
- 一周热门
-
-
C# 13 和 .NET 9 全知道 :13 使用 ASP.NET Core 构建网站 (1)
-
因果推断Matching方式实现代码 因果推断模型
-
git pull命令使用实例 git pull--rebase
-
git 执行pull错误如何撤销 git pull fail
-
面试官:git pull是哪两个指令的组合?
-
git fetch 和git pull 的异同 git中fetch和pull的区别
-
git pull 和git fetch 命令分别有什么作用?二者有什么区别?
-
git pull 之后本地代码被覆盖 解决方案
-
还可以这样玩?Git基本原理及各种骚操作,涨知识了
-
git命令之pull git.pull
-
- 最近发表
- 标签列表
-
- git pull (33)
- git fetch (35)
- mysql insert (35)
- mysql distinct (37)
- concat_ws (36)
- java continue (36)
- jenkins官网 (37)
- mysql 子查询 (37)
- python元组 (33)
- mybatis 分页 (35)
- vba split (37)
- redis watch (34)
- python list sort (37)
- nvarchar2 (34)
- mysql not null (36)
- hmset (35)
- python telnet (35)
- python readlines() 方法 (36)
- munmap (35)
- docker network create (35)
- redis 集合 (37)
- python sftp (37)
- setpriority (34)
- c语言 switch (34)
- git commit (34)