-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
issue 19 : A note on reproducibility #29
issue 19 : A note on reproducibility #29
Conversation
Looks good to me, do you agree @chkoar? |
You may want to include the use of your new |
Yes. But I have the impression that this approach is not safe in threaded environments. import random
import numpy as np
import time
from moead_framework.problem.numerical import Zdt1
from concurrent.futures import ThreadPoolExecutor
random.seed(0)
problem = Zdt1(2)
expected = [
[0.8444218515250481, 0.7579544029403025],
[0.420571580830845, 0.25891675029296335],
]
solutions = []
solutions.append(problem.generate_random_solution().solution)
solutions.append(problem.generate_random_solution().solution)
assert np.array_equal(solutions, expected)
def worker(*args):
time.sleep(np.random.randint(1,3))
return problem.generate_random_solution().solution
for i in range(10):
random.seed(0)
with ThreadPoolExecutor() as executor:
solutions = executor.map(worker, range(2))
solutions = [x for x in solutions]
assert np.array_equal(solutions, expected) |
It is available in 2c322fa
From the python documentation :
The solution available is to instantiate a new instance of Random for each Thread and use this instance in problems and algorithms but it requires a lot of refactoring in the framework to replace each call of "import random" (and it is only for the Random module, the problem is maybe the same for np.random)
I propose to add a note to prevent that this approach is not safe in threaded environments in python. |
A note in the docstring |
Available in 2b313c1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be sufficient for me
Well, having this I believe is sufficient. |
add the function set_seed()