Reputation: 1896
Let us suppose for demonstration that I have the following model that doesn't represent anything, that is only for an example:
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)
Now,
julia> num_constraints(m, GenericAffExpr{Float64,VariableRef}, MOI.GreaterThan{Float64})
46
Which is good. Then, the following:
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
Which means that the model now has doubled its number of constraints while all of them are in duplicate. This is problematic to me because I have another real model where I have many more constraints and I am sometimes adding constraints that might be duplicate or might not. All of this in for and while loops. So I was wondering if:
That is somewhat summarized by "how JuMP solvers deal with duplicate constraints?".
Finally, I understand num_constraints
function returns all user entered constraints. How can I get the number of unique constraints?
Upvotes: 0
Views: 237
Reputation: 2574
JuMP doesn't distinguish unique constraints. It will add every constraint you tell it to add. One reason for this is that it is possible to modify constraints after they have been created. However, I would only worry if you can show that the redundant constraints are a cause of significant slow-down. If so, I suggest you refactor your code to not add redundant constraints.
To respond to your questions:
It is only the number of constraints that doubles but the model exactly knows that he can avoid dealing with half of them?
Each solver's presolve should remove redundant constraints (but this takes time).
Does he still memorize the constraints and while avoiding half of them, it still takes double the memory?
Yes. 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?
There is no functionality to check if a constraint has already been added.
Upvotes: 1