nsga2中的非支配排序和拥挤度计算的matlab代码示例
时间: 2023-11-28 20:47:08 浏览: 193
以下是NSGA-II算法中非支配排序和拥挤度计算的Matlab代码示例:
```
function [FrontNo,CrowdDis] = NDSortAndCrowdingDistanceAssignment(FitnessValue,MaxF,MinF)
%NDSortAndCrowdingDistanceAssignment - Non-dominated sorting and crowding distance assignment
%NDSortAndCrowdingDistanceAssignment is to rank the whole population according to the
%non-domination levels of each individual and calculate the crowding distance of each individual.
%
%Syntax: [FrontNo,CrowdDis] = NDSortAndCrowdingDistanceAssignment(FitnessValue,MaxF,MinF)
%
%Input:
%FitnessValue - Objective function value of each individual.
%MaxF - Maximum of each element in all objective function vectors.
%MinF - Minimum of each element in all objective function vectors.
%
%Output:
%FrontNo - The non-domination level, which a solution belongs to.
%CrowdDis - Crowding distance of each individual.
%
%Example:
% [FrontNo,CrowdDis] = NDSortAndCrowdingDistanceAssignment(FitnessValue,MaxF,MinF);
%
%Reference:
%Kalyanmoy Deb, Amrit Pratap, Sameer Agarwal, and T. Meyarivan. A Fast and Elitist
%Multiobjective Genetic Algorithm: NSGA-II. IEEE Transactions on Evolutionary Computation,
%6(2):182-197, 2002.
%
%Author:Zhang Xin (zhxinbox@gmail.com)
%Blog:http://blog.sina.com.cn/zhangxin09
%Department of Computer Science and Technology, Tsinghua University
% Get the size of FitnessValue.
[PopObjDim,PopSize] = size(FitnessValue);
% Calculate the normalized objective function value.
NormFit = (FitnessValue - repmat(MinF,PopObjDim,1)) ./ repmat(MaxF-MinF,PopObjDim,1);
% Non-dominated sorting.
MaxFront = 0;
Individual = cell(PopSize,1);
for i = 1 : PopSize
Individual{i}.FitnessValue = FitnessValue(:,i);
Individual{i}.DominateSet = [];
Individual{i}.DominatedCount = 0;
for j = 1 : PopSize
Flag = domi(NormFit(:,i),NormFit(:,j));
if Flag == 1
Individual{i}.DominateSet = [Individual{i}.DominateSet j];
elseif Flag == -1
Individual{i}.DominatedCount = Individual{i}.DominatedCount + 1;
end
end
if Individual{i}.DominatedCount == 0
Individual{i}.Rank = 1;
MaxFront = MaxFront + 1;
Front{1}(end+1) = i;
end
end
curRank = 1;
while ~isempty(Front{curRank})
temp = [];
for i = 1 : length(Front{curRank})
for j = 1 : length(Individual{Front{curRank}(i)}.DominateSet)
Individual{Individual{Front{curRank}(i)}.DominateSet(j)}.DominatedCount = ...
Individual{Individual{Front{curRank}(i)}.DominateSet(j)}.DominatedCount - 1;
if Individual{Individual{Front{curRank}(i)}.DominateSet(j)}.DominatedCount == 0
Individual{Individual{Front{curRank}(i)}.DominateSet(j)}.Rank = curRank + 1;
temp(end+1) = Individual{Front{curRank}(i)}.DominateSet(j);
end
end
end
curRank = curRank + 1;
Front{curRank} = temp;
end
FrontNo = zeros(1,PopSize);
for i = 1 : MaxFront
FrontNo(Front{i}) = i;
end
% Calculate the crowding distance.
CrowdDis = zeros(1,PopSize);
for i = 1 : length(Front)
FrontSize = length(Front{i});
if FrontSize == 0
break;
end
Fitness = FitnessValue(:,Front{i});
NormFitValue = NormFit(:,Front{i});
for j = 1 : PopObjDim
[Temp,Order] = sort(NormFitValue(j,:));
Fitness(j,:) = Fitness(j,Order);
NormFitValue(j,:) = NormFitValue(j,Order);
CrowdDis(Front{i}(1)) = inf;
CrowdDis(Front{i}(end)) = inf;
for k = 2 : FrontSize-1
CrowdDis(Front{i}(k)) = CrowdDis(Front{i}(k)) + ...
(NormFitValue(j,k+1) - NormFitValue(j,k-1)) / (NormFitValue(j,end)-NormFitValue(j,1));
end
end
end
```
其中,`FitnessValue`是种群中每个个体的目标函数值,`MaxF`和`MinF`是所有个体目标函数值中各个元素的最大值和最小值。函数返回`FrontNo`和`CrowdDis`,分别表示每个个体所在的非支配等级和拥挤度。
阅读全文