How to enable parallel in scipy.optimize.differential_evolution?

試著忘記壹切 提交于 2020-12-11 05:00:13

问题


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)

回答1:


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__":.



来源:https://stackoverflow.com/questions/53888676/how-to-enable-parallel-in-scipy-optimize-differential-evolution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!