首页 > 解决方案 > 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"),
        )

有谁知道为什么?如果简单地复制快速启动代码对我不利,我会很困惑。

标签: python-3.xgurobimixed-integer-programming

解决方案


推荐阅读