pythonscipyscipy-optimizedifferential-evolution

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


I have an existing multiprocessing pool that I use for other functions that I'd like to pass to differential_evolution but I can't seem to get the worker input set correctly. Is this possible? The docs say that workers should be

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

I tried:

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'

Thanks.


Solution

  • For me your second solution is working

    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
    

    output

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

    my scipy version is

    import scipy
    print(scipy.__version__)
    1.6.1