注意:此代碼實現的是求目標函數最大值,求最小值可將適應度函數乘以-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_aquila\AO_Unit.m
% 天鷹算法個體
classdef AO_Unit < Unit
properties
end
methods
function self = AO_Unit()
end
end
end
天鷹算法算法主體
文件名:..\optimization algorithm\algorithm_aquila\AO_Base.m
% 天鷹算法
classdef AO_Base < Algorithm_Impl
properties
% 算法名稱
name = 'AO';
alpha = 0.1;
delta = 0.1;
omega = 0.005;
u = 0.0265;
r0 = 10;
end
% 外部可調用的方法
methods
function self = AO_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 ='AO';
end
end
% 繼承重寫父類的方法
methods (Access = protected)
% 初始化種群
function init(self)
init@Algorithm_Impl(self)
%初始化種群
for i = 1:self.size
unit = AO_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
end
% 每一代的更新
function update(self,iter)
update@Algorithm_Impl(self,iter)
if iter< self.iter_max*2/3
self.expanded(iter);
else
self.narrowed(iter);
end
end
% 階段1 擴張階段
function expanded(self,iter)
x = (self.r0+self.u*(1:self.dim)).*sin(-self.omega*(1:self.dim)+3*pi/2);
y = (self.r0+self.u*(1:self.dim)).*cos(-self.omega*(1:self.dim)+3*pi/2);
for i = 1:self.size
if rand < 0.5
pos_mean = mean(self.unit_list(i).position);
new_pos = self.position_best*(1-iter/self.iter_max) + rand*(pos_mean-self.position_best);
else
r_id = randperm(self.size,1);
new_pos = self.position_best.*Levy(self.dim)+self.unit_list(r_id).position+(y-x)*rand;
end
% 越界檢查,越界后再解空間隨機
new_pos = self.get_out_bound_value(new_pos);
new_value = self.cal_fitfunction(new_pos);
if new_value > self.unit_list(i).value
self.unit_list(i).position = new_pos;
self.unit_list(i).value = new_value;
end
end
end
% 階段2 收縮階段
function narrowed(self,iter)
QF=iter^((2*rand()-1)/(1-self.iter_max)^2);
G1 = 2*(1-iter/self.iter_max);
G2 = 2*rand-1;
for i = 1:self.size
if rand < 0.5
pos_mean = self.get_mean_pos();
new_pos = (self.position_best-pos_mean)*self.alpha - rand+self.delta*unifrnd(self.range_min_list,self.range_max_list);
else
new_pos = QF*self.position_best-(G2*self.unit_list(i).position*rand) - G1.*Levy(self.dim)+rand*G2;
end
% 越界檢查,越界后再解空間隨機
new_pos = self.get_out_bound_value(new_pos);
new_value = self.cal_fitfunction(new_pos);
if new_value > self.unit_list(i).value
self.unit_list(i).position = new_pos;
self.unit_list(i).value = new_value;
end
end
end
% 獲取種群平均位置
function pos_mean = get_mean_pos(self)
pos_mean = zeros(1,self.dim);
for i=1:self.size
pos_mean = pos_mean + self.unit_list(i).position;
end
pos_mean = pos_mean/self.size;
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 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_aquila\AO_Impl.m
算法實現,繼承于Base,圖方便也可不寫,直接用AO_Base,這里為了命名一致。
% 天鷹算法實現
classdef AO_Impl < AO_Base
% 外部可調用的方法
methods
function self = AO_Impl(dim,size,iter_max,range_min_list,range_max_list)
% 調用父類構造函數設置參數
self@AO_Base(dim,size,iter_max,range_min_list,range_max_list);
end
end
end
2.測試
測試F1
文件名:..\optimization algorithm\algorithm_aquila\Test.m
%% 清理之前的數據
% 清除所有數據
clear all;
close 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 = AO_Impl(dim,size,iter_max,range_min_list,range_max_list);
base.is_cal_max = false;
% 確定適應度函數
base.fitfunction = fobj;
% 運行
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);
% 繪制曲線,由于算法是求最大值,適應度函數為求最小值,故乘了-1,此時去掉-1
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)]);