Skip to content

pymoo/numpy-seed-ignored

Rule numpy-seed-ignored
Pack pymoo
Severity warning
Fix none, edit by hand
Library versions pymoo 0.6.1.6 and later

Since pymoo 0.6.1.6, pymoo draws from its own generator, so np.random.seed does not fix a run. Pass seed to minimize.

class MySampling(Sampling):
def _do(self, problem, n_samples, **kwargs):
return np.random.random((n_samples, problem.n_var))
def _do(self, problem, n_samples, random_state=None, **kwargs):
return random_state.random((n_samples, problem.n_var))
np.random.seed(1)
res = minimize(problem, NSGA2(pop_size=100, sampling=MySampling()), ("n_gen", 200))
res = minimize(problem, NSGA2(pop_size=100, sampling=MySampling()), ("n_gen", 200), seed=1)

The rule reports np.random.seed in a file whose minimize gets no seed, and a draw from np.random inside _do of a class that inherits from Sampling, Crossover or Mutation. Since pymoo 0.6.1.6 the library draws from a generator that minimize builds from seed and passes to each operator as random_state.

On pymoo 0.6.1.5 two runs after np.random.seed(1) returned the same front, and on 0.6.1.6 and 0.6.2 they returned different fronts. A custom operator gave the results in the table in two runs with seed=1.

Operator Draws from np.random Draws from random_state
a sampling different results the same result twice
a crossover different results the same result twice
a mutation different results the same result twice

xpd has no automatic fix, so make the change by hand.

xpd prints this message with the issue. A name that starts with $ stands for code from your file.

np.random.seed does not fix a pymoo run since 0.6.1.6, because pymoo draws from its own generator. Two runs after np.random.seed(1) returned different fronts. Pass seed to minimize.

Browse the other rules of the pymoo pack.