# Binary Variable

**URL:** https://discourse.julialang.org/t/binary-variable/18093
**Category:** Optimization (Mathematical)
**Created:** [November 28, 2018, 11:09am UTC](https://discourse.julialang.org/t/binary-variable/18093 "2018-11-28T11:09:20Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![muriloasouza](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/muriloasouza/32/4650_2.png) [@muriloasouza](https://discourse.julialang.org/u/muriloasouza)
#### Post date: [November 28, 2018, 11:09am UTC](https://discourse.julialang.org/t/binary-variable/18093/1 "2018-11-28T11:09:20Z")

</div>

I got in my problem some binary variable, thats a decision to install or not an equipment in a house. Among a lot of other variables and constraints, my problem is something like this:

```julia
N = 5; # number of houses
@variable(m, a[i=1:N], Bin)
@objective(m, Min, sum(a[i] for i = 1:N))

```

But for a larger N (around `30+`), the problem gets to big to be solved in an acceptable time (using a Branch-and-Bound solver).  
So, to reduce the number of binary variables, i was thinking in just choose some `a[i]` to be binary. For example, binary for `i = 2:3`:

`@variable(m, a[i=2:3], Bin)`

But i’m getting this error:

`ERROR: LoadError: Failed attempt to index JuMPArray along dimension 1: 1 ∉ 2:3`

Does anyone knows any trick in how work around this?

---

<div class="post-metadata">

### Author: ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)
#### Post date: [November 28, 2018, 12:32pm UTC](https://discourse.julialang.org/t/binary-variable/18093/2 "2018-11-28T12:32:20Z")

</div>

`1 ∉ 2:3` says there was no index 1 in 2:3(i think…), it might’ve come from `@objective` if it wasn’t updated to 2:3 as well.  
Otherwise it could just need to start from 1

…unless it really means dimensions when it says #1 is missing, but it doesn’t seem too likely

---

<div class="post-metadata">

### Author: ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)
#### Post date: [November 28, 2018, 1:51pm UTC](https://discourse.julialang.org/t/binary-variable/18093/3 "2018-11-28T13:51:42Z")

</div>

If you can to create 3 variables but only the last two are binary you need to do

```julia
julia> using JuMP
julia> model = Model()
julia> @variable(model, x[i=1:3], category = (i in 2:3 ? :Bin : :Cont))
julia> print(model)
Min 0
Subject to
 x[1]
 x[2] ∈ {0,1}
 x[3] ∈ {0,1}

```

Note that the above will work only with JuMP up to JuMP v0.18. With JuMP master, the syntax is now

```julia
@variable(model, x[i=1:3], binary = i in 2:3)

```

---

<div class="post-metadata">

### Author: ![muriloasouza](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/muriloasouza/32/4650_2.png) [@muriloasouza](https://discourse.julialang.org/u/muriloasouza)
#### Post date: [November 28, 2018, 3:31pm UTC](https://discourse.julialang.org/t/binary-variable/18093/4 "2018-11-28T15:31:34Z")

</div>

Yeah, this was the problem… i forgot to change the range of `i` in the `@objective` and in some `@constraint`.

Thanks!

---

<div class="post-metadata">

### Author: ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)
#### Post date: [November 29, 2018, 4:52am UTC](https://discourse.julialang.org/t/binary-variable/18093/5 "2018-11-29T04:52:54Z")

</div>

btw There seems to be something wrong with the JuMP documentation page, i had to Ctrl-F5 and then Esc to catch the page text before it disappeared  
/Firefox 60.0.1 on windows 7
