注意:此代碼實現的是求目標函數最大值,求最小值可將適應度函數乘以-1(框架代碼已實現)。
注意:此代碼實現的是求目標函數最大值,求最小值可將適應度函數乘以-1(框架代碼已實現)。
注意:此代碼實現的是求目標函數最大值,求最小值可將適應度函數乘以-1(框架代碼已實現)。
1. 代碼實現
不了解差分進化算法可以先看看優(yōu)化算法筆記(七)差分進化算法
實現代碼前需要先完成優(yōu)化算法matlab實現(二)框架編寫中的框架的編寫。
| 文件名 | 描述 |
|---|---|
| ..\optimization algorithm\frame\Unit.m | 個體 |
| ..\optimization algorithm\frame\Algorithm_Impl.m | 算法主體 |
以及優(yōu)化算法matlab實現(四)測試粒子群算法中的測試函數、函數圖像的編寫。
| 文件名 | 描述 |
|---|---|
| ..\optimization algorithm\frame\Get_Functions_details.m | 測試函數,求值用 |
| ..\optimization algorithm\frame\func_plot.m | 函數圖像,畫圖用 |
差分進化算法個體
文件名:.. \optimization algorithm\algorithm_differential_evolution\DE_Unit.m
% 差分進化算法個體
classdef DE_Unit < Unit
properties
% 個體的新位置(變異位置,交叉選擇用)
position_new
end
methods
function self = DE_Unit()
end
end
end
差分進化算法主體
文件名:..\optimization algorithm\ algorithm_differential_evolution\DE_Base.m
% 差分進化算法
classdef DE_Base < Algorithm_Impl
properties
% 算法名稱
name = 'DE';
% 交叉率
cross_rate;
% 變異率
alter_factor;
end
% 外部可調用的方法
methods
function self = DE_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.cross_rate = 0.3;
self.alter_factor = 0.5;
end
end
% 繼承重寫父類的方法
methods (Access = protected)
% 初始化種群
function init(self)
init@Algorithm_Impl(self)
%初始化種群
for i = 1:self.size
unit = DE_Unit();
% 隨機初始化位置:rand(0,1).*(max-min)+min
unit.position = unifrnd(self.range_min_list,self.range_max_list);
unit.position_new = unit.position;
% 計算適應度值
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)
% 變異
self.altered();
% 交叉
self.cross();
% 選擇
self.choose();
end
% 變異
function altered(self)
% 遍歷所有個體
for i = 1:self.size
% 在群體中隨機選擇3個個體
% 1.先將群體的id亂序
% 2.從亂序中選擇前3個id
rand_id_list = randperm(self.size);
r1 = rand_id_list(1);
r2 = rand_id_list(2);
r3 = rand_id_list(3);
if (r1 == i)
r1 =rand_id_list(4);
end
if (r2 == i)
r2 =rand_id_list(4);
end
if (r3 == i)
r3 =rand_id_list(4);
end
% 變異得到新位置
% new_pos = rand_1_pos+f*(rand_2_pos - rand_3_pos)
new_pos = self.unit_list(r1).position + self.alter_factor*(self.unit_list(r2).position - self.unit_list(r3).position);
new_pos = self.get_out_bound_value(new_pos);
% 保存該變異位置
self.unit_list(i).position_new = new_pos;
end
end
% 交叉
function cross(self)
% 遍歷步長為2,相鄰兩個個體交叉
for i = 1:self.size
% 隨機選擇一維
cur_dim = unidrnd(self.dim);
for d = 1:self.dim
% 滿足交叉條件則將原對應個體對應維度保留
r = unifrnd (0,1);
if(r<self.cross_rate || d == cur_dim)
% 保持不變
else
self.unit_list(i).position_new(d) = self.unit_list(i).position(d);
end
end
end
end
% 選擇個體,貪心算法保留對應個體
function choose(self)
for i = 1:self.size
new_value = self.cal_fitfunction(self.unit_list(i).position_new);
if (new_value > self.unit_list(i).value)
self.unit_list(i).value = new_value;
self.unit_list(i).position = self.unit_list(i).position_new;
end
end
end
end
end
文件名:..\optimization algorithm\algorithm_differential_evolution\DE_Impl.m
算法實現,繼承于Base,圖方便也可不寫,直接用DE_Base,這里為了命名一致。
% DE實現
classdef DE_Impl < DE_Base
properties
end
% 外部可調用的方法
methods
function self = DE_Impl(dim,size,iter_max,range_min_list,range_max_list)
% 調用父類構造函數設置參數
self@DE_Base(dim,size,iter_max,range_min_list,range_max_list);
end
end
end
2. 測試
測試F1
文件名:..\optimization algorithm\algorithm_differential_evolution \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 = DE_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)]);
