# Fixing the value of some variables when defining it, not in the constraints

I’m very new to JuMP so this might be a trivial question.

We have a binary variable x_{ij}. We would like to set x_{ij} to be zero for some i and some j. So for example, nodes 1:10 cannot be assigned to nodes 1:5. So here’s what I did so far

``````for i = 1:10
for j = 1:5
@constraint(model, x[i, j] == 0)
end
end

But I was wondering if this would increase the solution time and if there's a way to fix those variables when defining \$x\$ so the processing time is shorter?``````
1 Like

The `@variable` has keyword arguments `lower_bound` and `upper_bound`, if you set both to zero this will have the same effect as using a constraint to force equality to zero. There are also the `JuMP.fix` and `JuMP.unfix`. I do not believe any of these solutions should have a big impact on the solving time, but it is always good to profile your specific case.

2 Likes

Hi @CactusHamster (I’m guessing you’re the same CactusHamster as modeling - Coding some parameters with index zero in Julia - Operations Research Stack Exchange ).

To slightly elaborate on what @Henrique_Becker said,

``````for i=1:10, j=1:5
@constraint(model, x[i, j] == 0)
end
``````

will add 50 new linear constraints to your problem (rows to the constraint matrix). These should get pre-solved out by the solver, but it’s still work that needs to be done.

In contrast,

``````for i=1:10, j=1:5
fix(x[i, j], 0.0)  # potentially with `; force = true` if there are other bounds
end
``````

will set the lower and upper bounds of `x[i, j]` to `0.0`. This will not add any new constraints so it might be a bit faster.

3 Likes