優(yōu)化算法matlab實(shí)現(xiàn)(十二)煙花算法matlab實(shí)現(xiàn)

注意:此代碼實(shí)現(xiàn)的是求目標(biāo)函數(shù)最大值,求最小值可將適應(yīng)度函數(shù)乘以-1(框架代碼已實(shí)現(xiàn))。
注意:此代碼實(shí)現(xiàn)的是求目標(biāo)函數(shù)最大值,求最小值可將適應(yīng)度函數(shù)乘以-1(框架代碼已實(shí)現(xiàn))。
注意:此代碼實(shí)現(xiàn)的是求目標(biāo)函數(shù)最大值,求最小值可將適應(yīng)度函數(shù)乘以-1(框架代碼已實(shí)現(xiàn))。

1.代碼實(shí)現(xiàn)

不了解煙花算法可以先看看優(yōu)化算法筆記(十二)煙花算法
實(shí)現(xiàn)代碼前需要先完成優(yōu)化算法matlab實(shí)現(xiàn)(二)框架編寫(xiě)中的框架的編寫(xiě)。

文件名 描述
..\optimization algorithm\frame\Unit.m 個(gè)體
..\optimization algorithm\frame\Algorithm_Impl.m 算法主體

以及優(yōu)化算法matlab實(shí)現(xiàn)(四)測(cè)試粒子群算法中的測(cè)試函數(shù)、函數(shù)圖像的編寫(xiě)。

文件名 描述
..\optimization algorithm\frame\Get_Functions_details.m 測(cè)試函數(shù),求值用
..\optimization algorithm\frame\func_plot.m 函數(shù)圖像,畫(huà)圖用

煙花算法的個(gè)體沒(méi)有獨(dú)有屬性。
煙花算法個(gè)體
文件名:.. \optimization algorithm\algorithm_fireworks\FWA_Unit.m

% 煙花算法個(gè)體
classdef FWA_Unit < Unit
    
    properties
    end
    
    methods
        function self = FWA_Unit()
        end
    end
    
end

煙花算法主體
文件名:..\optimization algorithm\algorithm_fireworks\FWA_Base.m

% 煙花算法
classdef FWA_Base  < Algorithm_Impl
    
    properties
        % 算法名稱(chēng)
        name = 'FWA';
        % 生產(chǎn)火星數(shù)
        m;
        % 火星比例a
        a;
        % 火星比例b
        b;
        % 最大振幅
        amplitude_max;
        % 產(chǎn)生特殊火星數(shù)
        spec_num;
        % 記錄所有火星,選擇時(shí)用
        all_list;
    end
    
    % 外部可調(diào)用的方法
    methods
        function self = FWA_Base(dim,size,iter_max,range_min_list,range_max_list)
            % 調(diào)用父類(lèi)構(gòu)造函數(shù)
            self@Algorithm_Impl(dim,size,iter_max,range_min_list,range_max_list);
            self.name ='FWA';
            self.m=40;
            self.a=0.2;
            self.b=0.8;
            self.spec_num = 5;
            self.amplitude_max = 40;
        end
    end
    
    % 繼承重寫(xiě)父類(lèi)的方法
    methods (Access = protected)
        % 初始化種群
        function init(self)
            init@Algorithm_Impl(self)
            %初始化種群
            for i = 1:self.size
                unit = FWA_Unit();
                % 隨機(jī)初始化位置:rand(0,1).*(max-min)+min
                unit.position = unifrnd(self.range_min_list,self.range_max_list);
                % 計(jì)算適應(yīng)度值
                unit.value = self.cal_fitfunction(unit.position);
                % 將個(gè)體加入群體數(shù)組
                self.unit_list = [self.unit_list,unit];
            end
        end
        
        % 每一代的更新
        function update(self,iter)
            
            update@Algorithm_Impl(self,iter)
            % 生成火星
            for i = 1:self.size
                self.obtain_spark(i);
            end
            
            % 生成特殊火星
            for num = 1:self.spec_num
                rand = unidrnd(self.size);
                self.obtain_spec_spark(rand);
            end
            
            % 選擇火星保留到下一代
            self.select_sparks();
        end
        
        % 生成當(dāng)前個(gè)體的火星
        function obtain_spark(self,id)
            % 計(jì)算該個(gè)體應(yīng)生成的火星數(shù)
            num = self.get_spark_num(id);
            % 計(jì)算當(dāng)前個(gè)體振幅
            amplitude = self.get_spark_amplitude(id);
            % 依次生成n個(gè)火星
            for n = 1:num
                pos = self.unit_list(id).position;
                for d = 1:self.dim
                    if(rand<0.5)
                        pos(d) = pos(d)+ amplitude*unifrnd(-1,1);
                        % 超出范圍則取模
                        if (pos(d)>self.range_max_list(d) || pos(d)<self.range_min_list(d))
                            pos(d) = self.range_min_list(d)+mod(pos(d),(self.range_max_list(d)-self.range_min_list(d)));
                        end
                    end
                end
                unit = FWA_Unit();
                unit.position = pos;
                unit.value = self.cal_fitfunction(pos);
                self.all_list = [self.all_list,unit];
            end
        end
        
        % 生成當(dāng)前個(gè)體的特殊火星
        function obtain_spec_spark(self,id)
            pos = self.unit_list(id).position;
            for d = 1:self.dim
                if(rand<0.5)
                    pos(d) = pos(d)*normrnd(1,1);
                    % 超出范圍則取模
                    if (pos(d)>self.range_max_list(d) || pos(d)<self.range_min_list(d))
                        pos(d) = self.range_min_list(d)+mod(pos(d),(self.range_max_list(d)-self.range_min_list(d)));
                    end
                end
            end
            unit = FWA_Unit();
            unit.position = pos;
            unit.value = self.cal_fitfunction(pos);
            self.all_list = [self.all_list,unit];
        end
        
        % 選擇火星保留到下一代
        function select_sparks(self)
           self.all_list = [self.all_list,self.unit_list];
           [max_value,max_value_id] = max([self.all_list.value]);
           
           % 計(jì)算保留各個(gè)個(gè)體的概率
           rates=zeros(length(self.all_list(:)));
           % 總距離
           dist_sum=0;
           for i = 1:length(self.all_list(:))
               % 計(jì)算距離和(使用適應(yīng)度函數(shù)值計(jì)算)
               dist = 0;
               for j = 1:length(self.all_list(:))
                   dist = dist + abs(self.all_list(i).value-self.all_list(j).value);
               end
               dist_sum = dist_sum + dist;
               rates(i) = dist;
           end
           % 將概率歸一到[0,1]內(nèi)
           rates = rates/dist_sum;
           
           for i = 1:self.size
               if (i ==1)
                   % 保留最優(yōu)個(gè)體到第列表中的第一個(gè)
                   self.unit_list(1).position = self.all_list(max_value_id).position;
                   self.unit_list(1).value = self.all_list(max_value_id).value;
               else
                   for j = 1:length(self.all_list(:))
                       if(rand<rates(j))
                           self.unit_list(i).position = self.all_list(j).position;
                           self.unit_list(i).value = self.all_list(j).value;
                           break
                       end
                   end
               end
           end
           % 將數(shù)組清空
           self.all_list=[];
        end
        
        % 獲取當(dāng)前應(yīng)產(chǎn)生的火星數(shù)
        function num = get_spark_num(self,id)
            value_min = min([self.unit_list.value]);
            value_sum = sum([self.unit_list.value]);
            value = self.unit_list(id).value;
            num = round(self.m*(value-value_min+realmin('double'))/(value_sum-self.size*value_min+realmin('double')));
            if(num<self.a*self.m)
                num = round(self.a*self.m);
            elseif(num<self.b*self.m)
                num = round(self.b*self.m);
            else
            end
        end
        
        % 獲取當(dāng)前火星振幅
        function amplitude = get_spark_amplitude(self,id)
            value_max = max([self.unit_list.value]);
            value_sum = sum([self.unit_list.value]);
            value = self.unit_list(id).value;
            amplitude = self.amplitude_max*(value_max-value+realmin('double'))/(self.size*value_max-value_sum+realmin('double'));
        end
        
        % 獲取當(dāng)前最優(yōu)個(gè)體的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_fireworks\FWA_Impl.m
