thouska / spotpy

A Statistical Parameter Optimization Tool
https://spotpy.readthedocs.io/en/latest/
MIT License
248 stars 150 forks source link

Not possible to reproduce results with `random_state` #267

Closed mr-pablinho closed 3 years ago

mr-pablinho commented 3 years ago

Hello! I am using the DREAM algorithm and realized that it is not possible to reproduce the results by defining a number in the random_state variable within the function spotpy.algorithms.dream. In order to reproduce results, one has to add random.seed(any_number) at the beginning of the script. For instance:


if __name__ == "__main__":

    # set the random state
    random_state = 8
    random.seed(random_state)

    # general settings
    parallel ='seq'   
    spot_setup = spot_setup(GausianLike)

    # select number of maximum repetitions
    rep = 1000

    # number of chains and convergence limit (Gelman-Rubin)
    nChains = 4
    convergence_limit = 1.2

    # define DREAM algorithm parameters (further details in Vrugt, 2016)
    nCr = 3
    eps = 10e-6
    runs_after_convergence = 100
    acceptance_test_option = 6

    # initiate DREAM algorithm
    sampler = spotpy.algorithms.dream(spot_setup, 
                                      dbname='myData', 
                                      dbformat='csv',
                                      db_precision=np.float32, 
                                      save_sim=True,
                                      random_state=random_state)

    r_hat = sampler.sample(rep, 
                           nChains=nChains, 
                           convergence_limit=convergence_limit,
                           runs_after_convergence=runs_after_convergence,
                           acceptance_test_option=acceptance_test_option)

    # load the likelihood, and the parameter values of all simulations
    results = sampler.getdata()   

If I am right, this could be introduce also in the examples of the repository for clarification. If a pull request is needed I could do it. I've been also trying to solve the issue in the source code, but I've been not able so far.

If I am wrong, is there something that I didn't understand? I would be glad to know.

Thanks for you work and time on this tool.

thouska commented 3 years ago

Hi @mr-pablinho thank you for your message and your detailed error describtion. Indeed dream uses the sitepacke "random" and the other algorithms use "numpy.random" and I had set the random state for the numpy package only. I uploaded a corresponding fix. Would you mind to test it?

mr-pablinho commented 3 years ago

Sure, no problem! I did it and it is working now. Great! Thank you!

thouska commented 3 years ago

Perfect, thank you for testing and your feedback!