NSGA-II in MATLAB
Implement NSGA-II in MATLAB with non-dominated sorting, crowding distance, genetic operators, and two-objective test problems.

On this page
The Non-dominated Sorting Genetic Algorithm II (NSGA-II) is a multi-objective genetic algorithm proposed by Deb, Pratap, Agarwal and Meyarivan in 2002 (paper (opens in a new tab)). It extends and improves NSGA, which Srinivas and Deb proposed in 1994 (paper (opens in a new tab)). NSGA-II is one of the most widely used multi-objective evolutionary algorithms.
The multi-objective problem
A multi-objective problem has several cost functions that must be minimized at the same time, usually within bounds on the decision variables :
The objectives usually conflict, so there is no single best solution. Solutions are compared with Pareto dominance. A solution dominates a solution when it is no worse in every objective and strictly better in at least one:
The solutions that no other solution dominates form the Pareto set, and their objective vectors form the Pareto front. The aim of NSGA-II is to find a good approximation of this front, with solutions that are close to it and spread well along it.
How NSGA-II works
In addition to the genetic operators (crossover and mutation), NSGA-II uses two specialized multi-objective mechanisms:
- Non-dominated sorting: the population is sorted and partitioned into fronts , , and so on. holds the members that no other member dominates. When is set aside, the non-dominated members of the rest form , and so on. , the first front, approximates the Pareto front.
- Crowding distance: a measure that ranks the members within one front. Members of the same front do not dominate each other, so dominance cannot tell them apart. The crowding distance measures how close a member’s neighbors are in objective space, and a member in a less crowded region is preferred.
For a front sorted by objective , the two boundary members get an infinite distance, and every other member gets
where and are the values of the two neighbors of member in that order, and and are the largest and smallest values of objective in the front.
In each generation, the algorithm:
- Creates offspring with crossover and mutation.
- Merges the parents and the offspring into one population.
- Sorts the merged population into fronts and computes the crowding distance in each front.
- Sorts the members by front rank first, and by crowding distance (largest first) within a front.
- Keeps the first members as the next population.
Parents remain eligible for the next generation alongside their offspring. This is elitist environmental selection, although a finite population may still discard some non-dominated solutions. In the original paper, the parents are chosen with a binary tournament that uses the same rank and crowding distance order.
Implementation in MATLAB
This post shares the MATLAB implementation of NSGA-II as an open-source project. The algorithm is implemented in a structured way, so if you know MATLAB, you can easily use the code in your research projects.
What is in the download
The NSGA-II folder holds these files:
nsga2.m: the main script, with the problem definition, the parameters and the main loop.main.m: a one line entry point that runsnsga2.m.NonDominatedSorting.m,CalcCrowdingDistance.mandSortPopulation.m: the three ranking steps described above.Dominates.m: the Pareto dominance test.Crossover.m: arithmetic crossover, which mixes two parents with random weights.Mutate.m: Gaussian mutation of a few randomly chosen variables.MOP2.mandMOP4.m: two test problems with two objectives. The default isMOP4, the Kursawe function.PlotCosts.m: plots the members of .
To run the code, open the NSGA-II folder in MATLAB and run main.m. In each iteration the program prints the number of members of and plots their objective values.
In this code, the parents for crossover and mutation are picked at random from the current population. The selection pressure comes from the sorting and truncation step at the end of each iteration.
The code uses unifrnd and randsample, two functions of the Statistics and Machine Learning Toolbox, so it probably needs that toolbox.
Parameters in the code
The parameters are set at the top of nsga2.m:
| Parameter | Meaning | Value |
|---|---|---|
nVar |
Number of decision variables | 3 |
VarMin, VarMax |
Lower and upper bounds of the variables | , |
MaxIt |
Maximum number of iterations | 100 |
nPop |
Population size | 50 |
pCrossover |
Share of the population used to create offspring by crossover | 0.7 |
pMutation |
Share of the population used to create mutants | 0.4 |
mu |
Mutation rate (share of the variables that change) | 0.02 |
sigma |
Mutation step size |
To solve your own problem, write a cost function that takes a row vector and returns a column vector of objective values, and set CostFunction, nVar, VarMin and VarMax in nsga2.m.
Check the result
The first front is non-dominated within the evaluated population, not a proof of the true Pareto front. Inspect objective values, decision-variable bounds, and the spread of solutions across repeated runs. A larger front is not automatically a better approximation. When adapting the crowding-distance calculation, handle an objective whose range is zero so that the normalization does not divide by zero.
Applications
NSGA-II needs only the values of the objectives, not their gradients, so it suits problems where the trade-off between goals matters, for example cost against performance in engineering design, or several conflicting criteria in scheduling and planning.
Related posts
- NSGA-III in MATLAB extends this code to problems with many objectives.
- Multi-Objective PSO in MATLAB, SPEA2 in MATLAB, PESA-II in MATLAB and MOEA/D in MATLAB are other multi-objective algorithms on Yarpiz.
Downloads
How to cite
If you use this code or tutorial in your work, articles or publications, please cite it.
Mostapha Kalami Heris, NSGA-II in MATLAB (YPEA120), Yarpiz, 2015. https://yarpiz.com/nsga2-ypea120@misc{yarpiz-ypea120,
author = {{Mostapha Kalami Heris}},
title = {{NSGA-II in MATLAB}},
howpublished = {Yarpiz},
year = {2015},
note = {Code YPEA120},
url = {https://yarpiz.com/nsga2-ypea120}
}Published Updated
Comments