美文网首页
优化算法matlab实现(三十四)鸽群算法matlab实现

优化算法matlab实现(三十四)鸽群算法matlab实现

作者: stronghorse | 来源:发表于2022-09-09 22:54 被阅读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_pigeon\PIO_Unit.m

    % 鸽群算法个体
    classdef PIO_Unit < Unit
        
        properties
            % 速度
            velocity;
        end
        
        methods
            function self = PIO_Unit()
            end
        end
     
    end
    

    鸽群算法算法主体
    文件名:..\optimization algorithm\algorithm_pigeon\PIO_Base.m

    % 鸽群算法
    classdef PIO_Base  < Algorithm_Impl
        
        properties
            % 算法名称
            name = 'PIO';
            
            NcRate = 0.75;
            
            R = 0.2;
          
        end
        
        % 外部可调用的方法
        methods
            function self = PIO_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 ='PIO';
            end
        end
        
        % 继承重写父类的方法
        methods (Access = protected)
            % 初始化种群
            function init(self)
                init@Algorithm_Impl(self)
                %初始化种群
                for i = 1:self.size
                    unit = PIO_Unit();
                    % 随机初始化位置:rand(0,1).*(max-min)+min
                    unit.position = unifrnd(self.range_min_list,self.range_max_list);
                    unit.velocity = 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*self.NcRate
                    self.phase1(iter);
                else
                    self.phase2();
                end
                
            end
            
            % 阶段1 地图罗盘操作
            function phase1(self,iter)
                
                for i = 1:self.size
                    % 计算新速度
                    new_velocity = self.unit_list(i).velocity*exp(-self.R*iter) + unifrnd(0,1,1,self.dim).*(self.position_best-self.unit_list(i).position);
                    % 计算新位置
                    new_pos = self.unit_list(i).position + new_velocity;
                    % 越界检查,越界后再解空间随机
                    new_pos = self.get_out_bound_value_rand(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
            
            % 阶段2 地标操作
            function phase2(self)
                % 从大到小排序,数值越大越优
                [value,index] = sort([self.unit_list.value],'descend');
                      
                XC = zeros(1,self.dim);
                value_sum = 0;
                % 计算较优的部分值之和
                for i = 1:self.size/2
                    %(分子)
                    XC = XC + self.unit_list(index(i)).position*self.unit_list(index(i)).value;
                    % 分母
                    value_sum = value_sum + self.unit_list(index(i)).value;
                end
                
                % 计算较差部分的新位置
                for i = self.size/2:self.size
                    new_pos = self.unit_list(index(i)).position + unifrnd(0,1,1,self.dim).*(XC/value_sum - self.unit_list(index(i)).position);
                    new_pos = self.get_out_bound_value(new_pos);
                    new_value = self.cal_fitfunction(new_pos);
                    self.unit_list(index(i)).position = new_pos;
                    self.unit_list(index(i)).value = new_value;
                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
    

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

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

    2.测试

    测试F1
    文件名:..\optimization algorithm\algorithm_pigeon\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 = PIO_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/thtnnrtx.html