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

**URL:** https://discourse.julialang.org/t/fixing-the-value-of-some-variables-when-defining-it-not-in-the-constraints/81218
**Category:** Optimization (Mathematical)
**Created:** [May 17, 2022, 8:24pm UTC](https://discourse.julialang.org/t/fixing-the-value-of-some-variables-when-defining-it-not-in-the-constraints/81218 "2022-05-17T20:24:23Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![CactusHamster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cactushamster/32/35481_2.png) [@CactusHamster](https://discourse.julialang.org/u/CactusHamster)
#### Post date: [May 17, 2022, 8:24pm UTC](https://discourse.julialang.org/t/fixing-the-value-of-some-variables-when-defining-it-not-in-the-constraints/81218/1 "2022-05-17T20:24:23Z")

</div>

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

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

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [May 17, 2022, 9:17pm UTC](https://discourse.julialang.org/t/fixing-the-value-of-some-variables-when-defining-it-not-in-the-constraints/81218/2 "2022-05-17T21:17:54Z")

</div>

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.

---

<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: [May 17, 2022, 11:07pm UTC](https://discourse.julialang.org/t/fixing-the-value-of-some-variables-when-defining-it-not-in-the-constraints/81218/3 "2022-05-17T23:07:22Z")

</div>

Hi @CactusHamster (I’m guessing you’re the same CactusHamster as [modeling - Coding some parameters with index zero in Julia - Operations Research Stack Exchange](https://or.stackexchange.com/questions/8413/coding-some-parameters-with-index-zero-in-julia) 😄).

To slightly elaborate on what @Henrique_Becker said,

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

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