優化算法matlab實現(十六)混合蛙跳算法matlab實現

注意:此代碼實現的是求目標函數最大值,求最小值可將適應度函數乘以-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_shuffled_frog_leaping\SFLA_Unit.m

% 混合蛙跳算法個體
classdef SFLA_Unit < Unit
    
    properties
    end
    
    methods
        function self = SFLA_Unit()
        end
    end
    
end

混合蛙跳算法算法主體
文件名:..\optimization algorithm\algorithm_shuffled_frog_leaping\SFLA_Base.m

% 混合蛙跳算法
classdef SFLA_Base  < Algorithm_Impl
    
    properties
        % 算法名稱
        name = 'SFLA';
        % 分組數
        meme_num;
        % 分組id
        meme_list;
    end
    
    % 外部可調用的方法
    methods
        function self = SFLA_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 ='SFLA';
            % 每兩只青蛙一組
            self.meme_num = floor(self.size/2);
            self.meme_list = [];
        end
    end
    
    % 繼承重寫父類的方法
    methods (Access = protected)
        % 初始化種群
        function init(self)
            init@Algorithm_Impl(self)
            %初始化種群
            for i = 1:self.size
                unit = SFLA_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];
                for i = 1:self.meme_num
                    self.meme_list=[self.meme_list,[]];
                end
                disp(self.meme_list)
            end
        end
        
        % 每一代的更新
        function update(self,iter)
            update@Algorithm_Impl(self,iter)
            for i = 1:self.meme_num
                self.meme_list(i,:) = self.divide_meme(i);
            end
            for i = 1:self.meme_num
                self.update_worst_position(self.meme_list(i));
            end
        end     
       
        % 分組
        function meme_list = divide_meme(self,meme_id)
            % 求最大值,降序排列
            [value,index] = sort([self.unit_list.value],'descend');
            % 對index 取模分組
            meme_list = [];
            for s = meme_id:self.meme_num:self.size
                meme_list = [meme_list,index(s)];
            end
        end
        
        % 更新每一組的最差個體位置
        function update_worst_position(self,id_list)
            % 因為已經排序,故id_list中的第1個體就是本組的最優個體
            % 最后一個個體是最差個體,需要更新的個體
            meme_best_id = id_list(1);
            meme_worst_id = id_list(end);
            % 1.向著組內最優個體前進
            new_pos = self.unit_list(meme_worst_id).position+unifrnd(0,2,1,self.dim).*(self.unit_list(meme_best_id).position-self.unit_list(meme_worst_id).position);
            new_pos = self.get_out_bound_value(new_pos);
            new_value = self.cal_fitfunction(new_pos);
            
            if(new_value > self.unit_list(meme_worst_id).value)
                % 如果新位置更好,直接結束
                self.unit_list(meme_worst_id).value = new_value;
                self.unit_list(meme_worst_id).position = new_pos;
                return;
            end
            
            % 2.向著全局最優個體前進
            new_pos = self.unit_list(meme_worst_id).position+unifrnd(0,2,1,self.dim).*(self.position_best-self.unit_list(meme_worst_id).position);
            new_pos = self.get_out_bound_value(new_pos);
            new_value = self.cal_fitfunction(new_pos);
            
            if(new_value > self.unit_list(meme_worst_id).value)
                % 如果新位置更好,直接結束
                self.unit_list(meme_worst_id).value = new_value;
                self.unit_list(meme_worst_id).position = new_pos;
                return;
            end
            
            % 3.隨機選擇一個位置,不管好壞
            new_pos = unifrnd(self.range_min_list,self.range_max_list);
            new_value = self.cal_fitfunction(new_pos);
            self.unit_list(meme_worst_id).value = new_value;
            self.unit_list(meme_worst_id).position = new_pos;
        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_shuffled_frog_leaping\SFLA_Impl.m
算法實現,繼承于Base,圖方便也可不寫,直接用SFLA_Base,這里為了命名一致。

% 混合蛙跳算法實現
classdef SFLA_Impl < SFLA_Base
   
    % 外部可調用的方法
    methods
        function self = SFLA_Impl(dim,size,iter_max,range_min_list,range_max_list)
            % 調用父類構造函數設置參數
             self@SFLA_Base(dim,size,iter_max,range_min_list,range_max_list);
        end
    end 
end

2.測試

測試F1
文件名:..\optimization algorithm\algorithm_shuffled_frog_leaping\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 = SFLA_Impl(dim,size,iter_max,range_min_list,range_max_list);
% 告訴算法求不是求最大值
base.is_cal_max = false;
% 確定適應度函數
base.fitfunction =fobj;
% 運行
base.run();

%% 繪制圖像
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);
% 繪制曲線
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)]);
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,739評論 6 534
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,634評論 3 419
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,653評論 0 377
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,063評論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,835評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,235評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,315評論 3 442
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,459評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,000評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,819評論 3 355
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,004評論 1 370
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,560評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,257評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,676評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,937評論 1 288
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,717評論 3 393
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,003評論 2 374

推薦閱讀更多精彩內容