python-3.x - Python MIP 无法添加二进制变量向量
问题描述
我正在尝试在 python 中使用 MIP 包。我正在使用快速入门指南(https://python-mip.readthedocs.io/en/latest/quickstart.html)中的代码,但它会为二进制变量引发以下错误:
m = Model(sense=MAXIMIZE, solver_name=GRB) # using gurobi instead of CBC
x = m.add_var()
n = 10
y = [ m.add_var(var_type=BINARY) for i in range(n) ]
File "C:\Users\NAME\anaconda3\lib\site-packages\mip\gurobi.py", line 454, in add_var
name.encode("utf-8"),
TypeError: initializer for ctype 'char' must be a bytes of length 1, not bytes
该行对应于 gurobi.py 中此函数的最后一行,但我不知道这是如何分解的
def add_var(
self,
obj: float = 0,
lb: float = 0,
ub: float = INF,
var_type: str = CONTINUOUS,
column: Column = None,
name: str = "",
):
# collecting column data
nz = 0 if column is None else len(column.constrs)
if nz:
self.flush_rows()
vind = ffi.new("int[]", [c.idx for c in column.constrs])
vval = ffi.new("double[]", [column.coeffs[i] for i in range(nz)])
else:
vind = ffi.NULL
vval = ffi.NULL
# variable type
vtype = var_type.encode("utf-8")
st = GRBaddvar(
self._model,
nz,
vind,
vval,
obj,
lb,
ub,
vtype,
name.encode("utf-8"),
)
有谁知道为什么?如果简单地复制快速启动代码对我不利,我会很困惑。
解决方案
推荐阅读
- json - Ruby - 尝试使用嵌套散列迭代散列(在反序列化 JSON 对象之后)
- javascript - 如何选择第一个可用选项
- typescript - Typescript 使用路径来引用与文件路径不同的文件夹中的类型
- python - 简单的scapy脚本不发送数据包
- javascript - Intellisense 不适用于 VS 2019 PREVIEW 中的 Javascript
- uci - 如何使用 UCI 国际象棋引擎(例如 Stockfish)设置自定义棋盘
- regex - 仅当行不以某个字符开头时,powershell才替换文件中的行
- uicollectionview - 具有动态高度的 UICollectionViewLayout - 但不使用流布局
- swift - 不能对“Bool”类型的非可选值使用可选链接
- html - 如何修复出现在 ERB 生成的 HTML 中的额外数据