优化算法matlab实现(二十四)帝王蝶算法matlab实现

分享
手机软件开发 2024-10-7 09:23:59 38 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_monarch_butterfly\MBO_Unit.m
% 帝王蝶算法个体classdef MBO_Unit < Unit        properties    end        methods        function self = MBO_Unit()        end    endend帝王蝶算法算法主体
文件名:..\optimization algorithm\algorithm_monarch_butterfly\MBO_Base.m
% 帝王蝶算法classdef MBO_Base  < Algorithm_Impl        properties        % 算法名称        name = 'MBO';        % 迁徙概率        p = 5.0/12;            bar = 5.0/12;            peri = 1.2;            step_max = 100.0;                group1_ids = [];        group2_ids = [];    end        % 外部可调用的方法    methods        function self = MBO_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 ='MBO';        end    end        % 继续重写父类的方法    methods (Access = protected)        % 初始化种群        function init(self)            init@Algorithm_Impl(self)            %初始化种群            for i = 1:self.size                unit = MBO_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.divide_group();            % 迁徙            self.migration();            % 顺应情况            self.adjusting(iter);        end                % 分别种群        function divide_group(self)            % 举行迁徙的个体id            self.group1_ids = [];            % 举行顺应情况的个体id            self.group2_ids = [];            for i = 1:self.size                if rand< self.p                    self.group1_ids = [self.group1_ids,i];                else                    self.group2_ids = [self.group2_ids,i];                end            end        end               % 迁徙        function migration(self)            for index = 1:length(self.group1_ids()                r = unifrnd(0,1,1,self.dim);                new_pos = zeros(1,self.dim);                for d = 1:self.dim                    if (r(d)*self.peri) < self.p                        % 从群体1中随机选择个体                        r_index = randperm(length(self.group1_ids(),1);                        r_id = self.group1_ids(r_index);                        new_pos(d) = self.unit_list(r_id).position(d);                    else                         % 从群体2中随机选择个体                        r_index = randperm(length(self.group2_ids(),1);                        r_id = self.group2_ids(r_index);                        new_pos(d) = self.unit_list(r_id).position(d);                    end                end                id = self.group1_ids(index);                new_value = self.cal_fitfunction(new_pos);                if new_value > self.unit_list(id).value                    self.unit_list(id).value = new_value;                    self.unit_list(id).position = new_pos;                end            end        end                % 顺应情况        function adjusting(self,iter)            for index = 1:length(self.group2_ids()                r = unifrnd(0,1,1,self.dim);                r1 = unifrnd(0,1,1,self.dim);                r_levy = Levy(self.dim);                                % 默认取值为全局最优                new_pos = self.position_best;                for d = 1:self.dim                    if r(d) >=self.p                        % 从群体2中随机选择个体                        r_index = randperm(length(self.group2_ids(),1);                        r_id = self.group2_ids(r_index);                        new_pos(d) = self.unit_list(r_id).position(d);                        if r1(d) > self.bar                            new_pos(d) = new_pos(d) + self.step_max/(iter*iter)*(r_levy(d)-0.5);                        end                    end                end                                id = self.group2_ids(index);                % 越界查抄                new_pos = self.get_out_bound_value(new_pos);                new_value = self.cal_fitfunction(new_pos);                if new_value > self.unit_list(id).value                    self.unit_list(id).value = new_value;                    self.unit_list(id).position = new_pos;                end            end        end                        % 获取当前最优个体的id        function best_id=get_best_id(self)            % 求最大值则降序分列            [value,index] = sort([self.unit_list.value],'descend');            best_id = index(1);        end            endendfunction 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_monarch_butterfly\MBO_Impl.m
算法实现,继续于Base,图方便也可不写,直接用MBO_Base,这里为了定名同等。
% 帝王蝶算法实现classdef MBO_Impl < MBO_Base       % 外部可调用的方法    methods        function self = MBO_Impl(dim,size,iter_max,range_min_list,range_max_list)            % 调用父类构造函数设置参数             self@MBO_Base(dim,size,iter_max,range_min_list,range_max_list);        end    end end2.测试

测试F1
文件名:..\optimization algorithm\algorithm_monarch_butterfly\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 = MBO_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 spacesubplot(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 spacesubplot(1,2,2);% 绘制曲线,由于算法是求最大值,顺应度函数为求最小值,故乘了-1,此时去掉-1semilogy((base.value_best_history),'Color','r')title('Objective space')xlabel('Iteration');ylabel('Best score obtained so far');% 将坐标轴调解为紧凑型axis tight% 添加网格grid on% 四边都表现刻度box offlegend(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)]);
您需要登录后才可以回帖 登录 | 立即注册

Powered by CangBaoKu v1.0 小黑屋藏宝库It社区( 冀ICP备14008649号 )

GMT+8, 2024-10-18 16:49, Processed in 0.124431 second(s), 32 queries.© 2003-2025 cbk Team.

快速回复 返回顶部 返回列表