我试图在使用scipy=1.10.1和Python 3.9.7(macOS Ventura 13.4)调用differential_evolution
时,在并行设置workers=-1
中最小化__optimisation_function
,但我得到了下面的错误。
你知道如何让这个工作并行吗?df_1
和df_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'
型
1条答案
按热度按时间slsn1g291#
将我的函数名从
__optimisation_function
修改为optimisation_function
解决了我的问题。呼叫
字符串
其中func=
Optimisation._Optimisation__optimisation_function
而不是func=self.__optimisation_function
返回
型
因此,我不得不更改方法名称,因为
__optimisation_function
似乎不可pickleable。结果是
pickle
如何查找__name__
来序列化该方法的问题。此问题将在here中进一步详细说明。