美文网首页
优化算法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_harris_hawks\HHO_Unit.m

    % 哈里斯鹰算法个体
    classdef HHO_Unit < Unit
        
        properties
        end
        
        methods
            function self = HHO_Unit()
            end
        end
     
    end
    

    哈里斯鹰算法算法主体
    文件名:..\optimization algorithm\algorithm_harris_hawks\HHO_Base.m

    % 天鹰算法
    classdef HHO_Base  < Algorithm_Impl
        
        properties
            % 算法名称
            name = 'HHO';
    
        end
        
        % 外部可调用的方法
        methods
            function self = HHO_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 ='HHO';
            end
        end
        
        % 继承重写父类的方法
        methods (Access = protected)
            % 初始化种群
            function init(self)
                init@Algorithm_Impl(self)
                %初始化种群
                for i = 1:self.size
                    unit = HHO_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)
                E1 = 2*(1-iter/self.iter_max);
    
                % 获取群体平均数据
                pos_mean = self.get_mean_pos();
                
                % 遍历每一个个体
                for i = 1:self.size
                    escaping_energy = unifrnd(-1, 1)*E1;
                    if abs(escaping_energy) > 1
                        q = rand;
                        % 原文公式(1)
                        if q<=0.5
                            r_id = randperm(self.size,1);
                            new_pos = self.unit_list(r_id).position-rand*abs(self.unit_list(r_id).position-2*rand*self.unit_list(i).position);
                        else
                            new_pos = (self.position_best-self.unit_list(i).position)-rand*unifrnd(self.range_min_list,self.range_max_list);
                        end
                        % 直接接受该位置
                        new_pos = self.get_out_bound_value(new_pos);
                        new_value = self.cal_fitfunction(new_pos);
                        self.unit_list(i).position = new_pos;
                        self.unit_list(i).value =new_value;
                    else
                        r = rand;
                        if r>=0.5
                            if abs(escaping_energy)<0.5
                                % 原文公式(6)
                                new_pos = self.position_best-escaping_energy*abs(self.position_best-self.unit_list(i).position);
                            else
                                % 原文公式(4)
                                jump_strength = unifrnd(0, 2,1,self.dim);
                                new_pos = (self.position_best-self.unit_list(i).position);%-escaping_energy*abs(jump_strength.*self.position_best-self.unit_list(i).position);
                            end
                            % 直接接受该位置
                            new_pos = self.get_out_bound_value(new_pos);
                            new_value = self.cal_fitfunction(new_pos);
                            self.unit_list(i).position = new_pos;
                            self.unit_list(i).value =new_value;
                        else
                            jump_strength = unifrnd(0, 2,1,self.dim);
                            if abs(escaping_energy)<0.5
                                % 原文公式(7)
                                new_pos=self.position_best-escaping_energy*abs(jump_strength.*self.position_best-self.unit_list(i).position);
                            else
                                % 原文公式(12)
                                
                                new_pos=self.position_best-escaping_energy*abs(jump_strength.*self.position_best-pos_mean);
                            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;
                            else
                                % 如果结果较差则levy一下
                                new_pos = new_pos + rand*Levy(self.dim);
                                new_pos = self.get_out_bound_value(new_pos);
                                new_value = self.cal_fitfunction(new_pos);
                                self.unit_list(i).position = new_pos;
                                self.unit_list(i).value =new_value;
                            end
                        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 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_harris_hawks\HHO_Impl.m
    算法实现,继承于Base,图方便也可不写,直接用HHO_Base,这里为了命名一致。

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

    2.测试

    测试F1
    文件名:..\optimization algorithm\algorithm_harris_hawks\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 = HHO_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/qstnnrtx.html