優(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)(二)框架編寫中的框架的編寫。

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

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

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

麻雀算法的個(gè)體沒有獨(dú)有屬性。
麻雀算法個(gè)體
文件名:.. \optimization algorithm\algorithm_sparrow_search\SSA_Unit.m

% 麻雀搜索算法個(gè)體
classdef SSA_Unit < Unit
    
    properties
    end
    
    methods
        function self = SSA_Unit()
        end
    end
end

麻雀算法算法主體
文件名:..\optimization algorithm\algorithm_sparrow_search\SSA_Base.m

% 麻雀搜索算法
classdef SSA_Base  < Algorithm_Impl
    
    properties
        % 算法名稱
        name = 'SSA';

        ST = 0.8;
        % 發(fā)現(xiàn)者比例
        PROD_rate = 1.0;
        % 偵察者比例
        SD_rate = 0.2;
        % 當(dāng)前群體中最差位置
        position_worst;
        % 當(dāng)前群體中最差適應(yīng)度
        value_worst;
    end
    
    % 外部可調(diào)用的方法
    methods
        function self = SSA_Base(dim,size,iter_max,range_min_list,range_max_list)
            % 調(diào)用父類構(gòu)造函數(shù)
            self@Algorithm_Impl(dim,size,iter_max,range_min_list,range_max_list);
            self.name ='SSA';
        end
    end
    
    % 繼承重寫父類的方法
    methods (Access = protected)
        % 初始化種群
        function init(self)
            init@Algorithm_Impl(self)
            %初始化種群
            for i = 1:self.size
                unit = SSA_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)
            
            % 從優(yōu)到劣排序
            [value,index] = sort([self.unit_list.value],'descend');
            self.position_worst = self.unit_list(index(end)).position;
            self.value_worst = self.unit_list(index(end)).value;
            
            for i = 1:self.size
                if(i<self.size*self.PROD_rate)
                    % 較好的個(gè)體作為發(fā)現(xiàn)者
                    self.update_producer(i,index);
                else
                    % 其余作為跟隨者
                    self.update_scrounger(i,index);
                end
            end
            
            % 隨機(jī)亂序id
            id_list = randperm(self.size);
            for i = 1:floor(self.size*self.SD_rate)
                % 取一定比例的作為偵察者
                self.update_perceive_danger(id_list(i))
            end
        end     
        
        % 更新發(fā)現(xiàn)者
        function update_producer(self,i,index_list)
            id = index_list(i);
            if rand<self.ST
                new_pos = self.unit_list(id).position.*exp(-i./(unifrnd(0,1,1,self.dim)*self.iter_max));
                new_pos = self.get_out_bound_value(new_pos);
            else
                new_pos = self.unit_list(id).position + normrnd(0,1,1 ,self.dim);
                new_pos = self.get_out_bound_value(new_pos);
            end
            % 保留更優(yōu)的位置
            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
        
        % 更新跟隨者
        function update_scrounger(self,i,index_list)
            id = index_list(i);
            if i > self.size/2
                new_pos = normrnd(0,1,1 ,self.dim).*exp((self.position_worst-self.unit_list(id).position)/(i^2));
                new_pos = self.get_out_bound_value(new_pos);
            else
                % 隨機(jī)選擇一個(gè)發(fā)現(xiàn)者
                p_i = randperm(floor(self.size*self.SD_rate));
                p_id = index_list(p_i(1));
                rnd = unidrnd(2,1,self.dim)*2-3;
                sum_rnd = sum(rnd);
                new_pos = self.unit_list(p_id).position+(self.unit_list(id).position-self.unit_list(p_id).position)*sum_rnd/self.dim;
            end
             % 保留更優(yōu)的位置
            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
        
        % 更新偵察者
        function update_perceive_danger(self,id)
            if self.unit_list(id).value < self.value_best
                new_pos = self.position_best+normrnd(0,1,1 ,self.dim).*self.unit_list(id).position;
                new_pos = self.get_out_bound_value(new_pos);
            else
                new_pos = self.unit_list(id).position + unifrnd(-1,1,1,self.dim).*(self.unit_list(id).position-self.position_worst)/(self.unit_list(id).value-self.value_worst+realmin('double'));
                new_pos = self.get_out_bound_value(new_pos);
            end

            % 保留更優(yōu)的位置
            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
        
        
        % 獲取當(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_sparrow_search\SSA_Impl.m
算法實(shí)現(xiàn),繼承于Base,圖方便也可不寫,直接用SSA_Base,這里為了命名一致。

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

2.測試

測試F1
文件名:..\optimization algorithm\algorithm_sparrow_search\Test.m

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

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


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

%% 算法實(shí)例
% 種群數(shù)量
size = 50;
% 最大迭代次數(shù)
iter_max = 1000;
% 取值范圍上界
range_max_list = ones(1,dim).*ub;
% 取值范圍下界
range_min_list = ones(1,dim).*lb;

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

推薦閱讀更多精彩內(nèi)容