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

好好學習,天天向上,物理好資源網(原物理ok網)歡迎您!
當前位置:首頁 > > 信息公告 > 內容頁

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

2023-10-17 12:57:51信息公告47

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

文件名:..\.mJGZ物理好資源網(原物理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
JGZ物理好資源網(原物理ok網)

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

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

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

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

文件名:..\Test.mJGZ物理好資源網(原物理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)]);
JGZ物理好資源網(原物理ok網)

主站蜘蛛池模板: 宁陕县| 绥宁县| 土默特左旗| 榆林市| 景泰县| 洛宁县| 铁岭市| 库车县| 搜索| 宜良县| 台东县| 永定县| 凤城市| 四川省| 远安县| 通许县| 交城县| 四子王旗| 山阴县| 河东区| 翁源县| 阿拉善左旗| 兴仁县| 邳州市| 安塞县| 祁连县| 宁陵县| 玉树县| 武定县| 诸城市| 扎兰屯市| 金平| 洛浦县| 临湘市| 渝中区| 大宁县| 洛宁县| 鹤峰县| 鲁甸县| 红桥区| 双峰县|