# Fix variables or add as a contraint?

**URL:** https://discourse.julialang.org/t/fix-variables-or-add-as-a-contraint/108594
**Category:** Optimization (Mathematical)
**Tags:** gurobi
**Created:** [January 9, 2024, 9:49pm UTC](https://discourse.julialang.org/t/fix-variables-or-add-as-a-contraint/108594 "2024-01-09T21:49:16Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)
#### Post date: [January 9, 2024, 9:49pm UTC](https://discourse.julialang.org/t/fix-variables-or-add-as-a-contraint/108594/1 "2024-01-09T21:49:16Z")

</div>

Dear all,  
I have an Integer (binary) problem where I want to fix some variables to obtain an heuristic solution.  
I use to add the constraint:

```julia
@constraint(model, x[j] == 1)

```

if I want to fix x\_j in 1.  
I would like to know if there is difference in the performance of the Gurobi if I introduce the command

```julia
fix(x[j],1)

```

in the branch and bound tree. Which option is better from computational perspective?

---

<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: [January 9, 2024, 10:03pm UTC](https://discourse.julialang.org/t/fix-variables-or-add-as-a-contraint/108594/2 "2024-01-09T22:03:29Z")

</div>

It is better to use `fix`.

`fix` sets the lower and upper bounds of the variable to the value.

`@constraint` adds a new row to the constraint matrix, and `@constraint(model, x[j] == 1)` is equivalent to `@constraint(model, 1.0 * x[j] == 1)`.

---

<div class="post-metadata">

### Author: ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)
#### Post date: [January 9, 2024, 10:13pm UTC](https://discourse.julialang.org/t/fix-variables-or-add-as-a-contraint/108594/3 "2024-01-09T22:13:51Z")

</div>

If, for example, I have a constraint in my model:  
\displaystyle \sum\_{i=1}^{100} x\_j = 1  
and I use `fix(x[23],1)`, automaticaly Gurobi sets x\_i = 0 for all i \neq 23 or this command lead this information in the branch and bound tree?

---

<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: [January 9, 2024, 10:15pm UTC](https://discourse.julialang.org/t/fix-variables-or-add-as-a-contraint/108594/4 "2024-01-09T22:15:41Z")

</div>

Yes, Gurobi has a presolve routine that will identify this and set the variable values to zero before it starts branch and bound.

(It will also identify `@constraint(model, x == 1)`, but it’s still better to use `fix`.)

---

<div class="post-metadata">

### Author: ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)
#### Post date: [January 9, 2024, 10:55pm UTC](https://discourse.julialang.org/t/fix-variables-or-add-as-a-contraint/108594/5 "2024-01-09T22:55:54Z")

</div>

Dear @odow, it is possible in a binary model with 100 variables, I relax only the 10 first? I know that the command `relax_integrality(model)` relax all variables; I want only a subset.

---

<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: [January 9, 2024, 11:04pm UTC](https://discourse.julialang.org/t/fix-variables-or-add-as-a-contraint/108594/6 "2024-01-09T23:04:48Z")

</div>

You can use `unset_binary(x)` to relax a single variable:

> **[Variables · JuMP](https://jump.dev/JuMP.jl/stable/manual/variables/#Binary-variables)**
>
> Documentation for JuMP.

Just loop over the 10 variables that you want to relax.
