注意:此代碼實(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è)體有獨(dú)有屬性:刺激強(qiáng)度曙聂,是一個(gè)數(shù)值断国。
蝴蝶算法個(gè)體
文件名:.. \optimization algorithm\algorithm_butterfly\BFA_Unit.m
% 蝴蝶算法個(gè)體
classdef BFA_Unit < Unit
properties
% 刺激強(qiáng)度
intensity = 1.0;
end
methods
function self = BFA_Unit()
end
end
end
蝴蝶算法算法主體
文件名:..\optimization algorithm\algorithm_butterfly\BFA_Base.m
% 蝴蝶算法
classdef BFA_Base < Algorithm_Impl
properties
% 算法名稱
name = 'BFA';
% 指數(shù)
exponent = 0.01;
% 模式狀態(tài)
c = 0.1;
% 全局搜索概率
p = 0.8;
end
% 外部可調(diào)用的方法
methods
function self = BFA_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 ='BFA';
end
end
% 繼承重寫(xiě)父類的方法
methods (Access = protected)
% 初始化種群
function init(self)
init@Algorithm_Impl(self)
%初始化種群
for i = 1:self.size
unit = BFA_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
if rand< self.p
% 全局搜索
self.get_global_search_position(i);
else
% 局部搜索
self.get_local_search_position(i);
end
end
end
% 全局搜索位置
function get_global_search_position(self,id)
r = unifrnd(0,1,1,self.dim).^2;
unit = self.unit_list(id);
new_pos = unit.position+(r.*self.position_best-unit.position)*self.c*unit.intensity^self.exponent.*Levy(self.dim);
% 越界檢查
new_pos = self.get_out_bound_value(new_pos);
new_value = self.cal_fitfunction(new_pos);
self.unit_list(id).value = new_value;
self.unit_list(id).position = new_pos;
end
% 局部搜索位置
function get_local_search_position(self,id)
% 隨機(jī)選取兩個(gè)個(gè)體
r_ids = randperm(self.size,2);
r = unifrnd(0,1,1,self.dim).^2;
unit = self.unit_list(id);
% 獲取兩個(gè)隨機(jī)個(gè)體
unit1 = self.unit_list(r_ids(1));
unit2 = self.unit_list(r_ids(2));
new_pos = unit.position+(r.*unit1.position-unit2.position)*self.c*unit.intensity^self.exponent.*Levy(self.dim);
% 越界檢查
new_pos = self.get_out_bound_value(new_pos);
new_value = self.cal_fitfunction(new_pos);
self.unit_list(id).value = new_value;
self.unit_list(id).position = 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 o=Levy(d)
beta=1.5;
sigma=(gamma(1+beta)*sin(pi*beta/2)/(gamma((1+beta)/2)*beta*2^((beta-1)/2)))^(1/beta);
u=randn(1,d)*sigma;v=randn(1,d);step=u./abs(v).^(1/beta);
o=step;
end
文件名:..\optimization algorithm\algorithm_butterfly\BFA_Impl.m
算法實(shí)現(xiàn)薄疚,繼承于Base,圖方便也可不寫(xiě)街夭,直接用BFA_Base,這里為了命名一致埃碱。
% 蝴蝶算法實(shí)現(xiàn)
classdef BFA_Impl < BFA_Base
% 外部可調(diào)用的方法
methods
function self = BFA_Impl(dim,size,iter_max,range_min_list,range_max_list)
% 調(diào)用父類構(gòu)造函數(shù)設(shè)置參數(shù)
self@BFA_Base(dim,size,iter_max,range_min_list,range_max_list);
end
end
end
2.測(cè)試
測(cè)試F1
文件名:..\optimization algorithm\algorithm_butterfly\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 = 50;
% 最大迭代次數(shù)
iter_max = 1000;
% 取值范圍上界
range_max_list = ones(1,dim).*ub;
% 取值范圍下界
range_min_list = ones(1,dim).*lb;
% 實(shí)例化蝴蝶算法類
base = BFA_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)]);