優(yōu)化算法matlab實(shí)現(xiàn)(十九)頭腦風(fēng)暴算法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)

不了解頭腦風(fēng)暴算法可以先看看優(yōu)化算法筆記(十九)頭腦風(fēng)暴算法
實(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ù)圖像闲孤,畫圖用

頭腦風(fēng)暴算法的個(gè)體沒有獨(dú)有屬性谆级。
頭腦風(fēng)暴算法個(gè)體
文件名:.. \optimization algorithm\algorithm_brain_storm\BSO_Unit.m

% 頭腦風(fēng)暴算法個(gè)體
classdef BSO_Unit < Unit
    
    properties
    end
    
    methods
        function self = BSO_Unit()
        end
    end
    
end

頭腦風(fēng)暴算法算法主體
文件名:..\optimization algorithm\algorithm_brain_storm\BSO_Base.m

% 頭腦風(fēng)暴算法
classdef BSO_Base  < Algorithm_Impl
    
    properties
        % 算法名稱
        name = 'BSO';
        % 聚類數(shù)
        cluster_num = 5;
        p5a = 0.2;
        p6b = 0.8;
        p6b3 = 0.4;
        p6c = 0.5;
        k = 20;
        
        % 聚類分組map,key為分組序號(hào),value為個(gè)體列表
        cluster_map = containers.Map;
        % 列表讼积,保存對應(yīng)的聚類中心
        cluster_center_list;
        
    end
    
    % 外部可調(diào)用的方法
    methods
        function self = BSO_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 ='BSO';
        end
    end
    
    % 繼承重寫父類的方法
    methods (Access = protected)
        % 初始化種群
        function init(self)
            init@Algorithm_Impl(self)
            %初始化種群
            for i = 1:self.size
                unit = BSO_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
            % 初始化分組
            for i = 1:self.cluster_num
                self.cluster_map(num2str(i))=[];
            end
            % 初始化中心
            self.cluster_center_list = ones(1,self.cluster_num);
        end
        
        % 每一代的更新
        function update(self,iter)
            
            update@Algorithm_Impl(self,iter)
            
            self.divide_cluster();
            
            num = 0;
            % 每代最多替換一個(gè)中心
            if (rand<self.p5a)
                self.generate_from_center();
                num = 1;
            end
            
            % 每代只生成size個(gè)新個(gè)體
            while(num<self.size)
                self.generate_from_individuals(iter);
                num = num + 1;
            end
           
        end     
        
        % 劃分聚類
        function divide_cluster(self)
             % 初始化分組
            for i = 1:self.cluster_num
                self.cluster_map(num2str(i))=[];
            end
            % 初始化中心
            self.cluster_center_list = ones(1,self.cluster_num);
            
            % 將種群序列亂序排列
            rand_index = randperm(self.size);
            % 按照亂序序列對聚類數(shù)取模劃分聚類
            for i = 1:self.size
                % 取模的值+1為聚類id
                cluster_index = mod(i,self.cluster_num)+1;
                self.cluster_map(num2str(cluster_index))=[self.cluster_map(num2str(cluster_index)),rand_index(i)];
            end
            
            % 取出每個(gè)聚類的聚類中心
            for i = 1:self.cluster_num
                % 取出該聚類的所有個(gè)體id
                cluster_id_list = self.cluster_map(num2str(i));
                % 遍歷取最優(yōu)的id為聚類中心
                self.cluster_center_list(i)=cluster_id_list(1);
                for j = 2:length(cluster_id_list(:))
                    % 如果當(dāng)前個(gè)體的值優(yōu)于聚類中心的值肥照,則替換
                    if(self.unit_list(cluster_id_list(j)).value>self.unit_list(self.cluster_center_list(i)).value)
                        self.cluster_center_list(i) = cluster_id_list(j);
                    end
                end
            end
        end
        
        % 一定概率替換一個(gè)center
        function generate_from_center(self)
            % 隨機(jī)選擇一個(gè)聚類
            cluster_id = randperm(self.cluster_num,1);
            % 取出聚類中心
            cluster_center_id = self.cluster_center_list(cluster_id);
            % 隨機(jī)初始化中心
            new_pos = unifrnd(self.range_min_list,self.range_max_list);
            new_value = self.cal_fitfunction(new_pos);
            self.unit_list(cluster_center_id).position = new_pos;
            self.unit_list(cluster_center_id).value = new_value;
        end
        
        % 從個(gè)人中生成
        function generate_from_individuals(self,iter)
            if(rand<self.p6b)
                % 隨機(jī)選擇一個(gè)聚類
                cluster_id = randperm(self.cluster_num,1);
                if(rand<self.p6b3)
                    % 選擇該聚類的聚類中心
                    center_id = self.cluster_center_list(cluster_id);
                    % 加上一個(gè)隨機(jī)數(shù)
                    new_pos = self.generate_add_value(iter,self.unit_list(center_id).position);
                    new_value = self.cal_fitfunction(new_pos);
                    if(new_value > self.unit_list(center_id).value)
                        self.unit_list(center_id).position = new_pos;
                        self.unit_list(center_id).value = new_value;
                    end
                else
                    % 選擇該聚類的id
                    cluster_id_list = self.cluster_map(num2str(cluster_id));
                    % 選擇該聚類的隨機(jī)個(gè)體
                    rand_index = randperm(length(cluster_id_list(:)),1);
                    rand_id = cluster_id_list(rand_index);
                    new_pos = self.generate_add_value(iter,self.unit_list(rand_id).position);
                    new_value = self.cal_fitfunction(new_pos);
                    if(new_value > self.unit_list(rand_id).value)
                        self.unit_list(rand_id).position = new_pos;
                        self.unit_list(rand_id).value = new_value;
                    end
                end
            else
                % 隨機(jī)選擇兩個(gè)聚類
                cluster_ids = randperm(self.cluster_num,2);
                if(rand<self.p6c)
                    % 合并兩個(gè)聚類中心
                    % 選取這兩個(gè)聚類中心
                    center_id1 = self.cluster_center_list(cluster_ids(1));
                    center_id2 = self.cluster_center_list(cluster_ids(2));
                    % 獲得合并后的位置
                    new_pos = self.generate_combine_unit(self.unit_list(center_id1).position,self.unit_list(center_id2).position);
                    new_pos = self.generate_add_value(iter,new_pos);
                    new_value = self.cal_fitfunction(new_pos);
                    if (new_value > self.unit_list(center_id1).value)
                        self.unit_list(center_id1).value = new_value;
                        self.unit_list(center_id1).position = new_pos;
                    elseif(new_value > self.unit_list(center_id2).value)
                        self.unit_list(center_id2).value = new_value;
                        self.unit_list(center_id2).position = new_pos;
                    end
                else
                    % 合并兩個(gè)個(gè)體
                    
                    % 選擇這兩個(gè)聚類的隨機(jī)個(gè)體
                    cluster_id1_list = self.cluster_map(num2str(cluster_ids(1)));
                    % 隨機(jī)選擇該聚類中的index
                    rand_index1 = randperm(length(cluster_id1_list(:)),1);
                    % 該index對應(yīng)在種群中的id
                    rand_id1 = cluster_id1_list(rand_index1);
                    cluster_id2_list = self.cluster_map(num2str(cluster_ids(2)));
                    rand_index2 = randperm(length(cluster_id2_list(:)),1);
                    rand_id2 = cluster_id2_list(rand_index2);
                    
                    % 獲得合并后的位置
                    new_pos = self.generate_combine_unit(self.unit_list(rand_id1).position,self.unit_list(rand_id2).position);
                    new_pos = self.generate_add_value(iter,new_pos);
                    new_value = self.cal_fitfunction(new_pos);
                    if (new_value > self.unit_list(rand_id1).value)
                        self.unit_list(rand_id1).value = new_value;
                        self.unit_list(rand_id1).position = new_pos;
                    elseif(new_value > self.unit_list(rand_id2).value)
                        self.unit_list(rand_id2).value = new_value;
                        self.unit_list(rand_id2).position = new_pos;
                    end
                end
            end
                   
        end
        
        % 合并兩個(gè)個(gè)體思想
        function new_pos = generate_combine_unit(self,pos1,pos2)
            r = unifrnd(0,1,1,self.dim);
            new_pos = pos1.*r+(1-r).*pos2;
            new_pos = self.get_out_bound_value(new_pos);
        end
        
        % 錦上添花,添加偏移
        function new_pos = generate_add_value(self,iter,pos)
            new_pos = pos+sigmod((0.5*self.iter_max-iter)/self.k)*unifrnd(0,1,1,self.dim).*normrnd(0,1,1 ,self.dim);
            new_pos = self.get_out_bound_value(new_pos);
        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

