pymoo
Latest Version: pymoo==0.4.0

Display Custom OutputΒΆ

An algorithm prints by default some information each generation. However, this might be desired to be modified. To do that a Display object can be passed to the algorithm which overrides the default behavior.

For instance:

[1]:
from pymoo.algorithms.nsga2 import NSGA2
from pymoo.factory import get_problem
from pymoo.optimize import minimize
from pymoo.util.display import Display
import numpy as np


class MyDisplay(Display):

    def _do(self, problem, evaluator, algorithm):
        super()._do(problem, evaluator, algorithm)
        self.output.append("metric_a", np.mean(algorithm.pop.get("X")))
        self.output.append("metric_b", np.mean(algorithm.pop.get("F")))


problem = get_problem("zdt2")

algorithm = NSGA2(pop_size=100, display=MyDisplay())

res = minimize(problem,
               algorithm,
               ('n_gen', 10),
               seed=1,
               verbose=True)

=============================================
n_gen |  n_eval |   metric_a   |   metric_b
=============================================
    1 |     100 |  0.500122773 |  2.958637098
    2 |     200 |  0.457137310 |  2.716284231
    3 |     300 |  0.424878151 |  2.540654646
    4 |     400 |  0.394032982 |  2.404824717
    5 |     500 |  0.370407264 |  2.295560288
    6 |     600 |  0.352432747 |  2.213395359
    7 |     700 |  0.328691523 |  2.099318577
    8 |     800 |  0.309089487 |  1.997100714
    9 |     900 |  0.291940743 |  1.918699384
   10 |    1000 |  0.272572521 |  1.832162449