如何判断Gurobi求解的JuMP模型是否为MIP?

How can I determine whether a JuMP model solved by Gurobi is a MIP?

假设,我创建了一个 JuMP model, pass it to the solver and retrieve a solution. Now I want to determine whether the model solved by Gurobi(即在预求解之后)是一个混合整数程序 (MIP)。我需要此信息,因为我想打印解决方案的 MIP 间隙(如果存在)。显然,不一定事先知道,如果JuMP模型实际上是一个MIP,或者是否所有整数变量都会被presolve移除。

此代码示例创建了一个简单模型(没有任何整数变量)并对其求解:

import JuMP
import Gurobi

model = JuMP.Model(Gurobi.Optimizer)

JuMP.@variable(model, x)
JuMP.@constraint(model, x>=0)
JuMP.@objective(model, Min, x)

JuMP.optimize!(model)

如果问题是(即使在预求解之后)MIP,我可以使用

mip_gap = JuMP.relative_gap(model)

获取 MIP 差距。但在上述情况下(即不是 MIP),它会触发

ERROR: Gurobi.GurobiError(10005, "Unable to retrieve attribute 'MIPGap'")

也不起作用的是

mip_gap = JuMP.get_optimizer_attribute(model, "MIPGap")

因为这个 returns 用作终止标准的 MIP 间隙(即不是实际解决方案的 MIP 间隙)。

我在JuMP和MathOptInterface that returns the MIP gap directly. However, Gurobi has a model attribute called IsMIP的源代码中没有找到任何功能,应该可以访问。但是

is_mip = JuMP.get_optimizer_attribute(model, "IsMIP")

原因

ERROR: LoadError: Unrecognized parameter name: IsMIP.

我也试图在 Gurobi.jl and discovered that the Gurobi parameter "IsMIP" is implemented here. There is also a function called is_mip that indeed does what I want. The problem is, that I can not use it because the argument has to be a Gurobi Model 中找到解决方案,而不是 JuMP 模型。

我能做什么?

很遗憾,有几件事共同导致了您的问题。

1) JuMP的"optimizer attributes"对应Gurobi的"parameters." 所以只能用get/set_optimizer_attribute查询公差之类的东西。这就是为什么您可以查询 MIPGap(Gurobi 参数),但不能查询 IsMIP(Gurobi 模型属性)的原因。

2) 不用担心,因为您应该能够访问 Gurobi 模型属性(和 variable/constraint 属性),如下所示:

MOI.get(model, Gurobi.ModelAttribute("IsMIP"))

3) 但是,似乎堆栈中某处存在错误,这意味着我们在尝试从 JuMP 转到 Gurobi 时错误地重定向了调用。作为解决方法,您可以使用

MOI.get(backend(model).optimizer, Gurobi.ModelAttribute("IsMIP"))

我已提交问题,以便在未来的版本中解决此问题 (https://github.com/JuliaOpt/MathOptInterface.jl/issues/1092)。