注意:此代碼實(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)(四)測(cè)試粒子群算法中的測(cè)試函數(shù)、函數(shù)圖像的編寫毫目。
文件名 | 描述 |
---|---|
..\optimization algorithm\frame\Get_Functions_details.m | 測(cè)試函數(shù)蔬啡,求值用 |
..\optimization algorithm\frame\func_plot.m | 函數(shù)圖像,畫圖用 |
鯨魚算法的個(gè)體沒有獨(dú)有屬性镀虐。
鯨魚算法個(gè)體
文件名:.. \optimization algorithm\algorithm_whale\WOA_Unit.m
% 鯨魚算法個(gè)體
classdef WOA_Unit < Unit
properties
end
methods
function self = WOA_Unit()
end
end
end
鯨魚算法算法主體
文件名:..\optimization algorithm\algorithm_whale\WOA_Base.m
% 鯨魚算法
classdef WOA_Base < Algorithm_Impl
properties
% 算法名稱
name = 'WOA';
%
a;
%
b;
end
% 外部可調(diào)用的方法
methods
function self = WOA_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 ='WOA';
self.a = 2;
self.b = 1;
end
end
% 繼承重寫父類的方法
methods (Access = protected)
% 初始化種群
function init(self)
init@Algorithm_Impl(self)
%初始化種群
for i = 1:self.size
unit = WOA_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<0.5)
self.encircling_prey(i,iter);
else
self.bubble_net(i);
end
end
end
% 圍獵
function encircling_prey(self,id,iter)
A = unifrnd(-1,1,1,self.dim)*self.a*(1-iter/self.iter_max);
if (sqrt(sum(A.^2))<1)
new_pos = self.position_best - A.*abs(unifrnd(0,2,1,self.dim).*self.position_best-self.unit_list(id).position);
else
rand_id = unidrnd(self.size);
new_pos = self.unit_list(rand_id).position - abs(2*rand*A.*self.unit_list(rand_id).position-self.unit_list(id).position);
end
new_pos = self.get_out_bound_value(new_pos);
new_value = self.cal_fitfunction(new_pos);
if(new_value>self.unit_list(id).value)
self.unit_list(id).position = new_pos;
self.unit_list(id).value = new_value;
end
end
% 氣泡網(wǎng)
function bubble_net(self,id)
%RL
rl = unifrnd(-1,1,1,self.dim);
new_pos = self.position_best + abs(self.position_best - self.unit_list(id).position).*exp(self.b*rl).*cos(pi*2*rl);
new_pos = self.get_out_bound_value(new_pos);
new_value = self.cal_fitfunction(new_pos);
if(new_value>self.unit_list(id).value)
self.unit_list(id).position = new_pos;
self.unit_list(id).value = new_value;
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_whale\WOA_Impl.m
算法實(shí)現(xiàn)箱蟆,繼承于Base,圖方便也可不寫,直接用WOA_Base粉私,這里為了命名一致顽腾。
%鯨魚算法實(shí)現(xiàn)
classdef WOA_Impl < WOA_Base
% 外部可調(diào)用的方法
methods
function self = WOA_Impl(dim,size,iter_max,range_min_list,range_max_list)
% 調(diào)用父類構(gòu)造函數(shù)設(shè)置參數(shù)
self@WOA_Base(dim,size,iter_max,range_min_list,range_max_list);
end
end
end
2.測(cè)試
測(cè)試F1
文件名:..\optimization algorithm\algorithm_whale\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 = WOA_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)]);