注意:此代碼實(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)(二)框架編寫中的框架的編寫曹抬。
實(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)(四)測(cè)試粒子群算法中的測(cè)試函數(shù)急鳄、函數(shù)圖像的編寫谤民。
文件名 | 描述 |
---|---|
..\optimization algorithm\frame\Get_Functions_details.m | 測(cè)試函數(shù),求值用 |
..\optimization algorithm\frame\func_plot.m | 函數(shù)圖像攒岛,畫圖用 |
人工蜂群算法的每個(gè)個(gè)體有幾個(gè)獨(dú)有屬性:蜜源開采次數(shù)赖临、蜜蜂類型胞锰。
人工蜂群算法個(gè)體
文件名:.. \optimization algorithm\algorithm_artificial_bee_colony\ABC_Unit.m
% 人工蜂群算法個(gè)體
classdef ABC_Unit < Unit
properties
% 蜜蜂類型 1.雇傭蜂灾锯,2.非雇傭蜂,3.偵查蜂
type
% 蜜源最大開采次數(shù)
times
end
methods
function self = ABC_Unit()
end
end
end
人工蜂群算法主體
文件名:..\optimization algorithm\ algorithm_artificial_bee_colony\ABC_Base.m
% 人工蜂群算法
classdef ABC_Base < Algorithm_Impl
properties
%算法名稱
name = 'ABC';
% 蜜源最大開采次數(shù)
time_max = 60;
% 雇傭蜂占群體中的概率
employed_rate = 0.2;
% 雇傭蜂類型
EMPLOYED = 1;
% 非雇傭蜂類型
UNEMPLOYED = 2;
% 偵查蜂類型
SCOUT = 3;
end
% 外部可調(diào)用的方法
methods
function self = ABC_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 ='ABC';
end
end
% 繼承重寫父類的方法
methods (Access = protected)
% 初始化種群
function init(self)
init@Algorithm_Impl(self)
% 初始化種群
for i = 1:self.size
unit = ABC_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);
% 設(shè)置初始開采次數(shù)為0
unit.times = 0;
% 設(shè)置默認(rèn)身份為偵查蜂
unit.type = self.SCOUT;
% 將個(gè)體加入群體數(shù)組
self.unit_list = [self.unit_list,unit];
end
end
% 每一代的更新
function update(self,iter)
% 設(shè)置雇傭蜂
self.set_employed();
% 更新個(gè)體
for i = 1:self.size
% 根據(jù)不同類型選擇不同的更新方式
switch self.unit_list(i).type
case self.EMPLOYED
% 更新雇傭蜂
self.update_employed(i);
case self.UNEMPLOYED
% 更新非雇傭蜂
% 輪盤賭選擇目標(biāo)
goal_id = self.roulette();
self.update_unemployed(i,goal_id);
case self.SCOUT
% 更新偵查蜂
self.update_scout(i);
otherwise
continue
end
end
% 檢查開采次數(shù)
self.check_times();
update@Algorithm_Impl(self,iter)
end
% 設(shè)置雇傭蜂(當(dāng)雇傭蜂數(shù)量為0時(shí))
function set_employed(self)
% 計(jì)算種群中雇傭蜂數(shù)量
employed_num = 0;
for i = 1:self.size
if(self.unit_list(i).type == self.EMPLOYED)
employed_num = employed_num + 1;
end
end
if employed_num > 0
% 如果雇傭蜂數(shù)量不為0則直接跳出
return
end
% 求最大值則降序排列
[value,index] = sort([self.unit_list.value],'descend');
for i=1:self.size
if(i<self.employed_rate*self.size+1)
% 將排名靠前的部分蜜蜂設(shè)為雇傭蜂
self.unit_list(index(i)).type = self.EMPLOYED;
else
% 將其他蜜蜂設(shè)為非雇傭蜂
self.unit_list(index(i)).type = self.UNEMPLOYED;
end
end
end
% 更新雇傭蜂位置
function update_employed(self,id)
rand_id_list = randperm(self.size);
rand_id = rand_id_list(1);
if(rand_id == id)
rand_id = rand_id_list(2);
end
rnd = unifrnd(-1,1,1,self.dim);
% 計(jì)算新蜜源位置
new_pos = self.unit_list(id).position+rnd.*(self.unit_list(id).position-self.unit_list(rand_id).position);
% 越界檢查
new_pos = self.get_out_bound_value(new_pos);
new_value = self.cal_fitfunction(new_pos);
if (new_value > self.unit_list(id).value)
% 如果新位置更優(yōu)則更新
self.unit_list(id).position = new_pos;
self.unit_list(id).value = new_value;
self.unit_list(id).times = 0;
else
% 否則開采次數(shù)+1
self.unit_list(id).times = self.unit_list(id).times +1 ;
end
end
% 更新非雇傭蜂位置
function update_unemployed(self,id,goal_id)
% 隨機(jī)選擇一個(gè)雇傭蜂作為目標(biāo)
rand_id_list = randperm(self.size);
rand_id = rand_id_list(1);
if(rand_id == goal_id)
rand_id = rand_id_list(2);
end
new_pos = self.unit_list(goal_id).position+unifrnd(-1,1,1,self.dim).*(self.unit_list(goal_id).position-self.unit_list(rand_id).position);
new_pos = self.get_out_bound_value(new_pos);
new_value = self.cal_fitfunction(new_pos);
if(new_value> self.unit_list(goal_id).value)
% 如果新蜜源更優(yōu)則更新蜜源
self.unit_list(id).value = new_value;
self.unit_list(id).position = new_pos;
self.unit_list(id).type = self.EMPLOYED;
self.unit_list(id).times = 0;
% 原雇傭蜂變?yōu)榉枪蛡蚍? self.unit_list(goal_id).type = self.UNEMPLOYED;
self.unit_list(goal_id).times = 0;
elseif(new_value > self.unit_list(id).value)
% 如果新蜜源差于老蜜源嗅榕,但由于非雇傭蜂位置
self.unit_list(id).value = new_value;
self.unit_list(id).position = new_pos;
self.unit_list(goal_id).times = self.unit_list(goal_id).times + 1;
else
% 否則顺饮,蜜源開采次數(shù)+1
self.unit_list(goal_id).times = self.unit_list(goal_id).times + 1;
end
end
% 更新偵查蜂
function update_scout(self,id)
new_pos = unifrnd(self.range_min_list,self.range_max_list);
new_pos = self.get_out_bound_value(new_pos);
self.unit_list(id).position = new_pos;
self.unit_list(id).value = self.cal_fitfunction(new_pos);
self.unit_list(id).times = 0;
% 計(jì)算種群中雇傭蜂數(shù)量
employed_num = 0;
for i = 1:self.size
if(self.unit_list(i).type == self.EMPLOYED)
employed_num = employed_num + 1;
end
end
if (employed_num < self.size*self.employed_rate)
self.unit_list(id).type = self.EMPLOYED;
else
self.unit_list(id).type = self.UNEMPLOYED;
end
end
% 檢查蜜源開采次數(shù)吵聪,是否需要放棄,成為偵查蜂
function check_times(self)
for i = 1:self.size
if (self.unit_list(i).type == self.EMPLOYED)
if(self.unit_list(i).times > self.time_max)
% 將開采次數(shù)設(shè)為0
self.unit_list(i).times = 0;
% 轉(zhuǎn)化為偵查蜂
self.unit_list(i).type = self.SCOUT;
end
end
end
end
% 輪盤賭選擇蜜源
function goal_id = roulette(self)
% 獲取輪盤賭值
roulette_rate = self.get_roulette_rate();
% 群體輪盤賭值之和
roulette_sum = sum(roulette_rate);
goal_id = 1;
for i = 1:self.size
if(self.unit_list(i).type ~= self.EMPLOYED)
% 如果不是雇傭蜂則跳過(guò)
continue
end
% 當(dāng)前輪盤賭值
roulette_temp = roulette_rate(1);
rand = unifrnd(0, roulette_sum);
% 隨機(jī)數(shù)落在了哪個(gè)id的輪盤區(qū)間
for j = 1:self.size
if (j == self.size)
goal_id = self.size;
return
elseif(rand < roulette_temp)
goal_id = j;
return
end
roulette_temp = roulette_temp + roulette_rate(j);
end
end
end
% 計(jì)算輪盤賭概率
function rate_list = get_roulette_rate(self)
rate_list = zeros(1,self.size);
roulette_value_min = realmax('double');
% 計(jì)算出最小的適應(yīng)度值
for i = 1:self.size
if(self.unit_list(i).type ~= self.EMPLOYED)
% 如果不是雇傭蜂則跳過(guò)
continue
end
rate_list(i) = self.unit_list(i).value;
if (rate_list(i) < roulette_value_min)
roulette_value_min = rate_list(i);
end
end
% 計(jì)算出每個(gè)個(gè)體的輪盤賭數(shù)值
for i = 1:self.size
if(self.unit_list(i).type ~= self.EMPLOYED)
% 如果不是雇傭蜂則跳過(guò)
continue
end
rate_list(i) = rate_list(i) - roulette_value_min;
end
end
end
end
文件名:..\optimization algorithm\algorithm_artificial_bee_colony\ABC_Impl.m
算法實(shí)現(xiàn)兼雄,繼承于Base,圖方便也可不寫吟逝,直接用ABC_Base,這里為了命名一致赦肋。
%ABC實(shí)現(xiàn)
classdef ABC_Impl < ABC_Base
% 外部可調(diào)用的方法
methods
function self = ABC_Impl(dim,size,iter_max,range_min_list,range_max_list)
% 調(diào)用父類構(gòu)造函數(shù)設(shè)置參數(shù)
self@ABC_Base(dim,size,iter_max,range_min_list,range_max_list);
end
end
end
2.測(cè)試
測(cè)試F1
文件名:..\optimization algorithm\algorithm_artificial_bee_colony\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 = ABC_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)]);