Fix vs @constraint in JuMP

Dear All,

I am trying to solve a mixed-integer optimization problem in JuMP and Gurobi. I have a variable x_{i,j} over i,j \in \{1,2,\ldots,n\}, where n is a fairly large number. However, for this particular problem, over an index set
P=\{(i,j) \mid (i,j) \textrm{ satisfying some property}\},
I have x_{i,j} = 0 for all i,j \in P.

Because there are other constraints involving x_{i,j}, it is more convenient if I set those values over P to zero through JuMP rather than hard coding them. I see there are two ways I can do that in JuMP, one is via fix and the other one is through @constraint macro, e.g.,

for i,j in P
      @constraint(model_name, x[i,j] == 0.0)
end

or

for i, j in P
    fix(x[i,j], 0.0; force = true)
end

For better performance, which one would be more suitable? Any tips/suggestions will be much appreciated!

@constraint adds a new linear constraint (row to the constraint matrix).

fix modifies variable bounds.

You should almost always use fix.

Opened a PR to clarify the docs: [docs] suggest fix over a new constraint by odow · Pull Request #2645 · jump-dev/JuMP.jl · GitHub

1 Like

Thanks so much, @odow !

1 Like