NSGAII约束处理技术修改为静态罚函数法和Epsilon约束处理法

avatar 2024年08月07日17:49:04 0 728 views
博主分享免费Java教学视频,B站账号:Java刘哥 ,长期提供技术问题解决、项目定制:本站商品点此

在PlatEMO中,NSGAII采用的是CDP方法来处理约束(NDSort里)。

怎么实现罚函数法(简单,但不常用)和Epsilon约束处理方法(即CDP的放松版,常用)

直接贴代码

一、罚函数法

classdef NSGAII_static_penalty < ALGORITHM
% <multi> <real/integer/label/binary/permutation> <constrained/none>
% 使用静态惩罚函数法的NSGAII

    methods
        function main(Algorithm,Problem)
            %% Generate random population
            Population = Problem.Initialization();
            [~,FrontNo,CrowdDis] = EnvironmentalSelection(Population,Problem.N);

            %% Optimization
            while Algorithm.NotTerminated(Population)
                MatingPool = TournamentSelection(2,Problem.N,FrontNo,-CrowdDis);
                Offspring  = OperatorGA(Problem,Population(MatingPool));
                [Population,FrontNo,CrowdDis] = EnvironmentalSelection([Population,Offspring],Problem.N);
            end
        end
    end
end

function [Population,FrontNo,CrowdDis] = EnvironmentalSelection(Population,N)

    %% Non-dominated sorting
    % 修改 start
    % [FrontNo,MaxFNo] = NDSort(Population.objs,Population.cons,N);
    lambda  = 0.75;
    % Objs = Population.objs + lambda * Population.cons; 
    Objs = Population.objs + lambda * sum(max(0, Population.cons), 2);
    [FrontNo,MaxFNo] = NDSort(Objs, 0, N);
    % 修改 end

    Next = FrontNo < MaxFNo;
    
    %% Calculate the crowding distance of each solution
    CrowdDis = CrowdingDistance(Population.objs,FrontNo);
    
    %% Select the solutions in the last front based on their crowding distances
    Last     = find(FrontNo==MaxFNo);
    [~,Rank] = sort(CrowdDis(Last),'descend'); % 选稀疏的
    Next(Last(Rank(1:N-sum(Next)))) = true; % 差几个,选几个
    
    %% Population for next generation
    Population = Population(Next);
    FrontNo    = FrontNo(Next);
    CrowdDis   = CrowdDis(Next);
end

 

二、Epsilon约束处理法

classdef NSGAII_epsilon_cons < ALGORITHM
% <multi> <real/integer/label/binary/permutation> <constrained/none>
% 使用Epsilon约束的NSGAII

    methods
        function main(Algorithm,Problem)
            %% Generate random population
            Population = Problem.Initialization();

            % 修改 start
            % 
            cp = 2;
            Tc = 0.9 * ceil(Problem.maxFE/Problem.N); 
            epsilon_0 = max(Population.cons);
            epsilon_k = epsilon_0;
            % [~,FrontNo,CrowdDis] = EnvironmentalSelection(Population,Problem.N);
            [~,FrontNo,CrowdDis] = EnvironmentalSelection(Population,epsilon_0,Problem.N);
            % 修改 end
            
            
            %% Optimization
            while Algorithm.NotTerminated(Population)
               
                MatingPool = TournamentSelection(2,Problem.N,FrontNo,-CrowdDis);
                Offspring  = OperatorGA(Problem,Population(MatingPool));
                
                % 修改 start
                epsilon_k     = update_epsilon(ceil(Problem.FE/Problem.N), Tc, cp, epsilon_k);
                % [Population,FrontNo,CrowdDis] = EnvironmentalSelection([Population,Offspring],Problem.N);
                [Population,FrontNo,CrowdDis] = EnvironmentalSelection([Population,Offspring],epsilon_k,Problem.N);
                % 修改 end

                

            end
        end
    end
end

 %% Update the epsilon-level
function result = update_epsilon(T,Tc,cp,result_0)
    if T < Tc
        result = result_0 * (1 - T / Tc) ^ cp;
    else
        result = 0;
    end
end

function [Population,FrontNo,CrowdDis] = EnvironmentalSelection(Population,epsilon_k,N)

    %% Non-dominated sorting
    % [FrontNo,MaxFNo] = NDSort(Population.objs,Population.cons,N);  % Infeasible  = any(PopCon> 0,2);
    % 
    [FrontNo,MaxFNo] = NDSort(Population.objs,Population.cons - epsilon_k ,N);  

    Next = FrontNo < MaxFNo;
    
    %% Calculate the crowding distance of each solution
    CrowdDis = CrowdingDistance(Population.objs,FrontNo);
    
    %% Select the solutions in the last front based on their crowding distances
    Last     = find(FrontNo==MaxFNo);
    [~,Rank] = sort(CrowdDis(Last),'descend'); % 选稀疏的
    Next(Last(Rank(1:N-sum(Next)))) = true; % 差几个,选几个
    
    %% Population for next generation
    Population = Population(Next);
    FrontNo    = FrontNo(Next);
    CrowdDis   = CrowdDis(Next);
end
  • 微信
  • 交流学习,资料分享
  • weinxin
  • 个人淘宝
  • 店铺名:言曌博客咨询部

  • (部分商品未及时上架淘宝)
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0