# How to introduce an if sentence with optimization variables

**URL:** https://discourse.julialang.org/t/how-to-introduce-an-if-sentence-with-optimization-variables/102130
**Category:** Optimization (Mathematical)
**Tags:** question, jump, nonlinear-optimizati
**Created:** [July 26, 2023, 7:09pm UTC](https://discourse.julialang.org/t/how-to-introduce-an-if-sentence-with-optimization-variables/102130 "2023-07-26T19:09:20Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![PatrickeTownsend](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/patricketownsend/32/50028_2.png) [@PatrickeTownsend](https://discourse.julialang.org/u/PatrickeTownsend)
#### Post date: [July 26, 2023, 7:09pm UTC](https://discourse.julialang.org/t/how-to-introduce-an-if-sentence-with-optimization-variables/102130/1 "2023-07-26T19:09:20Z")

</div>

Hello again,  
For and electric propulsion system optimization, i’m trying to impose zero thrust during eclipse phases, I have this function created:

```julia
function Step_eclipse(j,r...,r_par...)
    Re = 6378
    if r_par[j] + sqrt(r^2 - Re^2) ≤ 0
        return s=0
    else
        return s=1
    end
end
register(lowThrust, :Step_eclipse, 3, Step_eclipse; autodiff=true)
@NLconstraint(lowThrust, T[j]== Tmax*Step_eclipse(j,r...,r_par...))

```

where Tmax is a constant. But it is constantly dropping error in register and the user defined function definition.  
‘r\_par’ and r are defined as:

```julia
@NLexpressions(lowThrust,begin
    rx[j=1:n], (r[j]/s[j])*sqrt(μ/p[j])*(cos(L[j]) + (alpha[j]^2)*cos(L[j])+2*h[j]*k[j]*sin(L[j]))
    ry[j=1:n], (r[j]/s[j])*sqrt(μ/p[j])*(sin(L[j]) - (alpha[j]^2)*sin(L[j])+2*h[j]*k[j]*cos(L[j]))
    rz[j=1:n], (2*r[j]/s[j])*(h[j]*sin(L[j]) - k[j]*cos(L[j]))
end)
@NLexpressions(lowThrust,begin
    r_par[j=1:n], rx[j]*r_sun[1] + ry[j]*r_sun[2] + rz*[j]*r_sun[3]
end)
@NLexpressions(lowThrust,begin
    q[j=1:n], 1 + f[j]*cos(L[j]) + g[j]*sin(L[j])
    r[j=1:n], p[j]/q[j]
    alpha[j=1:n], 1 - h[j]^2 - k[j]^2
    s[j=1:n], 1 + (h[j])^2 + (k[j])^2
end)

```

P,f,g,h,k, L are the optimization variables and alpha is a NLexpression.

I don’t know how to implement it correctly, if the full code is required please let me know, i’m a little bit desperate.

---

<div class="post-metadata">

### Author: ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)
#### Post date: [July 27, 2023, 11:25am UTC](https://discourse.julialang.org/t/how-to-introduce-an-if-sentence-with-optimization-variables/102130/2 "2023-07-27T11:25:58Z")

</div>

Have a look a the concept of an [indicator constraint](https://jump.dev/JuMP.jl/stable/manual/constraints/#Indicator-constraints). Alternatively, [complementarity constraints](https://jump.dev/JuMP.jl/stable/manual/constraints/#Complementarity-constraints).

By the way, you would increase the probability of getting a helpful answer by providing a complete code for a minimum (albeit not-yet-)working example.

---

<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: [July 27, 2023, 1:15pm UTC](https://discourse.julialang.org/t/how-to-introduce-an-if-sentence-with-optimization-variables/102130/3 "2023-07-27T13:15:45Z")

</div>

You might find that you can’t find a nonlinear solver that also supports indicator constraints.

The alternative is to use `ifelse`

```julia
Re = 6378
@NLconstraint(lowThrust, T[j] == Tmax*ifelse(r_par[j] + sqrt(r^2 - Re^2) ≤ 0, 0, 1))

```

But it’s hard to test that this works without a reproducible example.
