NSGA - 2 Tutorial
NSGA - 2 Tutorial
ALGORITHMS (MOEA)
ARAVIND SESHADRI
ARAVIND SESHADRI
3.2. Non-Dominated sort. The initialized population is sorted based on nondomination 2. The fast sort algorithm [3] is described as below for each
for each individual p in main population P do the following
Initialize Sp = . This set would contain all the individuals that is
being dominated by p.
Initialize np = 0. This would be the number of individuals that dominate p.
for each individual q in P
if p dominated q then
add q to the set Sp i.e. Sp = Sp {q}
else if q dominates p then
increment the domination counter for p i.e. np = np + 1
if np = 0 i.e. no individuals dominate p then p belongs to the first
front; Set rank of individual p to one i.e prank = 1. Update the first
front set by adding p to front one i.e F1 = F1 {p}
This is carried out for all the individuals in main population P .
Initialize the front counter to one. i = 1
following is carried out while the ith front is nonempty i.e. Fi 6=
Q = . The set for storing the individuals for (i + 1)th front.
for each individual p in front Fi
for each individual q in Sp (Sp is the set of individuals dominated
by p)
nq = nq 1, decrement the domination count for individual
q.
if nq = 0 then none of the individuals in the subsequent
fronts would dominate q. Hence set qrank = i + 1. Update
the set Q with individual q i.e. Q = Q q.
Increment the front counter by one.
Now the set Q is the next front and hence Fi = Q.
This algorithm is better than the original NSGA ( [5]) since it utilize the information about the set that an individual dominate (Sp ) and number of individuals
that dominate the individual (np ).
3.3. Crowding Distance. Once the non-dominated sort is complete the crowding
distance is assigned. Since the individuals are selected based on rank and crowding
distance all the individuals in the population are assigned a crowding distance value.
Crowding distance is assigned front wise and comparing the crowding distance
between two individuals in different front is meaning less. The crowing distance is
calculated as below
For each front Fi , n is the number of individuals.
initialize the distance to be zero for all the individuals i.e. Fi (dj ) = 0,
where j corresponds to the j th individual in front Fi .
for each objective function m
Sort the individuals in front Fi based on objective m i.e. I =
sort(Fi , m).
2An individual is said to dominate another if the objective functions of it is no worse than the
other and at least in one of its objective functions it is better than the other
NSGA-II
ARAVIND SESHADRI
k =1 [2(1 rk )] m + 1 if rk 0.5
rk is an uniformly sampled random number between (0, 1) and m is mutation
distribution index.
3.6. Recombination and Selection. The offspring population is combined with
the current generation population and selection is performed to set the individuals
of the next generation. Since all the previous and current best individuals are
added in the population, elitism is ensured. Population is now sorted based on
non-domination. The new generation is filled by each front subsequently until the
population size exceeds the current population size. If by adding all the individuals
in front Fj the population exceeds N then individuals in front Fj are selected based
on their crowding distance in the descending order until the population size is N .
And hence the process repeats to generate the subsequent generations.
4. MOP1 (Example Problem - 1)
This problem is to find the global pareto front for a discontinuous function given
by objective function as in [3]
f1 (x) =1 e4x1 sin6 (6x1 )
f2 (x) =g(x)(1 (
f1 (x) 2
) )
g(x)
where,
6
X
xi 0.25
g(x) =1 + 9(
)
4
i=1
NSGA-II
0.8
f(x )
0.6
0.4
0.2
0
0.2
0.4
0.6
f(x )
0.8
Figure 1. MOP1
Population Generations Pool Size Tour Size c m
200
1000
100
2
20 20
Table 2. MOP2- Parameters for NSGA-II (Figures 2, 3,4)
12
X
(xi 0.5)2
i=1
12
X
i=3
(xi 0.5)2
ARAVIND SESHADRI
f(x )
1
0.8
0.6
0.4
0.2
0
0.5
1.5
0
2
f(x )
2
f(x )
1
f(x )
1
0.8
0.6
0.4
0.2
0
0
0.5
1.5
f(x )
2
0.5
1.5
f(x )
1
NSGA-II
f(x )
1.5
1
0.5
0
0
2
0.5
1.5
1
1
1.5
f(x )
0.5
2
f(x2)
ARAVIND SESHADRI
f(x )
1.5
0.5
0
2
1.5
2
1.5
0.5
0.5
0
f(x )
2
f(x )
1
Figure 5. MOP2: Second set of the solutions after 2000 generations (view 1)
MOP2 using NSGAII
1.6
1.4
1.2
f(x )
1
0.8
0.6
0.4
0.2
0
0
0.5
1.5
f(x )
0
2
0.5
1
f(x )
1.5
NSGA-II
f(x )
1
0.8
0.6
0.4
0.2
0
0
1
1.5
0.5
0
2
f(x )
f(x )
f(x )
1.5
1
0.5
0
0
0.5
1
2
1.5
1.5
f(x )
1
1
2
0.5
0
f(x2)
10
ARAVIND SESHADRI
f(x )
1.5
0.5
0
2
1.5
2
1.5
0.5
0.5
0
f(x )
2
f(x )
1
Figure 9. MOP2: Another set of the solutions after 1000 generations (view 1))
MOP2 using NSGAII
1.5
f(x )
0.5
1.5
0
0
1
0.5
0.5
1
1.5
f(x )
2
f(x1)
NSGA-II
11
f(x )
0.8
0.6
0.4
0.2
0
0
0.5
1
1.5
0.2
0.4
0.8
0.6
1.2
1.4
f(x2)
f(x )
1
1.5
f(x )
0.5
0
0
0.5
1.5
1
1
0.5
1.5
f(x )
1
f(x2)
12
ARAVIND SESHADRI
1.4
1.2
0.8
f(x )
0.6
0.4
0.2
0
0
1.5
0.5
0.5
0
1.5
f(x )
f(x )
2
NSGA-II
%
% This function initializes the population with N individuals and each
% individual having M decision variables based on the selected problem.
% M = 6 for problem MOP1 and M = 12 for problem MOP2. The objective space
% for MOP1 is 2 dimensional while for MOP2 is 3 dimensional.
function f = initialize_variables(N,problem)
% Both the MOPs given in Homework # 5 has 0 to 1 as its range for all the
% decision variables.
min = 0; max = 1; switch problem
case 1
M = 6;
K = 8;
case 2
M = 12;
K = 15;
end for i = 1 : N
% Initialize the decision variables
for j = 1 : M
f(i,j) = rand(1); % i.e f(i,j) = min + (max - min)*rand(1);
end
% Evaluate the objective function
f(i,M + 1: K) = evaluate_objective(f(i,:),problem);
end
}
8.2. Non-Dominated Sort and Crowding Distance Calculation.
%% Non-Donimation Sort
% This function sort the current popultion based on non-domination. All the
% individuals in the first front are given a rank of 1, the second front
% individuals are assigned rank 2 and so on. After assigning the rank the
% crowding in each front is calculated.
function f = non_domination_sort_mod(x,problem) [N,M] = size(x);
switch problem
case 1
M = 2;
V = 6;
case 2
M = 3;
V = 12;
end front = 1;
% There is nothing to this assignment, used only to manipulate easily in
% MATLAB.
F(front).f = []; individual = []; for i = 1 : N
% Number of individuals that dominate this individual
individual(i).n = 0;
% Individuals which this individual dominate
individual(i).p = [];
for j = 1 : N
dom_less = 0;
dom_equal = 0;
dom_more = 0;
for k = 1 : M
if (x(i,V + k) < x(j,V + k))
dom_less = dom_less + 1;
elseif (x(i,V + k) == x(j,V + k))
dom_equal = dom_equal + 1;
else
dom_more = dom_more + 1;
end
end
if dom_less == 0 & dom_equal ~= M
individual(i).n = individual(i).n + 1;
elseif dom_more == 0 & dom_equal ~= M
individual(i).p = [individual(i).p j];
end
end
if individual(i).n == 0
x(i,M + V + 1) = 1;
F(front).f = [F(front).f i];
13
14
ARAVIND SESHADRI
end
end
% Find the subsequent fronts
while ~isempty(F(front).f)
Q = [];
for i = 1 : length(F(front).f)
if ~isempty(individual(F(front).f(i)).p)
for j = 1 : length(individual(F(front).f(i)).p)
individual(individual(F(front).f(i)).p(j)).n = ...
individual(individual(F(front).f(i)).p(j)).n - 1;
if individual(individual(F(front).f(i)).p(j)).n == 0
x(individual(F(front).f(i)).p(j),M + V + 1) = ...
front + 1;
Q = [Q individual(F(front).f(i)).p(j)];
end
end
end
end
front = front + 1;
F(front).f = Q;
end [temp,index_of_fronts] = sort(x(:,M + V + 1)); for i = 1 :
length(index_of_fronts)
sorted_based_on_front(i,:) = x(index_of_fronts(i),:);
end current_index = 0;
% Find the crowding distance for each individual in each front
for front = 1 : (length(F) - 1)
objective = [];
distance = 0;
y = [];
previous_index = current_index + 1;
for i = 1 : length(F(front).f)
y(i,:) = sorted_based_on_front(current_index + i,:);
end
current_index = current_index + i;
% Sort each individual based on the objective
sorted_based_on_objective = [];
for i = 1 : M
[sorted_based_on_objective, index_of_objectives] = ...
sort(y(:,V + i));
sorted_based_on_objective = [];
for j = 1 : length(index_of_objectives)
sorted_based_on_objective(j,:) = y(index_of_objectives(j),:);
end
f_max = ...
sorted_based_on_objective(length(index_of_objectives), V + i);
f_min = sorted_based_on_objective(1, V + i);
y(index_of_objectives(length(index_of_objectives)),M + V + 1 + i)...
= Inf;
y(index_of_objectives(1),M + V + 1 + i) = Inf;
for j = 2 : length(index_of_objectives) - 1
next_obj = sorted_based_on_objective(j + 1,V + i);
previous_obj = sorted_based_on_objective(j - 1,V + i);
if (f_max - f_min == 0)
y(index_of_objectives(j),M + V + 1 + i) = Inf;
else
y(index_of_objectives(j),M + V + 1 + i) = ...
(next_obj - previous_obj)/(f_max - f_min);
end
end
end
distance = [];
distance(:,1) = zeros(length(F(front).f),1);
for i = 1 : M
distance(:,1) = distance(:,1) + y(:,M + V + 1 + i);
end
y(:,M + V + 2) = distance;
y = y(:,1 : M + V + 2);
z(previous_index:current_index,:) = y;
end
f = z();
}
NSGA-II
function selection_individuals(chromosome,pool_size,tour_size)
function selection_individuals(chromosome,pool_size,tour_size) is the
selection policy for selecting the individuals for the mating pool. The
selection is based on tournament selection. Argument chromosome is the
current generation population from which the individuals are selected to
form a mating pool of size pool_size after performing tournament
selection, with size of the tournament being tour_size. By varying the
tournament size the selection pressure can be adjusted.
15
16
ARAVIND SESHADRI
end
while isequal(parent_chromosome(parent_1,:),parent_chromosome(parent_2,:))
parent_2 = round(N*rand(1));
if parent_2 < 1
parent_2 = 1;
end
end
parent_1 = parent_chromosome(parent_1,:);
parent_2 = parent_chromosome(parent_2,:);
for j = 1 : V
%% SBX (Simulated Binary Crossover)
% Generate a random number
u(j) = rand(1);
if u(j) <= 0.5
bq(j) = (2*u(j))^(1/(mu+1));
else
bq(j) = (1/(2*(1 - u(j))))^(1/(mu+1));
end
child_1(j) = ...
0.5*(((1 + bq(j))*parent_1(j)) + (1 - bq(j))*parent_2(j));
child_2(j) = ...
0.5*(((1 - bq(j))*parent_1(j)) + (1 + bq(j))*parent_2(j));
if child_1(j) > u_limit
child_1(j) = u_limit;
elseif child_1(j) < l_limit
child_1(j) = l_limit;
end
if child_2(j) > u_limit
child_2(j) = u_limit;
elseif child_2(j) < l_limit
child_2(j) = l_limit;
end
end
child_1(:,V + 1: M + V) = evaluate_objective(child_1,pro);
child_2(:,V + 1: M + V) = evaluate_objective(child_2,pro);
was_crossover = 1;
was_mutation = 0;
else
parent_3 = round(N*rand(1));
if parent_3 < 1
parent_3 = 1;
end
% Make sure that the mutation does not result in variables out of
% the search space. For both the MOPs the range for decision space
% is [0,1]. In case different variables have different decision
% space each variable can be assigned a range.
child_3 = parent_chromosome(parent_3,:);
for j = 1 : V
r(j) = rand(1);
if r(j) < 0.5
delta(j) = (2*r(j))^(1/(mum+1)) - 1;
else
delta(j) = 1 - (2*(1 - r(j)))^(1/(mum+1));
end
child_3(j) = child_3(j) + delta(j);
if child_3(j) > u_limit
child_3(j) = u_limit;
elseif child_3(j) < l_limit
child_3(j) = l_limit;
end
end
child_3(:,V + 1: M + V) = evaluate_objective(child_3,pro);
was_mutation = 1;
was_crossover = 0;
end
if was_crossover
child(p,:) = child_1;
child(p+1,:) = child_2;
was_cossover = 0;
p = p + 2;
elseif was_mutation
child(p,:) = child_3(1,1 : M + V);
NSGA-II
was_mutation = 0;
p = p + 1;
end
end f = child;
}
8.5. Objective Function Evaluation.
function f = evaluate_objective(x,problem)
% Function to evaluate the objective functions for the given input vector
% x. x has the decision variables
switch problem
case 1
f = [];
%% Objective function one
f(1) = 1 - exp(-4*x(1))*(sin(6*pi*x(1)))^6;
sum = 0;
for i = 2 : 6
sum = sum + x(i)/4;
end
%% Intermediate function
g_x = 1 + 9*(sum)^(0.25);
%% Objective function one
f(2) = g_x*(1 - ((f(1))/(g_x))^2);
case 2
f = [];
%% Intermediate function
g_x = 0;
for i = 3 : 12
g_x = g_x + (x(i) - 0.5)^2;
end
%% Objective function one
f(1) = (1 + g_x)*cos(0.5*pi*x(1))*cos(0.5*pi*x(2));
%% Objective function two
f(2) = (1 + g_x)*cos(0.5*pi*x(1))*sin(0.5*pi*x(2));
%% Objective function three
f(3) = (1 + g_x)*sin(0.5*pi*x(1));
end
}
8.6. Selection Operator.
function f = replace_chromosome(intermediate_chromosome,pro,pop)
[N,V] = size(intermediate_chromosome); switch pro
case 1
M = 2;
V = 6;
case 2
M = 3;
V = 12;
end
% Get the index for the population sort based on the rank
[temp,index] = sort(intermediate_chromosome(:,M + V + 1));
% Now sort the individuals based on the index
for i = 1 : N
sorted_chromosome(i,:) = intermediate_chromosome(index(i),:);
end
% Find the maximum rank in the current population
max_rank = max(intermediate_chromosome(:,M + V + 1));
% Start adding each front based on rank and crowing distance until the
% whole population is filled.
previous_index = 0; for i = 1 : max_rank
current_index = max(find(sorted_chromosome(:,M + V + 1) == i));
if current_index > pop
remaining = pop - previous_index;
temp_pop = ...
sorted_chromosome(previous_index + 1 : current_index, :);
[temp_sort,temp_sort_index] = ...
sort(temp_pop(:, M + V + 2),descend);
17
18
ARAVIND SESHADRI
for j = 1 : remaining
f(previous_index + j,:) = temp_pop(temp_sort_index(j),:);
end
return;
elseif current_index < pop
f(previous_index + 1 : current_index, :) = ...
sorted_chromosome(previous_index + 1 : current_index, :);
else
f(previous_index + 1 : current_index, :) = ...
sorted_chromosome(previous_index + 1 : current_index, :);
return;
end
previous_index = current_index;
end
}
8.7. Main Function NSGA-II.
%% Main Script
% Main program to run the NSGA-II MOEA.
% initialize_variables has two arguments; First being the population size
% and the second the problem number. 1 corresponds to MOP1 and 2
% corresponds to MOP2.
%% Initialize the variables
% Declare the variables and initialize their values
% pop - population
% gen - generations
% pro - problem number
function nsga_2(); pop = 500; gen = 1000; pro = 2;
switch pro
case 1
M = 2;
V = 6;
case 2
M = 3;
V = 12;
end
chromosome = initialize_variables(pop,pro);
NSGA-II
offspring_chromosome = genetic_operator(parent_chromosome,pro,mu,mum);
%% Intermediate population
% Intermediate population is the combined population of parents and
% offsprings of the current generation. The population size is almost 1 and
% half times the initial population.
[main_pop,temp] = size(chromosome);
[offspring_pop,temp] = size(offspring_chromosome);
intermediate_chromosome(1:main_pop,:) = chromosome;
intermediate_chromosome(main_pop + 1 : main_pop + offspring_pop,1 : M+V) = ...
offspring_chromosome;
%% Non-domination-sort of intermediate population
% The intermediate population is sorted again based on non-domination sort
% before the replacement operator is performed on the intermediate
% population.
intermediate_chromosome = ...
non_domination_sort_mod(intermediate_chromosome,pro);
%% Perform Selection
% Once the intermediate population is sorted only the best solution is
% selected based on it rank and crowding distance. Each front is filled in
% ascending order until the addition of population size is reached. The
% last front is included in the population based on the individuals with
% least crowding distance
chromosome = replace_chromosome(intermediate_chromosome,pro,pop);
if ~mod(i,10)
fprintf(%d\n,i);
end
end save solution.txt chromosome -ASCII switch pro
case 1
plot(chromosome(:,V + 1),chromosome(:,V + 2),*);
title(MOP1 using NSGA-II);
xlabel(f(x_1));
ylabel(f(x_2));
case 2
plot3(chromosome(:,V + 1),chromosome(:,V + 2),chromosome(:,V + 3),*);
title(MOP2 using NSGA-II);
xlabel(f(x_1));
ylabel(f(x_2));
zlabel(f(x_3));
end
19
20
ARAVIND SESHADRI
References
1. Hans-Georg Beyer and Kalyanmoy Deb, On Self-Adaptive Features in Real-Parameter Evolutionary Algorithm, IEEE Trabsactions on Evolutionary Computation 5 (2001), no. 3, 250
270.
2. Kalyanmoy Deb and R. B. Agarwal, Simulated Binary Crossover for Continuous Search Space,
Complex Systems 9 (1995), 115 148.
3. Kalyanmoy Deb, Amrit Pratap, Sameer Agarwal, and T. Meyarivan, A Fast Elitist Multiobjective Genetic Algorithm: NSGA-II, IEEE Transactions on Evolutionary Computation 6 (2002),
no. 2, 182 197.
4. M. M. Raghuwanshi and O. G. Kakde, Survey on multiobjective evolutionary and real coded
genetic algorithms, Proceedings of the 8th Asia Pacific Symposium on Intelligent and Evolutionasy Systems, 2004, pp. 150 161.
5. N. Srinivas and Kalyanmoy Deb, Multiobjective Optimization Using Nondominated Sorting in
Genetic Algorithms, Evolutionary Computation 2 (1994), no. 3, 221 248.
6. Gary G. Yen and Haiming Lu, Dynamic multiobjective evolutionary algorithm: adaptive cellbased rank and density estimation, Evolutionary Computation, IEEE Transactions on 7 (2003),
no. 3, 253 274.
E-mail address: [email protected]