添加重复约束时 JuMP 求解器的行为

JuMP solvers' behavior when adding duplicate constraints

让我们假设为了演示,我有以下不代表任何东西的模型,这只是一个例子:

using GLPK
using JuMP
m = Model(GLPK.Optimizer)
@variable(m, y[i=1:100], Bin)
@objective(m, Min, sum(y))
@constraint(m, [j=5:50], sum([y[i] for i in j:j+10]) >= 5)

现在,

julia> num_constraints(m, GenericAffExpr{Float64,VariableRef}, MOI.GreaterThan{Float64})
46

哪个好。然后是:

julia> @constraint(m, [j=5:50], sum([y[i] for i in j:j+10]) >= 5)
[...]
julia> num_constraints(m, GenericAffExpr{Float64,VariableRef}, MOI.GreaterThan{Float64})
92

这意味着模型现在的约束数量增加了一倍,而所有约束都是重复的。这对我来说是有问题的,因为我有另一个真实模型,其中有更多约束,有时我会添加可能重复或不重复的约束。所有这些都在 for 和 while 循环中。所以我想知道是否:

“JuMP 求解器如何处理重复约束?”总结了这一点。

终于,我理解了num_constraints函数returns所有用户输入的约束。如何获取唯一约束的数量?

JuMP 不区分唯一约束。它将添加您告诉它添加的每个约束。这样做的原因之一是可以在创建约束后对其进行修改。但是,如果您能证明冗余约束是显着减速的原因,我只会担心。如果是这样,我建议您重构代码以不添加冗余约束。

回答您的问题:

It is only the number of constraints that doubles but the model exactly knows that he can avoid dealing with half of them?

每个求解器的预求解都应该移除冗余约束(但这需要时间)。

Does he still memorize the constraints and while avoiding half of them, it still takes double the memory?

是的。它仍然需要双倍的内存。

And if so, is there a way to check whether a set of constraints already exist before adding it?

没有检查是否已添加约束的功能。