Non-dominated Sorting Genetic Algorithm II (NSGA-II) is a multi-objective genetic algorithm, proposed by Deb et al., in 2002. It is an extension and improvement of NSGA, which is proposed earlier by Srinivas and Deb, in 1995.

In the structure of NSGA-II, in addition to genetic operators, crossover and mutation, two specialized multi-objective operators and mechanisms are defined and utilized:

**Non-dominated Sorting:**the population is sorted and partitioned into fronts (*F*_{1},*F*_{2}, etc.), where*F*_{1}(first front) indicates the approximated Pareto front.**Crowding Distance:**it is a mechanism of ranking among members of a front, which are dominating or dominated by each other.

These ranking mechanisms are used with genetic selection operators (usually Tournament Selection Operator), to create the population of next generation.

In this post, we are going to share with you, the MATLAB implementation of NSGA-II, as an open source project. The algorithm is implemented in a structured manner and if you are familiar with MATLAB programming language, you will find it easy, to use the codes in your research projects.

### Downloads

The download link of this project follows.

**Implementation of NSGA-II in MATLAB**

### Citing This Work

If you wish, you can cite this content as follows.

**Cite as:**

Dear yarpiz developers,

What version of MATLAB was employed to develop this code?

In MATLAB 7.5.0 (R2007b) a error appeared when running nsga2.m

??? Error: File: SortPopulation.m Line: 17 Column: 7

Expression or statement is incorrect–possibly unbalanced (, {, or [.

Error in ==> nsga2 at 76

[pop, F]=SortPopulation(pop);

Dear Corneliu,

These codes are developed using MATLAB 8.1 (or upper versions).

In line 17 of file “SortPopulation.m”, the code is as follows:

[~, CDSO]=sort([pop.CrowdingDistance],’descend’);

If you replace the ~ sign, with any unused valid variable name, such as “SCD” (for Sorted Crowding Distance), the code will run.

If you encounter same error again, replacement of ~ sign with variable name, will treat the error.

if objective function is f1(x)=x1 and f2(x)=(1+x2)/x1 for design variables 0.1 <x1<1 and 0<x2<5

when i code it in matlab as:-

z1=x(1) and z2=(1+x(2))/x(1) and

VarMin=[0.1 0] and VarMax=[1 5]

there is error in matlab

In an assignment A(I) = B, the number of elements in B and I must be the same.

Error in Mutate (line 24)

y(j)=x(j)+sigma*randn(size(j));

Error in nsga2 (line 108)

popm(k).Position=Mutate(p.Position,mu,sigma);

how to rectify it

Did you set the number of variables (nVar) correctly?

You should modify the line 22 as follows:

nVar = 2;

If you have two decision variables.

yes no of variables is set to 2 (nVar=2) and as per this VarSize=[1 2]

But still same error in Matlab when trying to run the programme

There was a minor bug in the implementation of Mutation function. The code is revised and now it is running. Please download the code once again, and let us know about the result.

Thanks for the quick rectification.

But still there are 2 things missing.

First there is no way to handle constraints.(i.e how to add inequality type constraints )

Second there is no file to handle output of the result(i.e viewing the results in a text file).

If this 2 files are included then it will be a complete code.

Hi

I am using your code to solve a 3 objective functions 8 variables optimization problem. the code might finished the calculation but error in Plotcosts(line18), so how to handle this?

thank you

The function PlotCosts is only needed to demonstrate the optimization results. You may modify the code, to work with 3 objectives or simply comment the line, which calls this function in main program file.

Dear all

what is the meaning of MOP4 function?

The MOP4 is codename for a benchmark multi-objective optimization problem, which is also known as

Kursawe function. For more information, you can refer to this link on Wikipedia:Test functions for optimization.It is a good algorithm suited for many applications.

Can this be used for constrained optimization? How to handle constraints?

Plz post improves nsga ii code for matlab…