优化算法matlab大杀器 —— 实现秃鹰算法
wptr33 2025-05-28 20:48 22 浏览
注意:此代码实现的是求目标函数最大值,求最小值可将适应度函数乘以-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
end2.测试
测试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)]);相关推荐
- oracle数据导入导出_oracle数据导入导出工具
-
关于oracle的数据导入导出,这个功能的使用场景,一般是换服务环境,把原先的oracle数据导入到另外一台oracle数据库,或者导出备份使用。只不过oracle的导入导出命令不好记忆,稍稍有点复杂...
- 继续学习Python中的while true/break语句
-
上次讲到if语句的用法,大家在微信公众号问了小编很多问题,那么小编在这几种解决一下,1.else和elif是子模块,不能单独使用2.一个if语句中可以包括很多个elif语句,但结尾只能有一个else解...
- python continue和break的区别_python中break语句和continue语句的区别
-
python中循环语句经常会使用continue和break,那么这2者的区别是?continue是跳出本次循环,进行下一次循环;break是跳出整个循环;例如:...
- 简单学Python——关键字6——break和continue
-
Python退出循环,有break语句和continue语句两种实现方式。break语句和continue语句的区别:break语句作用是终止循环。continue语句作用是跳出本轮循环,继续下一次循...
- 2-1,0基础学Python之 break退出循环、 continue继续循环 多重循
-
用for循环或者while循环时,如果要在循环体内直接退出循环,可以使用break语句。比如计算1至100的整数和,我们用while来实现:sum=0x=1whileTrue...
- Python 中 break 和 continue 傻傻分不清
-
大家好啊,我是大田。今天分享一下break和continue在代码中的执行效果是什么,进一步区分出二者的区别。一、continue例1:当小明3岁时不打印年龄,其余年龄正常循环打印。可以看...
- python中的流程控制语句:continue、break 和 return使用方法
-
Python中,continue、break和return是控制流程的关键语句,用于在循环或函数中提前退出或跳过某些操作。它们的用途和区别如下:1.continue(跳过当前循环的剩余部分,进...
- L017:continue和break - 教程文案
-
continue和break在Python中,continue和break是用于控制循环(如for和while)执行流程的关键字,它们的作用如下:1.continue:跳过当前迭代,...
- 作为前端开发者,你都经历过怎样的面试?
-
已经裸辞1个月了,最近开始投简历找工作,遇到各种各样的面试,今天分享一下。其实在职的时候也做过面试官,面试官时,感觉自己问的问题很难区分候选人的能力,最好的办法就是看看候选人的github上的代码仓库...
- 面试被问 const 是否不可变?这样回答才显功底
-
作为前端开发者,我在学习ES6特性时,总被const的"善变"搞得一头雾水——为什么用const声明的数组还能push元素?为什么基本类型赋值就会报错?直到翻遍MDN文档、对着内存图反...
- 2023金九银十必看前端面试题!2w字精品!
-
导文2023金九银十必看前端面试题!金九银十黄金期来了想要跳槽的小伙伴快来看啊CSS1.请解释CSS的盒模型是什么,并描述其组成部分。答案:CSS的盒模型是用于布局和定位元素的概念。它由内容区域...
- 前端面试总结_前端面试题整理
-
记得当时大二的时候,看到实验室的学长学姐忙于各种春招,有些收获了大厂offer,有些还在苦苦面试,其实那时候的心里还蛮忐忑的,不知道自己大三的时候会是什么样的一个水平,所以从19年的寒假放完,大二下学...
- 由浅入深,66条JavaScript面试知识点(七)
-
作者:JakeZhang转发链接:https://juejin.im/post/5ef8377f6fb9a07e693a6061目录由浅入深,66条JavaScript面试知识点(一)由浅入深,66...
- 2024前端面试真题之—VUE篇_前端面试题vue2020及答案
-
添加图片注释,不超过140字(可选)1.vue的生命周期有哪些及每个生命周期做了什么?beforeCreate是newVue()之后触发的第一个钩子,在当前阶段data、methods、com...
- 今年最常见的前端面试题,你会做几道?
-
在面试或招聘前端开发人员时,期望、现实和需求之间总是存在着巨大差距。面试其实是一个交流想法的地方,挑战人们的思考方式,并客观地分析给定的问题。可以通过面试了解人们如何做出决策,了解一个人对技术和解决问...
- 一周热门
- 最近发表
- 标签列表
-
- 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)
