# Conditional NLconstraint, Couenne

**URL:** <https://discourse.julialang.org/t/conditional-nlconstraint-couenne/26801>\
**Category:** Optimization (Mathematical)\
**Created:** [July 25, 2019, 5:00pm UTC](https://discourse.julialang.org/t/conditional-nlconstraint-couenne/26801 "2019-07-25T17:00:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raminel](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@Raminel](https://discourse.julialang.org/u/Raminel)\
**Post date:** [July 25, 2019, 5:00pm UTC](https://discourse.julialang.org/t/conditional-nlconstraint-couenne/26801/1 "2019-07-25T17:00:09Z")

</div>

Hi all,

I am trying to solve a MINLP problem with Couenne. I have difficulty implementing an NLconstraint of this form: if x[i,j] == 0, y[i,j] == 0 else y[i,j] = 1/x[i,j], I have checked the Syntax notes and I still cannot get it right. If I try to use isapprox(x[i,j], 0, atol=1e-8), I get an error:  
LoadError: MethodError: no method matching isapprox(::VariableRef, ::Int64; atol=1.0e-8)

here is a simplified version of my code excluding many other parts that makes it a MINLP problem:

using JuMP  
using AmplNLWriter

```
m = Model(with_optimizer(AmplNLWriter.Optimizer, "path to couenne.exe"))

    D_st = [0, 0.03, 0.06, 0.2, 0.3, 0.5]
    Q = [0.02; 0.05; 0.07]
    NP = size(Q,1)
    ND = size(D_st,1)
    min_velocity = 0.6
    max_velocity = 2

    @variable(m, D[1:NP, 1:NP] >= 0)
    @variable(m, D_idx[1:NP, 1:NP, 1:ND] >= 0, Bin)
    @variable(m, x[1:NP, 1:NP] >= 0)

    @constraint(m, 1 .>= sum(x, dims=2))

    @constraint(m, [i = 1:NP, j = 1:NP], D[i,j] == sum(D_st[n]*D_idx[i,j,n] for n = 1:ND))

    @NLexpression(m, v_aux[i = 1:NP, j = 1:NP], 4*x[i,j]*Q[i]/(pi*D[i,j]^2))
    @NLexpression(m, QF[i = 1:NP], Q[i] - sum(x[j,i]*Q[j] for j = 1:NP))

    @expression(m, v[i = 1:NP, j = 1:NP], ifelse(isapprox(D[i,j],0, atol=1e-8),0,1)*v_aux[i,j])

    @NLconstraint(m, [i = 1:NP, j = 1:NP], min_velocity <= v[i,j])
    @NLconstraint(m, [i = 1:NP, j = 1:NP], max_velocity >= v[i,j])

    @NLconstraint(m, sum(QF[i] for i =1:NP)/sum(Q[i] for i = 1:NP) == 0.8)

    JuMP.optimize!(m)

    x = JuMP.value.(x);
    D = JuMP.value.(D)
    v = JuMP.value.(v)

    println("x = ", x)
    println("D = ", D)
    println("v = ", v)

```

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [July 25, 2019, 5:24pm UTC](https://discourse.julialang.org/t/conditional-nlconstraint-couenne/26801/2 "2019-07-25T17:24:05Z")

</div>

A standard way to implement this kind of constraint is using a big-M formulation. Are you familiar with that?

---

<div class="post-metadata">

**Author:** ![Raminel](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@Raminel](https://discourse.julialang.org/u/Raminel)\
**Post date:** [July 25, 2019, 5:32pm UTC](https://discourse.julialang.org/t/conditional-nlconstraint-couenne/26801/3 "2019-07-25T17:32:00Z")

</div>

No I am not, but I can look it up. Thanks for the tip!

---

<div class="post-metadata">

**Author:** ![Henderson](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@Henderson](https://discourse.julialang.org/u/Henderson)\
**Post date:** [May 11, 2020, 4:43am UTC](https://discourse.julialang.org/t/conditional-nlconstraint-couenne/26801/4 "2020-05-11T04:43:13Z")

</div>

@tkoolen sorry, i’m not familiar with that. Could you help me understand (or recommend a source) how the big m formulation could be applied in this case? another question: is this a requirement of the input format of the couenne optimization problem?

---

<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:** [May 11, 2020, 4:50pm UTC](https://discourse.julialang.org/t/conditional-nlconstraint-couenne/26801/5 "2020-05-11T16:50:26Z")

</div>

> **[Documentation - Gurobi Optimization](https://www.gurobi.com/documentation/9.0/refman/num_dealing_with_big_m_con.html)**
>
> Est. reading time: 1 minute

> **[Big M: good in practice, bad in theory, and ugly numerically!](https://thiagoserra.com/2017/06/15/big-m-good-in-practice-bad-in-theory-and-ugly-numerically/)**
>
> If you have seen or modeled enough optimization problems, you probably came across or felt the need for some big Ms: very large coefficients used as a proxy for infinity. Most often, a big M is use…
