優化算法matlab實現(十四)水波算法matlab實現

注意:此代碼實現的是求目標函數最大值,求最小值可將適應度函數乘以-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_water_wave\WWO_Unit.m

% 水波算法個體
classdef WWO_Unit < Unit
    properties
        % 水波高度
        h;
        % 波長
        lambda;
    end
    
    methods
        function self = WWO_Unit()
        end
    end 
end

水波算法算法主體
文件名:..\optimization algorithm\algorithm_water_wave\WWO_Base.m

% 水波算法
classdef WWO_Base  < Algorithm_Impl
    
    properties
        % 算法名稱
        name = 'WWO';
        % 波高
        h_max;
        % 波長
        lambda;
        % 波長衰減系數
        alpha;
        % 碎浪系數
        beta;
    end
    
    % 外部可調用的方法
    methods
        function self = WWO_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 ='WWO';
            self.h_max=12;
            self.lambda=0.5;
            self.alpha = 1.0026;
            self.beta = 0.25;
        end
    end
    
    % 繼承重寫父類的方法
    methods (Access = protected)
        % 初始化種群
        function init(self)
            init@Algorithm_Impl(self)
            %初始化種群
            for i = 1:self.size
                unit = WWO_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);
                % 設置初始波高
                unit.h = self.h_max;
                % 設置初始波長
                unit.lambda=self.lambda;
                % 將個體加入群體數組
                self.unit_list = [self.unit_list,unit];
            end
        end
        
        % 每一代的更新
        function update(self,iter)
            update@Algorithm_Impl(self,iter)
            
            for i = 1:self.size
                % 傳播:包含碎浪
                self.pass_wave(i,iter);
                if(self.unit_list(i).h<=0)
                    % 高度為0時折射
                    self.refraction(i);
                end
            end
            self.update_lambda();
        end
        
        % 傳播
        function pass_wave(self,id,iter)
            % 計算新位置
            new_pos = self.unit_list(id).position+self.unit_list(id).lambda*unifrnd(-1,1,1,self.dim).*(self.range_max_list-self.range_min_list);
            % 越界檢查
            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).value = new_value;
                self.unit_list(id).position = new_pos;
                self.unit_list(id).h = self.h_max;
                if(new_value>self.value_best)
                    % 如果優于當前最優解則進行一次碎浪
                    self.breaking_wave(id,iter);
                end
            else
                % 沒有找到更好的值,水波高度 -1
                self.unit_list(id).h = self.unit_list(id).h-1;
            end
        end
        
        % 折射
        function refraction(self,id)
            new_pos = zeros(1,self.dim);
            % 計算折射位置
            for d = 1:self.dim
                new_pos(d) = normrnd((self.position_best(d)+self.unit_list(id).position(d))/2,abs(self.position_best(d)-self.unit_list(id).position(d))/2);
            end
            % 越界檢查
            new_pos = self.get_out_bound_value(new_pos);
            new_value = self.cal_fitfunction(new_pos);
            if(new_value ~= 0)
                % 適應度函數值作分母,不為0時計算新波長
                new_lambda = self.unit_list(id).lambda*self.unit_list(id).value/new_value;
                self.unit_list(id).lambda = new_lambda;
            end
            self.unit_list(id).position = new_pos;
            self.unit_list(id).value = new_value;
        end
        
        % 碎浪
        function breaking_wave(self,id,iter)
            beta_iter = self.beta - iter/self.iter_max * (self.beta - 0.001);
            new_pos = self.position_best;
            for d = 1:self.dim
                if (rand<0.5)
                    % 隨機選取一半維度
                    new_pos(d) = new_pos(d)+normrnd(0,1)*beta_iter*(self.range_max_list(d)-self.range_min_list(d));
                end
            end
            % 越界檢查
            new_pos = self.get_out_bound_value(new_pos);
            new_value = self.cal_fitfunction(new_pos);
            self.unit_list(id).position = new_pos;
            self.unit_list(id).value = new_value;
        end
        
        % 更新波長
        function update_lambda(self)
            value_max = max([self.unit_list.value]);
            value_min = min([self.unit_list.value]);
            for i = 1:self.size
                b = (value_max-self.unit_list(i).value+realmin('double'))/(value_max-value_min+realmin('double'));
                lambda_new = self.unit_list(i).lambda*power(self.alpha,b);
                self.unit_list(i).lambda = lambda_new;
            end
        end

        % 獲取當前最優個體的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_water_wave\WWO_Impl.m
算法實現,繼承于Base,圖方便也可不寫,直接用WWO_Base,這里為了命名一致。

% 水波算法實現
classdef WWO_Impl < WWO_Base
   
    % 外部可調用的方法
    methods
        function self = WWO_Impl(dim,size,iter_max,range_min_list,range_max_list)
            % 調用父類構造函數設置參數
             self@WWO_Base(dim,size,iter_max,range_min_list,range_max_list);
        end
    end 
end

2.測試

測試F1
文件名:..\optimization algorithm\algorithm_water_wave\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 = WWO_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)]);
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容