# Termination Criterion¶

Whenever an algorithm is executed it needs to be decided whether a next iteration should be started or not. For single-objective algorithms a naive implementation can consider the relative improvement in the last $$n$$ generations.

## Number of Evaluations (‘n_eval’)¶

The termination can simply be reached by providing an upper bound for the number of function evaluations. Whenever, in an iteration the number of function evaluations is larger than this upper bound the algorithm terminates.

[1]:

from pymoo.algorithms.nsga2 import NSGA2
from pymoo.factory import get_problem, get_termination
from pymoo.optimize import minimize

problem = get_problem("zdt3")
algorithm = NSGA2(pop_size=100)
termination = get_termination("n_eval", 300)

res = minimize(problem,
algorithm,
termination,
pf=problem.pareto_front(),
seed=1,
verbose=True)

======================================================================
n_gen | n_eval  | igd          | gd           | hv
======================================================================
1     | 100     | 2.0831435338 | 3.1025851756 | 0.0000000000
2     | 200     | 2.0831435338 | 2.7631979206 | 0.0000000000
3     | 300     | 1.8379419062 | 2.6148863000 | 0.0000000000


## Number of Generations (‘n_gen’)¶

Moreover, the number of generations / iterations can be limited as well.

[2]:

from pymoo.algorithms.nsga2 import NSGA2
from pymoo.factory import get_problem, get_termination
from pymoo.optimize import minimize

problem = get_problem("zdt3")
algorithm = NSGA2(pop_size=100)
termination = get_termination("n_gen", 10)

res = minimize(problem,
algorithm,
termination,
pf=problem.pareto_front(),
seed=1,
verbose=True)

======================================================================
n_gen | n_eval  | igd          | gd           | hv
======================================================================
1     | 100     | 2.0831435338 | 3.1025851756 | 0.0000000000
2     | 200     | 2.0831435338 | 2.7631979206 | 0.0000000000
3     | 300     | 1.8379419062 | 2.6148863000 | 0.0000000000
4     | 400     | 1.8142589933 | 2.6613133627 | 0.0000000000
5     | 500     | 1.7680334810 | 2.4224261267 | 0.0000000000
6     | 600     | 1.4674130497 | 2.2908878184 | 0.0000000000
7     | 700     | 1.2980643774 | 2.1700689556 | 0.0000000000
8     | 800     | 1.2470033825 | 1.9702397019 | 0.0000000000
9     | 900     | 1.2344123230 | 1.7927009948 | 0.0000000000
10    | 1000    | 1.2342466357 | 1.5023082384 | 0.0000000000


## Design Space Tolerance (‘x_tol’)¶

Also, we can track the change in the design space. For a parameter explanation please have a look at ‘ftol’.

[3]:

from pymoo.algorithms.nsga2 import NSGA2
from pymoo.factory import get_problem, get_termination
from pymoo.optimize import minimize

problem = get_problem("zdt3")
algorithm = NSGA2(pop_size=100)
termination = get_termination("x_tol", tol=0.001, n_last=20, n_max_gen=None, nth_gen=10)

res = minimize(problem,
algorithm,
termination,
pf=problem.pareto_front(),
seed=1,
verbose=False)

print(res.algorithm.n_gen)


110


## Objective Space Tolerance (‘f_tol’)¶

Probably the most interesting stopping criterion is to use the objective space change to make decision whether to continue or not. Here, we mostly use a naive and efficient procedure to determine whether to stop or not. We aim to improve it further in the future. If somebody in interested in collaborating please let us know.

The parameters of our implementation are:

tol: What is the tolerance in the objective space in average. If the value is below this bound we terminate.

n_last: To make the criterion more robust, we consider the last $$n$$ generations and take the maximum. This considers the worst case in a window.

n_max_gen: As a fallback the generation number can be used. For some problems the termination criterion might not be reached, however, an upper bound for generations can be defined to stop in that case.

nth_gen: Defines whenever the termination criterion is calculated. By default, every 10th generation.

[4]:

from pymoo.algorithms.nsga2 import NSGA2
from pymoo.factory import get_problem, get_termination
from pymoo.optimize import minimize
from pymoo.visualization.scatter import Scatter

problem = get_problem("zdt3")
algorithm = NSGA2(pop_size=100)
termination = get_termination("f_tol", tol=0.001, n_last=20, n_max_gen=1000, nth_gen=10)

res = minimize(problem,
algorithm,
termination,
pf=problem.pareto_front(),
seed=1,
verbose=False)

print(res.algorithm.n_gen)
plot = Scatter(title="ZDT3")

110