美文网首页
优化算法matlab实现(二十五)飞蛾扑火算法matlab实现

优化算法matlab实现(二十五)飞蛾扑火算法matlab实现

作者: stronghorse | 来源:发表于2022-09-03 16:34 被阅读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_moth_flame\MFO_Unit.m

    % 飞蛾扑火算法个体
    classdef MFO_Unit < Unit
        
        properties
        end
        
        methods
            function self = MFO_Unit()
            end
        end
        
    end
    

    飞蛾扑火算法算法主体
    文件名:..\optimization algorithm\algorithm_moth_flame\MFO_Base.m

    % 飞蛾扑火算法
    classdef MFO_Base  < Algorithm_Impl
        
        properties
            % 算法名称
            name = 'MFO';
            % 飞蛾数量
            flame_num;
            % 火焰列表,已有的unit_list为飞蛾列表
            flame_list=[];
            t = -1;
        end
        
        % 外部可调用的方法
        methods
            function self = MFO_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 ='MFO';
                % 初始化每个飞蛾一个火焰
                self.flame_num = size;
            end
        end
        
        % 继承重写父类的方法
        methods (Access = protected)
            % 初始化种群
            function init(self)
                init@Algorithm_Impl(self)
                %初始化种群
                for i = 1:self.size
                    unit = MFO_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];
                    
                    flame = MFO_Unit();
                    % 随机初始化位置:rand(0,1).*(max-min)+min
                    flame.position = unifrnd(self.range_min_list,self.range_max_list);
                    % 计算适应度值
                    flame.value = self.cal_fitfunction(flame.position);
                    % 将个体加入群体数组
                    self.flame_list = [self.flame_list,flame];
                end
            end
            
            % 每一代的更新
            function update(self,iter)
                
                update@Algorithm_Impl(self,iter)
                % 更新t
                self.update_t(iter);
                % 更新火焰数量
                self.update_flame_num(iter);
                % 更新飞蛾位置
                self.update_moth();
                % 更新火焰位置
                self.update_flame();
            end
            
            % 更新全局变量t
            function update_t(self,iter)
                min_t = -2.0;
                max_t = -1.0;
                self.t = max_t - (max_t -min_t)*iter/self.iter_max;
            end
            
            % 更新全局变量,火焰数量
            function update_flame_num(self,iter)
                num = floor(self.size-self.size*iter/self.iter_max);
                if(num<=0)
                    num = 1;
                end
                self.flame_num = num;
            end
            
            % 更新飞蛾为位置
            function update_moth(self)
                for i = 1:self.size
                    % 随机选择火焰id
                    fi = randperm(self.flame_num,1);
                    moth = self.unit_list(i);
                    flame = self.flame_list(fi);
                    
                    r = unifrnd(self.t,1,1,self.dim);
                    new_pos = flame.position + abs(moth.position - flame.position).*exp(r).*cos(2*pi*r);
                    % 越界检查
                    new_pos = self.get_out_bound_value(new_pos);
                    new_value = self.cal_fitfunction(new_pos);
                    self.unit_list(i).value = new_value;
                    self.unit_list(i).position = new_pos;
                end
            end
            
            % 更新火焰位置
            function update_flame(self)
                % 将火焰和飞蛾从优到劣排序
                all_list = [self.flame_list,self.unit_list,];
                [value,index] = sort([all_list.value],'descend');
                % 取较优的部分为火焰
                for i = 1:self.flame_num
                    self.flame_list(i).position = all_list(index(i)).position;
                    self.flame_list(i).value = all_list(index(i)).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_moth_flame\MFO_Impl.m
    算法实现,继承于Base,图方便也可不写,直接用MFO_Base,这里为了命名一致。

    % 飞蛾扑火算法实现
    classdef MFO_Impl < MFO_Base
       
        % 外部可调用的方法
        methods
            function self = MFO_Impl(dim,size,iter_max,range_min_list,range_max_list)
                % 调用父类构造函数设置参数
                 self@MFO_Base(dim,size,iter_max,range_min_list,range_max_list);
            end
        end 
    end
    

    2.测试

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

    %% 清理之前的数据
    % 清除所有数据
    clear 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 = MFO_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/uswunrtx.html