# Round variables in JuMP

**URL:** <https://discourse.julialang.org/t/round-variables-in-jump/109045>\
**Category:** Optimization (Mathematical)\
**Created:** [January 20, 2024, 2:31pm UTC](https://discourse.julialang.org/t/round-variables-in-jump/109045 "2024-01-20T14:31:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)\
**Post date:** [January 20, 2024, 2:31pm UTC](https://discourse.julialang.org/t/round-variables-in-jump/109045/1 "2024-01-20T14:31:49Z")

</div>

Dear all,  
I have a model in JuMP with the following range index:  
`variable(model, x[i in 1:10, t in 1:10, h in H[i], bin)`

where

```julia
H=[
[1,2];
[3,4,5];
[5];
[6,7];
[1,2,3];
[1,2,4];
[1,2,3];
[1,2,6,7];
[1,2,4];
[1,2];
]

```

ie., h depends on i.

Due numerial rounds, in the optimal solution of my model, `value.(x)` are not binary.  
How can I round `value.(x)` where the index h range depends on i? I have tried `round.(value.(x))` but this does not work.

---

<div class="post-metadata">

**Author:** ![mike\_k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike_k/32/211864_2.png) [@mike\_k](https://discourse.julialang.org/u/mike_k)\
**Post date:** [January 20, 2024, 3:14pm UTC](https://discourse.julialang.org/t/round-variables-in-jump/109045/2 "2024-01-20T15:14:13Z")

</div>

You could use a loop. Something like

```julia
xvals = value.(x)

for i in 1:10, t in 1:10, h in H[i]
    xvals[i,t,h] = round(Int, xvals[i,t,h])
end

```

---

<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:** [January 20, 2024, 9:11pm UTC](https://discourse.julialang.org/t/round-variables-in-jump/109045/3 "2024-01-20T21:11:50Z")

</div>

> [@angeloaliano1](#):
>
> round.(value.(x))

This should work for the output. Perhaps even `round.(Int, value.(x))`.

But you can’t use this as a constraint inside the model. Solvers do not use exact arithmetic. They use a tolerance to check if the variable is “binary” so you can get small values like `1e-8` or nearly one values like `0.99999999` or `1.00000001`. This should be considered “0” and “1” for practical purposes.

---

<div class="post-metadata">

**Author:** ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)\
**Post date:** [January 20, 2024, 9:12pm UTC](https://discourse.julialang.org/t/round-variables-in-jump/109045/4 "2024-01-20T21:12:50Z")

</div>

I use the round after the optimization process.
