久久天天躁狠狠躁夜夜躁,国产精品入口福利,97久久精品人人爽人人爽蜜臀 ,中文字幕国产精品一区二区

當前位置首頁 > 信息公告

優化算法matlab實現(十七)萬有引力算法matlab實現

更新時間:2023-10-17 文章作者:佚名 信息來源:網絡整理 閱讀次數:

注意:此代碼實現的是求目標函數最大值,求最小值可將適應度函數除以-1(框架代碼已實現)。QCC物理好資源網(原物理ok網)

注意:此代碼實現的是求目標函數最大值,求最小值可將適應度函數除以-1(框架代碼已實現)。QCC物理好資源網(原物理ok網)

注意:此代碼實現的是求目標函數最大值,求最小值可將適應度函數除以-1(框架代碼已實現)。QCC物理好資源網(原物理ok網)

1.代碼實現QCC物理好資源網(原物理ok網)

不了解萬有引力算法可以先瞧瞧優化算法筆記(十七)萬有引力算法QCC物理好資源網(原物理ok網)

實現代碼前須要先完成優化算法實現(二)框架編撰中的框架的編撰。QCC物理好資源網(原物理ok網)

文件名描述QCC物理好資源網(原物理ok網)

..\frameUnit.mQCC物理好資源網(原物理ok網)

個體QCC物理好資源網(原物理ok網)

..\frame.mQCC物理好資源網(原物理ok網)

算法主體QCC物理好資源網(原物理ok網)

以及優化算法實現(四)測試粒子群算法中的測試函數、函數圖象的編撰。QCC物理好資源網(原物理ok網)

文件名描述QCC物理好資源網(原物理ok網)

..\frames.mQCC物理好資源網(原物理ok網)

測試函數,求值用QCC物理好資源網(原物理ok網)

..\frame.mQCC物理好資源網(原物理ok網)

函數圖象萬有引力常數,作圖用QCC物理好資源網(原物理ok網)

萬有引力算法的個體有三個獨有屬性:速率、質量和加速度。QCC物理好資源網(原物理ok網)

萬有引力算法個體QCC物理好資源網(原物理ok網)

文件名:..\.mQCC物理好資源網(原物理ok網)

% 萬有引力算法個體
classdef GSA_Unit < Unit
    
    properties
        % 速度
        velocity;
        % 加速度
        acc;
        % 質量
        mass;
    end
    
    methods
        function self = GSA_Unit()
        end
    end
    
end
QCC物理好資源網(原物理ok網)

引力常數等于多少_引力常數的數值_萬有引力常數QCC物理好資源網(原物理ok網)

萬有引力算法算法主體QCC物理好資源網(原物理ok網)

文件名:..\.mQCC物理好資源網(原物理ok網)

% 萬有引力算法
classdef GSA_Base  < Algorithm_Impl
    
    properties
        % 算法名稱
        name = 'GSA';
        % 重力加速度
        G;
    end
    
    % 外部可調用的方法
    methods
        function self = GSA_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 ='GSA';
            % 初始加速度
            self.G = 100;
        end
    end
    
    % 繼承重寫父類的方法
    methods (Access = protected)
        % 初始化種群
        function init(self)
            init@Algorithm_Impl(self)
            %初始化種群
            for i = 1:self.size
                unit = GSA_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.mass = 0;
                unit.velocity = zeros(1,self.dim);
                unit.acc = zeros(1,self.dim);
                % 將個體加入群體數組
                self.unit_list = [self.unit_list,unit];
            end
        end
        
        % 每一代的更新
        function update(self,iter)
            update@Algorithm_Impl(self,iter)
            % 更新質量
            self.update_mass();
            % 更新加速度
            self.update_acc(iter);
            % 更新速度
            self.update_velocity();
            % 更新位置
            self.update_position();
        end     
       
        % 更新質量
        function update_mass(self)
            sum_mass = 0;
            [value,index] = sort([self.unit_list.value],'descend');
            best_value = value(1);
            worst_value = value(end);
            
            % 計算各個個體的質量
            for i = 1:self.size
                self.unit_list(i).mass = (self.unit_list(i).value-worst_value+realmin('double'))/(best_value-worst_value+realmin('double'));
                sum_mass = sum_mass + self.unit_list(i).mass;
            end
            
            % 將質量歸一化
            for i = 1:self.size
                self.unit_list(i).mass = self.unit_list(i).mass/sum_mass;
            end
        end
        
        % 更新加速度
        function update_acc(self,iter)
            cur_G = self.G*exp(-20*iter/self.iter_max);
            for i = 1:self.size
                new_acc = zeros(1,self.dim);
                for j = 1:self.size
                    if(i==j)
                        continue
                    end
                    % 計算兩個體距離
                    distance = sqrt(sum((self.unit_list(i).position-self.unit_list(j).position).^2));
                    % 根據距離,位置,質量計算加速度
                    new_acc = new_acc+self.unit_list(j).mass*unifrnd(0,1,1,self.dim).*(self.unit_list(j).position-self.unit_list(i).position)/(distance+realmin('double'));
                end
                new_acc = new_acc*cur_G;
                self.unit_list(i).acc = new_acc;
            end
        end
        
        % 更新速度
        function update_velocity(self)
             for i = 1:self.size
                 self.unit_list(i).velocity = unifrnd(0,1,1,self.dim).*self.unit_list(i).velocity + self.unit_list(i).acc;
             end
        end
        
        % 更新位置
        function update_position(self)
             for i = 1:self.size
                 new_pos = self.unit_list(i).velocity + self.unit_list(i).position;
                 new_pos = self.get_out_bound_value(new_pos);
                 new_value = self.cal_fitfunction(new_pos);
                 self.unit_list(i).position = new_pos;
                 self.unit_list(i).value = new_value;
             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
QCC物理好資源網(原物理ok網)

文件名:..\.mQCC物理好資源網(原物理ok網)

算法實現,承繼于Base,圖便捷也可不寫,直接用萬有引力常數,這兒為了命名一致。QCC物理好資源網(原物理ok網)

% 萬有引力算法實現
classdef GSA_Impl < GSA_Base
   
    % 外部可調用的方法
    methods
        function self = GSA_Impl(dim,size,iter_max,range_min_list,range_max_list)
            % 調用父類構造函數設置參數
             self@GSA_Base(dim,size,iter_max,range_min_list,range_max_list);
        end
    end 
end
QCC物理好資源網(原物理ok網)

2.測試QCC物理好資源網(原物理ok網)

測試F1QCC物理好資源網(原物理ok網)

文件名:..\Test.mQCC物理好資源網(原物理ok網)

%% 清理之前的數據
% 清除所有數據
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 = GSA_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)]);
QCC物理好資源網(原物理ok網)

發表評論

統計代碼放這里
主站蜘蛛池模板: 荥经县| 阿拉尔市| 得荣县| 梅州市| 石门县| 乃东县| 武清区| 玉屏| 大埔区| 永兴县| 婺源县| 偏关县| 宝兴县| 涞水县| 吉水县| 社会| 明光市| 成安县| 延津县| 丹东市| 固安县| 海安县| 井冈山市| 海安县| 牙克石市| 射阳县| 翁源县| 团风县| 桦川县| 东乡| 革吉县| 浑源县| 台北市| 周口市| 西贡区| 太谷县| 车险| 津市市| 滨州市| 郴州市| 福建省|