将可调用的“类似地图”传递给 scipy 的差异进化

问题描述

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

...类似地图的可调用对象,例如用于并行评估总体的 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)]
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,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)]

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.])

我的 scipy 版本是

import scipy
print(scipy.__version__)
1.6.1