pymoo
Latest Version: pymoo==0.4.1

Display

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 minimize function (or also to the Algorithm constructor) to override 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)

res = minimize(problem,
               algorithm,
               ('n_gen', 10),
               seed=1,
               display=MyDisplay(),
               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

To just add some printouts of an algorithm you can inherit your own custom display MyDisplay from the algorithm’s default Display object. Whereas some algorithms have already a custom implementation for printing purposes, others just use the default single or multi-objective behavior. For instance, to keep the default output of NSGA2 and simply add the metrics shown above, MyDisplay has to inherit from MultiObjectiveDisplay and the calling the super()._do(problem, evaluator, algorithm) will add the default output.