優(yōu)化算法matlab實(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))。
注意:此代碼實(shí)現(xiàn)的是求目標(biāo)函數(shù)最大值,求最小值可將適應(yīng)度函數(shù)乘以-1(框架代碼已實(shí)現(xiàn))。

1. 代碼實(shí)現(xiàn)

實(shí)現(xiàn)代碼前需要先完成上一篇優(yōu)化算法matlab實(shí)現(xiàn)(二)框架編寫(xiě)中的框架的編寫(xiě)。

文件名 描述
:..\optimization algorithm\frame\Unit.m 個(gè)體
:..\optimization algorithm\frame\Algorithm_Impl.m 算法主體

粒子群算法的每個(gè)粒子有兩個(gè)獨(dú)特的屬性,速度和歷史最優(yōu)位置,在繼承后需要單獨(dú)添加。
如果不了解粒子群算法可以先看看優(yōu)化算法筆記(三)粒子群算法(1)
粒子群算法個(gè)體
文件名:..\optimization algorithm\algorithm_particle_swarm\PSO_Unit.m

classdef PSO_Unit < Unit
    properties
        % 個(gè)體的速度
        velocity
        % 個(gè)體的歷史最優(yōu)
        position_best
    end
    
    methods
        function obj = PSO_Unit()
        end
    end
end

粒子群算法主體
文件名:..\optimization algorithm\algorithm_particle_swarm\PSO_Base.m

% 粒子群算法
classdef PSO_Base < Algorithm_Impl
    %% 算法屬性及參數(shù)
    properties
        % 算法名稱(chēng):
        name = 'PSO';
        % 最大速度限制
        velocity_max_list;
        % 自我學(xué)習(xí)系數(shù)
        C1 = 2;
        % 全局學(xué)習(xí)系數(shù)
        C2 = 2;
        % 慣性系數(shù)
        W = 1;
    end
    
    %% 外部可調(diào)用的方法
    methods
        % 構(gòu)造函數(shù)
        function self = PSO_Base(dim,size,iter_max,range_min_list,range_max_list)
            self@Algorithm_Impl(dim,size,iter_max,range_min_list,range_max_list);
            % 初始化速度上限(為取值范圍長(zhǎng)度的1/10)
            self.velocity_max_list = (self.range_max_list - self.range_min_list)*0.1;
        end
    end 
    
    %% 繼承重寫(xiě)父類(lèi)的方法
    methods (Access = protected)
        % 初始化種群
        function init(self)
            init@Algorithm_Impl(self)
            % 初始化種群中的每個(gè)個(gè)體
            for i = 1:self.size 
                unit = PSO_Unit();
                % 隨機(jī)初始化位置:rand(0,1).*(max-min)+min
                unit.position = unifrnd(self.range_min_list,self.range_max_list);
                % 初始化時(shí),歷史最優(yōu)就是當(dāng)前位置
                unit.position_best = unit.position;
                % 隨機(jī)初始化速度:rand(0,1).*(max-(-max))+(-max)
                unit.velocity = unifrnd(-self.velocity_max_list , self.velocity_max_list);
                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
                % 更新該個(gè)體的速度
                self.update_velocity(i)
                % 更新該個(gè)體的位置
                self.update_position(i)
            end
        end
        
        % 更新個(gè)體的速度
        function update_velocity(self,id)
            % 獲取當(dāng)前個(gè)體實(shí)例
            unit = self.unit_list(id);
            % 計(jì)算新的速度
            % 公式: v_new = W*v_old+C1*rand(0,1).*(p_best-unit_pos)+C2*rand(0,1).*(g_best-unit_pos)
            velocity_new = self.W*unit.velocity+self.C1*rand(1,self.dim).*(unit.position_best-unit.position)+self.C2*rand(1,self.dim).*(self.position_best-unit.position);
            velocity_new = self.get_out_bound_value(velocity_new,-self.velocity_max_list,self.velocity_max_list);
            % 修改該個(gè)體速度
            unit.velocity = velocity_new;
            % 保存修改值
            self.unit_list(id) = unit;
        end
        
        % 更新個(gè)體的位置
        function update_position(self,id)
            unit = self.unit_list(id);
            % 計(jì)算出新的位置
            position_new = unit.position + unit.velocity;
            % 個(gè)體移動(dòng)到該位置
            unit.position = position_new;
            % 計(jì)算該位置的適應(yīng)度值
            value = self.cal_fitfunction(unit.position);
            if (value > unit.value)
                % 只記錄歷史最優(yōu)值
                unit.value = value;
                % 由于歷史最優(yōu)值時(shí)更新歷史最優(yōu)位置
                unit.position_best = unit.position;
            end
            % 保存修改值
            self.unit_list(id) = unit;
        end
       %%
    end
end

文件名:..\optimization algorithm\algorithm_particle_swarm\PSO_Impl.m
算法實(shí)現(xiàn),繼承于Base,圖方便也可不寫(xiě),直接用PSO_Base,這里為了命名一致。

%PSO實(shí)現(xiàn)
classdef PSO_Impl < PSO_Base

    % 外部可調(diào)用的方法
    methods
        function self = PSO_Impl(dim,size,iter_max,range_min_list,range_max_list)
            % 調(diào)用父類(lèi)構(gòu)造函數(shù)設(shè)置參數(shù)
             self@PSO_Base(dim,size,iter_max,range_min_list,range_max_list);
        end
    end 
end

2. 測(cè)試

寫(xiě)一個(gè)簡(jiǎn)單的測(cè)試函數(shù)試一試代碼能否正常運(yùn)行。
文件名:..\optimization algorithm\algorithm_particle_swarm\Test_Function.m
測(cè)試函數(shù)求(x-10)的平方和的最小值

function value = Test_Function( x )
    value=sum((x-10).^2);
end

測(cè)試代碼
文件名:..\optimization algorithm\algorithm_particle_swarm\Test.m

%% 清理之前的數(shù)據(jù)
% 清除所有數(shù)據(jù)
clear all;
% 清除窗口輸出
clc;

%% 添加框架路徑
% 將上級(jí)目錄中的frame文件夾加入路徑
addpath('../frame')

%% 算法實(shí)例
% 算法維度
dim = 2;
% 種群數(shù)量
size = 20;
% 最大迭代次數(shù)
iter_max = 50;
% 取值范圍上界
range_max_list = ones(1,dim)*100;
% 取值范圍下界
range_min_list = -ones(1,dim)*100;

% 實(shí)例化粒子群類(lèi)
base = PSO_Impl(dim,size,iter_max,range_min_list,range_max_list);
%告訴算法目標(biāo)函數(shù)求的是最小值,不是求最大值
base.is_cal_max = false;
% 確定適應(yīng)度函數(shù)
base.fitfunction = @Test_Function;
% 運(yùn)行
base.run();

% 添加負(fù)號(hào)是因?yàn)槟繕?biāo)函數(shù)是求最小值
disp(['最優(yōu)值',num2str(-base.value_best)])
disp(['最優(yōu)解',num2str(base.position_best)])

% 畫(huà)一個(gè)簡(jiǎn)單的圖像
plot(base.value_best_history);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容