算法實(shí)現(xiàn),繼承于Base,圖方便也可不寫(xiě),直接用FWA_Base,這里為了命名一致。

%煙花算法實(shí)現(xiàn)
classdef FWA_Impl < FWA_Base
   
    % 外部可調(diào)用的方法
    methods
        function self = FWA_Impl(dim,size,iter_max,range_min_list,range_max_list)
            % 調(diào)用父類(lèi)構(gòu)造函數(shù)設(shè)置參數(shù)
             self@FWA_Base(dim,size,iter_max,range_min_list,range_max_list);
        end
    end 
end

2.測(cè)試

測(cè)試F1
文件名:..\optimization algorithm\algorithm_fireworks\Test.m

%% 清理之前的數(shù)據(jù)
% 清除所有數(shù)據(jù)
clear all;
% 清除窗口輸出
clc;

%% 添加目錄
% 將上級(jí)目錄中的frame文件夾加入路徑
addpath('../frame')


%% 選擇測(cè)試函數(shù)
Function_name='F1';
%[最小值,最大值,維度,測(cè)試函數(shù)]
[lb,ub,dim,fobj]=Get_Functions_details(Function_name);

%% 算法實(shí)例
% 種群數(shù)量
size = 5; % 煙花算法種群數(shù)要比其他少,一個(gè)個(gè)體會(huì)產(chǎn)生數(shù)個(gè)火星
% 最大迭代次數(shù)
iter_max = 1000;
% 取值范圍上界
range_max_list = ones(1,dim)*ub;
% 取值范圍下界
range_min_list = ones(1,dim)*lb;

% 實(shí)例化煙花算法類(lèi)
base = FWA_Impl(dim,size,iter_max,range_min_list,range_max_list);
base.is_cal_max = false;
% 確定適應(yīng)度函數(shù)
base.fitfunction = fobj;
% 運(yùn)行
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);
% 繪制曲線,由于算法是求最大值,適應(yīng)度函數(shù)為求最小值,故乘了-1,此時(shí)去掉-1
semilogy((base.value_best_history),'Color','r')
title('Objective space')
xlabel('Iteration');
ylabel('Best score obtained so far');
% 將坐標(biāo)軸調(diào)整為緊湊型
axis tight
% 添加網(wǎng)格
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)]);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容