function value = sigmod(x)
value = 1./(1+exp(-x));
end

文件名:..\optimization algorithm\algorithm_brain_storm\BSO_Impl.m
算法實(shí)現(xiàn)勤众,繼承于Base,圖方便也可不寫舆绎,直接用BSO_Base,這里為了命名一致们颜。

% 頭腦風(fēng)暴算法實(shí)現(xiàn)
classdef BSO_Impl < BSO_Base
   
    % 外部可調(diào)用的方法
    methods
        function self = BSO_Impl(dim,size,iter_max,range_min_list,range_max_list)
            % 調(diào)用父類構(gòu)造函數(shù)設(shè)置參數(shù)
             self@BSO_Base(dim,size,iter_max,range_min_list,range_max_list);
        end
    end 
end

2.測試

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

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

%% 添加框架路徑
% 將上級(jí)目錄中的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í)例化頭腦風(fēng)暴算法類
base = BSO_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();

%% 繪制圖像
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');
% 將坐標(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)系作者
  • 序言:七十年代末努溃,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子阻问,更是在濱河造成了極大的恐慌梧税,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,000評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件称近,死亡現(xiàn)場離奇詭異第队,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)刨秆,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,745評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門凳谦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人衡未,你說我怎么就攤上這事尸执。” “怎么了缓醋?”我有些...
    開封第一講書人閱讀 168,561評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵剔交,是天一觀的道長。 經(jīng)常有香客問我改衩,道長岖常,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,782評(píng)論 1 298
  • 正文 為了忘掉前任葫督,我火速辦了婚禮竭鞍,結(jié)果婚禮上板惑,老公的妹妹穿的比我還像新娘。我一直安慰自己偎快,他們只是感情好冯乘,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,798評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著晒夹,像睡著了一般裆馒。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上丐怯,一...
    開封第一講書人閱讀 52,394評(píng)論 1 310
  • 那天喷好,我揣著相機(jī)與錄音,去河邊找鬼读跷。 笑死梗搅,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的效览。 我是一名探鬼主播无切,決...
    沈念sama閱讀 40,952評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼丐枉!你這毒婦竟也來了哆键?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,852評(píng)論 0 276
  • 序言:老撾萬榮一對情侶失蹤瘦锹,失蹤者是張志新(化名)和其女友劉穎籍嘹,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體沼本,經(jīng)...
    沈念sama閱讀 46,409評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡噩峦,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,483評(píng)論 3 341
  • 正文 我和宋清朗相戀三年锭沟,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了抽兆。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,615評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡族淮,死狀恐怖辫红,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情祝辣,我是刑警寧澤贴妻,帶...
    沈念sama閱讀 36,303評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站蝙斜,受9級(jí)特大地震影響名惩,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜孕荠,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,979評(píng)論 3 334
  • 文/蒙蒙 一娩鹉、第九天 我趴在偏房一處隱蔽的房頂上張望攻谁。 院中可真熱鬧,春花似錦弯予、人聲如沸戚宦。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,470評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽受楼。三九已至,卻和暖如春呼寸,著一層夾襖步出監(jiān)牢的瞬間艳汽,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,571評(píng)論 1 272
  • 我被黑心中介騙來泰國打工等舔, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留骚灸,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,041評(píng)論 3 377
  • 正文 我出身青樓慌植,卻偏偏與公主長得像甚牲,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子蝶柿,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,630評(píng)論 2 359