# Binary Variable

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:

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

`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

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

``````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
x ∈ {0,1}
x ∈ {0,1}
``````

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

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

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

Thanks!

1 Like

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