How to enable parallel in scipy.optimize.differential_evolution?
Asked Answered
C

1

5

I am trying to find the global minimum of a function using differential_evolution from scipy.optimize. As explained in the scipy reference guide, I should set in the options: updating='deferred',workers=number of cores

However, when I run the code, it freezes and does nothing. How can I solve this issue, or is there any better way for parallelizing the global optimizer?

The following is in my code:

scipy.optimize.differential_evolution(objective, bnds, args=(), 
            strategy='best1bin', maxiter=1e6,
            popsize=15, tol=0.01, mutation=(0.5, 1),    
            recombination=0.7, seed=None,
            callback=None, disp=False, polish=True,
            init='latinhypercube', atol=0,
            updating='deferred',workers=2)
Cacogenics answered 21/12, 2018 at 17:24 Comment(3)
Also when I use Spyder to run this code, it gives me the following error: differential_evolution() got an unexpected keyword argument 'updating'Cacogenics
I installed conda instead of using IDLE, and now it works from terminalCacogenics
following up on your comment to my question , I'll give it a shot and let you know. Could you just add what objective, bounds, and scipy version you're using? Also, where did you find about the workers argument? I can't find it on the scipy.optimize.differential_evolution pageAffiche
C
7

Came across the same problem myself. The support for parallelism in scipy.optimize.differential_evolution was added in version 1.2.0 and the version I had was too old. When looking for the documentation, the top result also referred to the old version. The newer documentation can instead be found at https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.differential_evolution.html.

I use virtualenvironment and pip for package management, and to upgrade to the latest version of scipy I just had to run pip install --upgrade scipy. If using anaconda, you might need to do e.g. conda install scipy=1.4.1.

In order to activate the parallelism, set the workers flag to something > 1 for a specific number of cores or workers=-1 to use all available cores.


One caveat: Don't make the same mistake as me and try to run the differential evolution directly in the top level of a Python script on Windows because it won't run. This is due to how multiprocessing.Pool functions. Specifically, instead of the following:

import scipy.optimize

def minimize_me(x, *args):
    ...  # Your code
    return result

# DO NOT DO LIKE THIS
...  # Prepare all the arguments
# This will give errors
result = scipy.optimize.differential_evolution(minimize_me, bounds=function_bounds, args=extraargs,
                                               disp=True, polish=False, updating='deferred', workers=-1)
print(result)

use the code below:

import scipy.optimize

def minimize_me(x, *args):
    ...  # Your code
    return result

# DO LIKE THIS
if __name__ == "__main__":
    ...  # Prepare all the arguments
    result = scipy.optimize.differential_evolution(minimize_me, bounds=function_bounds, args=extraargs,
                                                   disp=True, polish=False, updating='deferred', workers=-1)
    print(result)

See this post for more info about parallel execution on Windows: Compulsory usage of if __name__=="__main__" in windows while using multiprocessing Note that even if not on Windows, it's anyway a good practice to use if __name__ == "__main__":.

Cryoscopy answered 27/3, 2019 at 9:45 Comment(2)
Freezes for me unfortunatley, someone knows why?Spital
@Spital probably something specific to your code, maybe share some details?Palfrey

© 2022 - 2024 — McMap. All rights reserved.