1

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

...类似于地图的可调用对象,例如用于并行评估总体的 multiprocessing.Pool.map。

我试过了:

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'

谢谢。

4

1 回答 1

1

对我来说,您的第二个解决方案正在工作

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
于 2021-04-01T09:55:48.120 回答