Home \ Multiobjective Optimization \ NSGA-II in MATLAB


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 (F1, F2, etc.), where F1 (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.


The download link of this project follows.

Implementation of NSGA-II in MATLAB



  1. 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.

  2. 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)
    Error in nsga2 (line 108)

    how to rectify it

  3. 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.

  4. 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.

  5. Dear all
    what is the meaning of MOP4 function?

Leave a Reply

Your email address will not be published. Required fields are marked *



Check Also


Pareto Envelope-based Selection Algorithm II (PESA-II) is a multi-objective evolutionary optimization algorithm, which uses the ...