在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
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