# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + raw_mimetype="text/restructuredtext" active=""
# .. _nb_nsga2:
# -

# ## NSGA-II

# The algorithm is implemented based on <cite data-cite="nsga2"></cite> [\[benchmark\]](https://www.egr.msu.edu/coinlab/blankjul/pymoo-benchmark/nsga2.html) [\[data\]](https://www.egr.msu.edu/coinlab/blankjul/pymoo-benchmark/nsga2.zip)
# . A benchmark of the algorithm against the original C code can be found 
# The algorithm follows the general
# outline of a genetic algorithm with a modifed mating and survival selection. In NSGA-II, first, individuals
# are selected frontwise. By doing so, there will be the situation where a front needs to be splitted because not all indidividuals are allowed to survive. In this splitting front, solutions are selected based on crowding distance. 

# <div style="display: block;margin-left: auto;margin-right: auto;width: 80%;">
# ![nsga2_survival](../resources/images/nsga2_survival.png)
# </div>

#
# The crowding distance is basically the Manhatten Distance in the objective space. However, the extreme points are desired to be kept every generation and, therefore, get assigned a crowding distance of infinity.

# <div style="display: block;margin-left: auto;margin-right: auto;width: 50%;">
# ![nsga2_crowding](../resources/images/nsga2_crowding.png)
# </div>

# Furthermore, to increase some selection preasure NSGA-II uses a binary tournament mating selection. Each individual is first compared by rank and then by crowding distance. There also exists a variant in the original C code where instead of using the rank the domination criterium between two solutions is used.

# ### Example

# +
from pymoo.optimize import minimize
from pymoo.algorithms.nsga2 import nsga2
from pymoo.util import plotting
from pymop.factory import get_problem

# create the algorithm object
method = nsga2(pop_size=100, elimate_duplicates=True)

# execute the optimization
res = minimize(get_problem("zdt1"),
               method,
               termination=('n_gen', 200))

plotting.plot(res.F, no_fill=True)

# -

# ### API

# + raw_mimetype="text/restructuredtext" active=""
# .. autofunction:: pymoo.algorithms.nsga2.nsga2
#     :noindex:

StarcoderdataJupyterScriptsDedupFiltered