Fix variables or add as a contraint?

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:

@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

fix(x[j],1)

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

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

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?

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

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.

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

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