# Termination Criteria¶

Different termination criteria can be used to determin how long an alorithm shoud run until the result is returned. In *pymoo* different ways to set the termination criteria are provided.

Commonly used criteria can be set by using tupels e.g.

`('n_gen', 5)`

An existing class inheriting from

`Termination`

can be passedA custom class can be created and used to determine the termination

In the following each of these possible termination criterium is shown in an example. Please note, that some of them are only applicable for test problems where the optimal solution(s) is/are known.

```
[1]:
```

```
# import the necessary classes and functions
from pymoo.optimize import minimize
from pymop.factory import get_problem
from pymoo.factory import get_algorithm
from pymoo.model.termination import MaximumGenerationTermination, Termination
# create a test problem for the purpose of demonstration
problem = get_problem("zdt1")
pf = problem.pareto_front()
```

## Number of Generations¶

First, the number of generations can be used to stop the executation of an algorithm. For any kind of evolutionary algorithm each iteration is called generation.

```
[2]:
```

```
res = minimize(problem,
method=get_algorithm('nsga2', pop_size=20),
termination=('n_gen', 5)
)
```

## Number of Function Evaluations¶

Also, the number of function evaluations can be used. Please note that for some algorithms slightly more evaluations will be executed.

```
[3]:
```

```
res = minimize(problem,
method=get_algorithm('nsga2', pop_size=20),
termination=('n_eval', 80)
)
```

## Performance Indicator¶

Furthermore, the performance indicator can be used to determine whether the algorithm should continue the run or not.

```
[4]:
```

```
from pymoo.factory import get_termination
res = minimize(problem,
method=get_algorithm('nsga2', pop_size=20),
termination=get_termination('igd', 0.1, pf = problem.pareto_front()),
pf=pf
)
```

## Other method to define the Termination Criterium¶

Moreover, instead of passing the convenience tuple which is loads internally a class implementation of the corresponding termination criterium, the class object can be passed directly.

```
[5]:
```

```
res = minimize(problem,
method=get_algorithm('nsga2', pop_size=20),
termination=MaximumGenerationTermination(25),
)
```

## Custom Termination Criterium¶

Therefore, instead of using a already written termination criterium, you can define a termination class by yourself and pass it to the algorithm. Here the class called `MyTermination`

is defined by inherting from the `Termination`

class.

```
[6]:
```

```
# the customized termination criterium
class MyTermination(Termination):
def __init__(self, perc_improvement) -> None:
super().__init__()
self.perc_improvement = perc_improvement
def _do_continue(self, algorithm):
import numpy as np
return np.random.random() > self.perc_improvement
res = minimize(problem,
method=get_algorithm('nsga2', pop_size=20),
termination=MyTermination(0.01),
)
```