# Variant of an "elseif" loop with binary values

**URL:** https://discourse.julialang.org/t/variant-of-an-elseif-loop-with-binary-values/105348
**Category:** Optimization (Mathematical)
**Tags:** gurobi
**Created:** [October 24, 2023, 2:08pm UTC](https://discourse.julialang.org/t/variant-of-an-elseif-loop-with-binary-values/105348 "2023-10-24T14:08:48Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Fabian1](https://avatars.discourse-cdn.com/v4/letter/f/eada6e/32.png) [@Fabian1](https://discourse.julialang.org/u/Fabian1)
#### Post date: [October 24, 2023, 2:08pm UTC](https://discourse.julialang.org/t/variant-of-an-elseif-loop-with-binary-values/105348/1 "2023-10-24T14:08:49Z")

</div>

Hi !

I’m looking for another possibility to implement my elseif loop for my linear program.  
The goal is to implement the following:

```julia
if q_out >= 9
    eff == 0.75
elseif 7 <= q_out < 9
    eff == 1
else
    eff == 0.5
end

```

I have already implemented these lines of code (see an extract below).

```plaintext
set_optimizer_attribute(model, "NonConvex",2)

M = 10^(10) # parameter to select my binary variable

# definition of the variables
@variable(model, 0 <= q_out <= 15)
@variable(model, 0 <= eff <= 1)
@variable(model, 0 <= z_1 <= 1, binary=true)  
@variable(model, 0 <= z_2 <= 1, binary=true)

# constraints
@constraint(model, q_out <= 7-0.1*10^(-1) + M * (1 - z_1)) 
@constraint(model, q_out >= 7-0.1*10^(-1) - M * z_1)
    
@constraint(model, q_out <= 9-0.1*10^(-1) + M * (1 - z_2)) 
@constraint(model, q_out >= 9-0.1*10^(-1) - M * z_2)

@constraint(model, eff == 0.75 * (1-z_2) + 1*z_2 + (0.5-1)*z_1 )

```

But I’m not convinced of its result.  
When `q_out` approaches the limit 9, for example `q_out = 8.989999389648451`, my binary value `z_2=0.9999999999999933` is no longer binary, which is problematic for the variable `eff`.

Has somebody an idea of other possibilities to write my code to avoid this kind of problem?

Thank you in advance for your help!

---

<div class="post-metadata">

### Author: ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)
#### Post date: [October 24, 2023, 2:21pm UTC](https://discourse.julialang.org/t/variant-of-an-elseif-loop-with-binary-values/105348/2 "2023-10-24T14:21:09Z")

</div>

I think it is expected binary/integer variables do not fulfil their constraint exactly for performance reasons. The details will depend on the solver used ([example](https://github.com/ERGO-Code/HiGHS/pull/560#issuecomment-896507341)). If you want exact values then I suggest rounding with `round(Int, x)`

---

<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: [October 24, 2023, 9:03pm UTC](https://discourse.julialang.org/t/variant-of-an-elseif-loop-with-binary-values/105348/3 "2023-10-24T21:03:21Z")

</div>

Just to clarify, you cannot use `round(Int, x)` inside a JuMP model.

Gurobi has some helpful articles for understanding what is going on:

> **[Guidelines for Numerical Issues - Gurobi Optimization](https://www.gurobi.com/documentation/10.0/refman/guidelines_for_numerical_i.html)**
>
> Guidelines for Numerical Issues

> **[Dealing with big-M constraints - Gurobi Optimization](https://www.gurobi.com/documentation/10.0/refman/dealing_with_big_m_constra.html)**
>
> Dealing with big-M constraints

As suggested at the bottom of the last article, you could also use indicator constraints:

```julia
using JuMP, Gurobi
model = Model(Gurobi.Optimizer)
@variable(model, q_out)
@variable(model, eff)
@variable(model, z[1:3], Bin)
@constraint(model, sum(z) == 1)
@constraint(model, z[1] --> {q_out >= 9})
@constraint(model, z[2] --> {q_out >= 7})
@constraint(model, z[2] --> {q_out <= 9})
@constraint(model, z[3] --> {q_out <= 7})
@constraint(model, eff == 0.75 * z[1] + 1.0 * z[2] + 0.5 * z[3])

```

(I haven’t tested this code, so there might be typos, etc.)

---

<div class="post-metadata">

### Author: ![Fabian1](https://avatars.discourse-cdn.com/v4/letter/f/eada6e/32.png) [@Fabian1](https://discourse.julialang.org/u/Fabian1)
#### Post date: [October 25, 2023, 6:53am UTC](https://discourse.julialang.org/t/variant-of-an-elseif-loop-with-binary-values/105348/4 "2023-10-25T06:53:19Z")

</div>

Hi Odow,

thank you for your answer! It was very helpful!
