# Type of binary variables in constraints

**URL:** https://discourse.julialang.org/t/type-of-binary-variables-in-constraints/25264
**Category:** Optimization (Mathematical)
**Created:** [June 13, 2019, 6:58pm UTC](https://discourse.julialang.org/t/type-of-binary-variables-in-constraints/25264 "2019-06-13T18:58:21Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![owiecc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/owiecc/32/8894_2.png) [@owiecc](https://discourse.julialang.org/u/owiecc)
#### Post date: [June 13, 2019, 6:58pm UTC](https://discourse.julialang.org/t/type-of-binary-variables-in-constraints/25264/1 "2019-06-13T18:58:21Z")

</div>

When I set this problem:

```julia
data = [2;3;4;-1;0]
model = Model(with_optimizer(GLPK.Optimizer))
@variable(model, x[1:length(data)], Bin)
@objective(model, Min, sum(data.*x))
optimize!(model)
typeof(value.(x))

```

I would expect the type of `x` to be `Array{Bool,1}` but I get `Array{Float64,1}` instead. Is there a reason behind using Floats instead of Bools for binary constraints?

---

<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: [June 13, 2019, 7:19pm UTC](https://discourse.julialang.org/t/type-of-binary-variables-in-constraints/25264/2 "2019-06-13T19:19:58Z")

</div>

Yes. Almost all solvers implement binary (and integer) variables in floating point. They check integrality against a tolerance (e.g., [`IntFeasTol` in Gurobi](http://www.gurobi.com/documentation/8.1/refman/numerics_tolerances_and_us.html)).

Your can (and should expect to) obtain solutions like `1.000001` that are interpreted as “binary” by the solvers.

---

<div class="post-metadata">

### Author: ![owiecc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/owiecc/32/8894_2.png) [@owiecc](https://discourse.julialang.org/u/owiecc)
#### Post date: [June 13, 2019, 7:30pm UTC](https://discourse.julialang.org/t/type-of-binary-variables-in-constraints/25264/3 "2019-06-13T19:30:07Z")

</div>

Is it due to speed, simplicity or something else?

---

<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: [June 13, 2019, 8:11pm UTC](https://discourse.julialang.org/t/type-of-binary-variables-in-constraints/25264/4 "2019-06-13T20:11:43Z")

</div>

All of the above.

The most common approach to solving these (NP-hard) problems is to relax the integrality, solve the continuous relaxation, and then successively partition the search space until the relaxed problem has an integer solution. I’m guessing you don’t have a background in integer optimization, so you may want to read up on the following:

[http://www.gurobi.com/resources/getting-started/mip-basics](http://www.gurobi.com/resources/getting-started/mip-basics)

> **[Integer programming](https://en.wikipedia.org/wiki/Integer_programming)**
>
> An integer programming problem is a mathematical optimization or feasibility program in which some or all of the variables are restricted to be integers. In many settings the term refers to integer linear programming (ILP), in which the objective function and the constraints (other than the integer constraints) are linear.
> Integer programming is NP-complete\[citation needed\]. In particular, the special case of 0-1 integer linear programming, in which unknowns are binary, and only the restrictions...

---

<div class="post-metadata">

### Author: ![owiecc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/owiecc/32/8894_2.png) [@owiecc](https://discourse.julialang.org/u/owiecc)
#### Post date: [June 13, 2019, 8:28pm UTC](https://discourse.julialang.org/t/type-of-binary-variables-in-constraints/25264/5 "2019-06-13T20:28:00Z")

</div>

Thanks. I can see there is also a bit on integer programming in the Algorithms for optimization book. Will check that out.
