Pyomo:向 cbc 求解器发送 options="threads" 会导致错误

问题描述

可以在命令行中激活多线程:

$cbc -threads=6
Welcome to the CBC MILP Solver
Version: 2.9.9
Build Date: Aug 21 2017
$command line - cbc -threads=6 (default strategy 1)
threads was changed from 0 to 6

但是当我尝试在 pyomo 代码中激活此选项时

opt = SolverFactory('cbc')
result = opt.solve(instance,options="threads=4")

我收到一个错误

File "/usr/local/lib/python3.9/dist-packages/pyomo/opt/base/solvers.py",line 561,in solve
    self.options.update(kwds.pop('options',{}))
  File "/usr/local/lib/python3.9/dist-packages/pyutilib/misc/misc.py",line 360,in update
    if type(d[k]) is dict:
TypeError: string indices must be integers

有什么想法吗?

解决方法

options 关键字参数需要字典。如果您想使用与命令行相同的语法,则在 options_string

之后
opt.solve(instance,options_string="threads=4")
opt.solve(instance,options={"threads": 4})