# Fix vs @constraint in JuMP

**URL:** <https://discourse.julialang.org/t/fix-vs-constraint-in-jump/64509>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [July 12, 2021, 2:07pm UTC](https://discourse.julialang.org/t/fix-vs-constraint-in-jump/64509 "2021-07-12T14:07:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shuvomoy\_Das\_Gupta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuvomoy_das_gupta/32/10069_2.png) [@Shuvomoy\_Das\_Gupta](https://discourse.julialang.org/u/Shuvomoy_Das_Gupta)\
**Post date:** [July 12, 2021, 2:07pm UTC](https://discourse.julialang.org/t/fix-vs-constraint-in-jump/64509/1 "2021-07-12T14:07:05Z")

</div>

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.,

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

```

or

```julia
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!

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [July 12, 2021, 8:23pm UTC](https://discourse.julialang.org/t/fix-vs-constraint-in-jump/64509/2 "2021-07-12T20:23:42Z")

</div>

`@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](https://github.com/jump-dev/JuMP.jl/pull/2645)

---

<div class="post-metadata">

**Author:** ![Shuvomoy\_Das\_Gupta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuvomoy_das_gupta/32/10069_2.png) [@Shuvomoy\_Das\_Gupta](https://discourse.julialang.org/u/Shuvomoy_Das_Gupta)\
**Post date:** [July 12, 2021, 9:02pm UTC](https://discourse.julialang.org/t/fix-vs-constraint-in-jump/64509/3 "2021-07-12T21:02:35Z")

</div>

Thanks so much, @odow !
