将 'map-like' 可调用传递给 scipy 的 differential_evolution

Passing a 'map-like' callable to scipy's differential_evolution

我有一个现有的多处理池,用于我想传递给 differential_evolution 的其他功能,但我似乎无法正确设置工作人员输入。这可能吗? docsworkers 应该是

...a map-like callable, such as multiprocessing.Pool.map for evaluating the population in parallel.

我试过了:

import multiprocessing as mp
from scipy.optimize import rosen, differential_evolution

pool = mp.Pool(2)  # existing worker pool

bounds = [(0,2), (0, 2), (0, 2), (0, 2), (0, 2)]
result = differential_evolution(rosen, bounds, updating='deferred', workers=pool)
# TypeError: int() argument must be a string, a bytes-like object or a number, not 'Pool'

result = differential_evolution(rosen, bounds, updating='deferred', workers=pool.map)
# RuntimeError: The map-like callable must be of the form f(func, iterable), returning a sequence of numbers the same length as 'iterable'

谢谢。

对我来说,你的第二个解决方案有效

import multiprocessing as mp
from scipy.optimize import rosen, differential_evolution

pool = mp.Pool(2)  # existing worker pool

bounds = [(0,2), (0, 2), (0, 2), (0, 2), (0, 2)]

result = differential_evolution(rosen, bounds, updating='deferred', workers=pool.map)
result

输出

     fun: 0.0
 message: 'Optimization terminated successfully.'
    nfev: 51006
     nit: 679
 success: True
       x: array([1., 1., 1., 1., 1.])

我的scipy版本是

import scipy
print(scipy.__version__)
1.6.1