Error when executing the workers parameter in scipy.differential_evolution

Viewed 257

I'm trying to learn about parallel execution for Differential Evolution method. I used the example found on the documentation:

from scipy.optimize import rosen, differential_evolution
bounds = [(0,2), (0, 2), (0, 2), (0, 2), (0, 2)]
result = differential_evolution(rosen, bounds, updating='deferred',workers=2)

But it throws the following error: enter image description here

Can somebody please suggest a method to solve this issue.

1 Answers

That Scipy function uses the multiprocessing module under the hood if the workers argument is supplied. We must therefore "protect the entry point of the program by using if __name__ == '__main__'" as the documentation insists in section "Safe importing of main module".

if __name__ == '__main__':
    from scipy.optimize import rosen, differential_evolution
    bounds = [(0,2), (0, 2), (0, 2), (0, 2), (0, 2)]
    result = differential_evolution(rosen, bounds, updating='deferred',workers=2)
    print(result)

The example from the Scipy documentation is run in an interactive Python session where this is not necessary. But when run as a script, we must add the code fence to avoid the error you encountered. Otherwise the optimization run is started over and over again in each of the multiple processes.

The code fence is also not necessary when running the script on Linux. On that platform, the default "start method" for the worker processes is "fork" (as mentioned in the error message) and not "spawn" as on Windows and macOS. The "fork" method is more lightweight and avoids restarting the Python interpreter, which is why the entry point need not be protected.

Related