美文网首页
优化算法matlab实现(三十五)天鹰算法matlab实现

优化算法matlab实现(三十五)天鹰算法matlab实现

作者: stronghorse | 来源:发表于2022-09-10 12:27 被阅读0次

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

1.代码实现

不了解天鹰算法可以先看看优化算法笔记(三十五)天鹰算法
实现代码前需要先完成优化算法matlab实现(二)框架编写中的框架的编写。

文件 名描述
..\optimization algorithm\frame\Unit.m 个体
..\optimization algorithm\frame\Algorithm_Impl.m 算法主体

以及优化算法matlab实现(四)测试粒子群算法中的测试函数、函数图像的编写。

文件名 描述
..\optimization algorithm\frame\Get_Functions_details.m 测试函数,求值用
..\optimization algorithm\frame\func_plot.m 函数图像,画图用

天鹰算法的个体没有独有属性。
天鹰算法个体
文件名:.. \optimization algorithm\algorithm_aquila\AO_Unit.m

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

天鹰算法算法主体
文件名:..\optimization algorithm\algorithm_aquila\AO_Base.m

% 天鹰算法
classdef AO_Base  < Algorithm_Impl
    
    properties
        % 算法名称
        name = 'AO';
        
        alpha = 0.1;
    
        delta = 0.1;
    
        omega = 0.005;
    
        u = 0.0265;
    
        r0 = 10;
      
    end
    
    % 外部可调用的方法
    methods
        function self = AO_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 ='AO';
        end
    end
    
    % 继承重写父类的方法
    methods (Access = protected)
        % 初始化种群
        function init(self)
            init@Algorithm_Impl(self)
            %初始化种群
            for i = 1:self.size
                unit = AO_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)
            if iter< self.iter_max*2/3
                self.expanded(iter);
            else
                self.narrowed(iter);
            end
            
        end
        
        % 阶段1 扩张阶段
        function expanded(self,iter)
            x = (self.r0+self.u*(1:self.dim)).*sin(-self.omega*(1:self.dim)+3*pi/2);
            y = (self.r0+self.u*(1:self.dim)).*cos(-self.omega*(1:self.dim)+3*pi/2);
            for i = 1:self.size
                if rand < 0.5
                    pos_mean = mean(self.unit_list(i).position);
                    new_pos = self.position_best*(1-iter/self.iter_max) + rand*(pos_mean-self.position_best);
                else
                    r_id = randperm(self.size,1);
                    
                    new_pos = self.position_best.*Levy(self.dim)+self.unit_list(r_id).position+(y-x)*rand;
                end
                
                % 越界检查,越界后再解空间随机
                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).position = new_pos;
                    self.unit_list(i).value = new_value;
                end
            end   
        end
        
        % 阶段2 收缩阶段
        function narrowed(self,iter)
            QF=iter^((2*rand()-1)/(1-self.iter_max)^2);
            G1 = 2*(1-iter/self.iter_max);
            G2 = 2*rand-1;
            
            for i = 1:self.size
                if rand < 0.5
                    pos_mean = self.get_mean_pos();
                    new_pos = (self.position_best-pos_mean)*self.alpha - rand+self.delta*unifrnd(self.range_min_list,self.range_max_list);
                else
                    new_pos = QF*self.position_best-(G2*self.unit_list(i).position*rand) - G1.*Levy(self.dim)+rand*G2;

                end
                % 越界检查,越界后再解空间随机
                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).position = new_pos;
                    self.unit_list(i).value = new_value;
                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;
            end
            pos_mean = pos_mean/self.size;
        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 o=Levy(d)
beta=1.5;
sigma=(gamma(1+beta)*sin(pi*beta/2)/(gamma((1+beta)/2)*beta*2^((beta-1)/2)))^(1/beta);
u=randn(1,d)*sigma;v=randn(1,d);step=u./abs(v).^(1/beta);
o=step;
end

文件名:..\optimization algorithm\algorithm_aquila\AO_Impl.m
算法实现,继承于Base,图方便也可不写,直接用AO_Base,这里为了命名一致。

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

2.测试

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

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

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


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

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

% 实例化天鹰算法类
base = AO_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)]);

相关文章

网友评论

      本文标题:优化算法matlab实现(三十五)天鹰算法matlab实现

      本文链接:https://www.haomeiwen.com/subject/wrtnnrtx.html