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

seed option to keep the results reproducible #128

Closed ashuein closed 1 year ago

ashuein commented 4 years ago

Is there a "seed" option in the Platypus which can be defined to keep the results reproducible?

dhadka commented 4 years ago

Try https://docs.python.org/3/library/random.html#random.seed

ashuein commented 4 years ago

Thanks dhadka for the response. The python random.seed function, I know, but I am asking how to use it in the Platypus function? NSGAII or NSGAIII doesn't seem to have a seed argument in the function call ?

mronda commented 4 years ago

Hi @ashuein , you can try to monkey patch the initialization of the algorithm you are using to init a random seed. For example:

from platypus.algorithms import NSGAII 

old_init = NSGAII.__init__

def new_init(self, problem,
                 population_size = 100,
                 generator = RandomGenerator(),
                 selector = TournamentSelector(2),
                 variator = None,
                 archive = None,
                 seed = None
                 **kwargs):

    old_init(self, problem,
                 population_size = 100,
                 generator = RandomGenerator(),
                 selector = TournamentSelector(2),
                 variator = None,
                 archive = None,
                 **kwargs)

    if self.seed is None:
        self.seed = random.randint(0,1000)
        random.seed(self.seed)
    else:
        random.seed(self.seed)

NSGAII.__init__ = new_init

You can save that into a file and import before you begin calling Platypus stuff.

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.