注意:此代碼實現的是求目標函數最大值,求最小值可將適應度函數乘以-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_brain_storm\BSO_Unit.m
% 頭腦風暴算法個體
classdef BSO_Unit < Unit
properties
end
methods
function self = BSO_Unit()
end
end
end
頭腦風暴算法算法主體
文件名:..\optimization algorithm\algorithm_brain_storm\BSO_Base.m
% 頭腦風暴算法
classdef BSO_Base < Algorithm_Impl
properties
% 算法名稱
name = 'BSO';
% 聚類數
cluster_num = 5;
p5a = 0.2;
p6b = 0.8;
p6b3 = 0.4;
p6c = 0.5;
k = 20;
% 聚類分組map,key為分組序號,value為個體列表
cluster_map = containers.Map;
% 列表,保存對應的聚類中心
cluster_center_list;
end
% 外部可調用的方法
methods
function self = BSO_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 ='BSO';
end
end
% 繼承重寫父類的方法
methods (Access = protected)
% 初始化種群
function init(self)
init@Algorithm_Impl(self)
%初始化種群
for i = 1:self.size
unit = BSO_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
% 初始化分組
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;
% 每代最多替換一個中心
if (rand<self.p5a)
self.generate_from_center();
num = 1;
end
% 每代只生成size個新個體
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);
% 按照亂序序列對聚類數取模劃分聚類
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
% 取出每個聚類的聚類中心
for i = 1:self.cluster_num
% 取出該聚類的所有個體id
cluster_id_list = self.cluster_map(num2str(i));
% 遍歷取最優的id為聚類中心
self.cluster_center_list(i)=cluster_id_list(1);
for j = 2:length(cluster_id_list(:))
% 如果當前個體的值優于聚類中心的值,則替換
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
% 一定概率替換一個center
function generate_from_center(self)
% 隨機選擇一個聚類
cluster_id = randperm(self.cluster_num,1);
% 取出聚類中心
cluster_center_id = self.cluster_center_list(cluster_id);
% 隨機初始化中心
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
% 從個人中生成
function generate_from_individuals(self,iter)
if(rand<self.p6b)
% 隨機選擇一個聚類
cluster_id = randperm(self.cluster_num,1);
if(rand<self.p6b3)
% 選擇該聚類的聚類中心
center_id = self.cluster_center_list(cluster_id);
% 加上一個隨機數
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));
% 選擇該聚類的隨機個體
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
% 隨機選擇兩個聚類
cluster_ids = randperm(self.cluster_num,2);
if(rand<self.p6c)
% 合并兩個聚類中心
% 選取這兩個聚類中心
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
% 合并兩個個體
% 選擇這兩個聚類的隨機個體
cluster_id1_list = self.cluster_map(num2str(cluster_ids(1)));
% 隨機選擇該聚類中的index
rand_index1 = randperm(length(cluster_id1_list(:)),1);
% 該index對應在種群中的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
% 合并兩個個體思想
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
% 獲取當前最優個體的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
算法實現,繼承于Base,圖方便也可不寫,直接用BSO_Base,這里為了命名一致。
% 頭腦風暴算法實現
classdef BSO_Impl < BSO_Base
% 外部可調用的方法
methods
function self = BSO_Impl(dim,size,iter_max,range_min_list,range_max_list)
% 調用父類構造函數設置參數
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
%% 清理之前的數據
% 清除所有數據
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 = BSO_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)]);