How to introduce an if sentence with optimization variables

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

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:

@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.

Have a look a the concept of an indicator constraint. Alternatively, 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.

4 Likes

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

The alternative is to use ifelse

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.

3 Likes