1

我正在尝试使用scipy 差分进化算法workers的参数。

当我把它设置为 1 时,我的脚本运行没有问题。如果我放了一些不同的东西,它会失败并出现以下回溯:

Traceback (most recent call last):
  File "/home/ubuntu/.local/lib/python3.6/site-packages/scipy/_lib/_util.py", line 419, in __call__
    return self._mapfunc(func, iterable)
  File "/usr/lib/python3.6/multiprocessing/pool.py", line 266, in map
    return self._map_async(func, iterable, mapstar, chunksize).get()
  File "/usr/lib/python3.6/multiprocessing/pool.py", line 644, in get
    raise self._value
  File "/usr/lib/python3.6/multiprocessing/pool.py", line 424, in _handle_tasks
    put(task)
  File "/usr/lib/python3.6/multiprocessing/connection.py", line 206, in send
    self._send_bytes(_ForkingPickler.dumps(obj))
  File "/usr/lib/python3.6/multiprocessing/reduction.py", line 51, in dumps
    cls(buf, protocol).dump(obj)
TypeError: cannot serialize '_io.TextIOWrapper' object

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ubuntu/.local/lib/python3.6/site-packages/scipy/optimize/_differentialevolution.py", line 878, in _calculate_population_energies
    parameters_pop[0:nfevs]))
  File "/home/ubuntu/.local/lib/python3.6/site-packages/scipy/_lib/_util.py", line 422, in __call__
    raise TypeError("The map-like callable must be of the"
TypeError: The map-like callable must be of the form f(func, iterable)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main_parallel.py", line 323, in <module>
    optimizer(args.asset_class, udl, tenor, args.criteria, params_decay, params, couch_server, args.num_workers)
  File "main_parallel.py", line 269, in optimizer
    maxiter=5, workers=num_workers, mutation=(0.5, 1.5), recombination=0.8)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/scipy/optimize/_differentialevolution.py", line 306, in differential_evolution
    ret = solver.solve()
  File "/home/ubuntu/.local/lib/python3.6/site-packages/scipy/optimize/_differentialevolution.py", line 745, in solve
    self.population[self.feasible]))
  File "/home/ubuntu/.local/lib/python3.6/site-packages/scipy/optimize/_differentialevolution.py", line 883, in _calculate_population_energies
    raise RuntimeError("The map-like callable must be of the"
RuntimeError: The map-like callable must be of the form f(func, iterable), returning a sequence of numbers the same length as 'iterable'

有人可以帮忙解释如何使用这个参数(特定的目标函数?其他约束?)?

还可以欣赏使用多核的简单 python 差异进化优化的示例。

4

1 回答 1

2

关于工人使用的差异进化文档的关键部分是警告,当worker != 1

[...] 要求是func可腌制的

引发的错误中有几个提示表明func不可腌制,即

    self._mapfunc(func, iterable)
    ...
    self._send_bytes(_ForkingPickler.dumps(obj))
    ...
TypeError: cannot serialize '_io.TextIOWrapper' object

显然,有人尝试过酸洗func,但失败了,大概是因为func不可酸洗。正如文档中的另一个签名所表明的那样,它看起来像是workers试图被解释为map-like 或 callable 。可以预见的是,这也失败了,因为workers实际上是一个int.

该文档包含一个完整的示例workers != 1其中可以正常工作,

from scipy.optimize import differential_evolution, rosen
bounds = [(0,2), (0, 2), (0, 2), (0, 2), (0, 2)]
result = differential_evolution(rosen, bounds, updating='deferred',
                                workers=-1)
result.x, result.fun

如果您通过酸洗func将所使用的内容重构scipy.optimize.differential_evolution为可序列化,您应该能够成功使用。workers != 1

于 2020-01-15T03:52:10.580 回答