scipy.optimize.differential_evolution不会与worker并行工作

ljsrvy3e  于 2023-08-05  发布在  其他
关注(0)|答案(1)|浏览(81)

我试图在使用scipy=1.10.1和Python 3.9.7(macOS Ventura 13.4)调用differential_evolution时,在并行设置workers=-1中最小化__optimisation_function,但我得到了下面的错误。
你知道如何让这个工作并行吗?
df_1df_2是pandas DataFrames。
differential_evolution中删除workers=-1使其工作正常。

from scipy.optimize import differential_evolution

class Optimisation():

    def __optimisation_function(self, x, *args) -> float:

        a, b, c, d, e = x

        df_1 = args[0]
        df_2 = args[1]

        return something

    def run_optimisation(self):

        optimisation_result = differential_evolution(
            func=self.__optimisation_function,
            bounds=bounds,
            constraints=constraints,
            args=(df_1, df_2),
            workers=-1
        )

字符串
错误类型

.../python3.9/site-packages/scipy/optimize/_differentialevolution.py:382: UserWarning: differential_evolution: the 'workers' keyword has overridden updating='immediate' to updating='deferred'
  with DifferentialEvolutionSolver(func, bounds, args=args,
Process SpawnPoolWorker-2:
Traceback (most recent call last):
  File ".../python3.9/multiprocessing/process.py", line 315, in _bootstrap
    self.run()
  File ".../python3.9/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File ".../python3.9/multiprocessing/pool.py", line 114, in worker
    task = get()
  File ".../python3.9/multiprocessing/queues.py", line 368, in get
    return _ForkingPickler.loads(res)
AttributeError: 'Optimisation' object has no attribute '__optimisation_function'
Process SpawnPoolWorker-1:
Traceback (most recent call last):
  File ".../python3.9/multiprocessing/process.py", line 315, in _bootstrap
    self.run()
  File ".../python3.9/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File ".../python3.9/multiprocessing/pool.py", line 114, in worker
    task = get()
  File ".../python3.9/multiprocessing/queues.py", line 368, in get
    return _ForkingPickler.loads(res)
AttributeError: 'Optimisation' object has no attribute '__optimisation_function'

slsn1g29

slsn1g291#

将我的函数名从__optimisation_function修改为optimisation_function解决了我的问题。
呼叫

optimisation_result = differential_evolution(
    func=Optimisation._Optimisation__optimisation_function,
    bounds=bounds,
    constraints=constraints,
    args=(df_1, df_2),
    workers=-1
)

字符串
其中func= Optimisation._Optimisation__optimisation_function而不是func= self.__optimisation_function
返回

Exception has occurred: PicklingError
Can't pickle <function Optimisation.__optimisation_function at 0x7fa17107e040>: attribute lookup Optimisation.__optimisation_function on source.optimiser failed


因此,我不得不更改方法名称,因为__optimisation_function似乎不可pickleable。
结果是pickle如何查找__name__来序列化该方法的问题。
此问题将在here中进一步详细说明。

相关问题