Project-Platypus / Platypus

A Free and Open Source Python Library for Multiobjective Optimization
GNU General Public License v3.0
563 stars 153 forks source link

How to print out the non dominant solution after each iteration while using Genetic Algorithm or NSGA II? #126

Closed ghost closed 1 year ago

ghost commented 4 years ago

I am interested in monitoring the performance of the optimization algorithm. Hence, after every few iterations, I would like to print out the nondominant solutions from the population. How can that be achieved if I am using Genetic Algorithms or NSGA II?

Any help is much appreciated!

dhadka commented 4 years ago

You should be able to get the population from the algorithm, which is a list of solutions, and print them out. Similar to how you would read the result.

ghost commented 4 years ago

Yes I know how to do that. However, after algorithm.run(10,000) for example, I would like to get the non-dominant solution after every 10th iteration. At the end I would like to plot the list of solutions out on a graph. The x-axis should reflect 1 non dominant solution after every 10th iteration, while the y-axis should reflect the fitness of the non dominant solutions. Therefore, I would like to have something as:

for i in range(10000):
    algorithm.run(1)
    if i%10 == 0:
        print(nondominant(algorithm.population))

I know that this is not how to do it, but I hope my idea is clearer now. Thank you

dhadka commented 4 years ago

Ah, I see what you're saying, thanks for clarifying. So there are a few things you can do...

  1. Call algorithm.step() instead of algorithm.run(). algorithm.step() performs one iteration of the algorithm.

  2. The run() method also takes a callback argument that is invoked each iteration. Something like this should work:

def print_nondominant(algorithm):
    if algorithm.nfe % 100 == 0: # this assumes 100 is a multiple of your population size
        print(nondominant(algorithm.population))

algorithm.run(10000, callback=print_nondominant)
ghost commented 4 years ago

Thank you @dhadka for your answer. That was helpful and your solution works.

Jmiguel17 commented 4 years ago

Hi,

I was wondering if instead of saving the non-dominated solutions in each generation (nondominant(algorithm.population)) if there is a way to save the archive that saves the cumulative non-dominated solutions up to the current generation?

Do all algorithms create an Archive with the non-dominated solutions? If there isn't, I could create a callback method, that would create my own Archive with the cumulative updated non-dominated solutions in each generation. However, maybe this is already implemented and just I would be slowing down the search as I would be performing a non-dom sort at the end of each generation.

beforehand thanks for your help

github-actions[bot] commented 1 year ago

This issue is stale and will be closed soon. If you feel this issue is still relevant, please comment to keep it active. Please also consider working on a fix and submitting a PR.