Search code examples
pythonoptimizationpymoo

How can i access the number of evaluations in my Pymoo result object?


When minimizing a Pymoo test problem i want to get the number of evaluations n_eval. Calling minimize with verbose option prints out this display information:

n_gen  |  n_eval  | n_nds  |      igd      |       gd     
==========================================================
     1 |      100 |      1 |  0.1852373892 |  0.1852373892
     2 |      200 |      1 |  0.0594274085 |  0.0594274085
     3 |      300 |      1 |  0.0148930530 |  0.0148930530
     4 |      400 |      1 |  0.0000231350 |  0.0000231350
     5 |      500 |      1 |  0.0000231350 |  0.0000231350

I expected to get access to a variable n_eval in the result object. I have not found any documentation on that online so far.

---UPDATE---

So, I found a solution which is too resource intensive imo as it requires saving the whole algorithm history, but it works for now.
I set the attribute saveHistory=True in the minimize method. Then I can read out the number of evaluations n_eval from the result object using n_gen and n_pop.

    n_gen = len(result.history)
    n_pop = result.algorithm.pop_size
    n_eval = n_gen * n_pop

See the documentation for more info: pymoo - Minimize

save_history: A boolean value representing whether a snapshot of the algorithm should be stored in each iteration. If enabled, the Result object contains the history.


Solution

  • In Pymoo, you can access the n_eval attribute of an optimization result object to get the number of function evaluations performed during the optimization process.

    Assuming you have already performed an optimization using Pymoo, and have stored the result in a variable result, you can access the n_eval attribute as follows:

    n_evaluations = result.algorithm.evaluator.n_eval