How to implement a conditional over an exogenous parameter in a non linear constraint in JuMP?

I need to add a condition over an exogenous parameter. These seems not to work:

@NLconstraint(m, myConstraint[i in 1:N],   x[i]   == (myExogenousParameter[i] == 0.0 ?  0.0 : expression[i])  )

I found then this that seems to work, but I wonder if it is the correct approach:

parCheck(nzpar,expr) = (nzpar == 0 ? 0.0 : return expr)
register(m, :parCheck, 2, parCheck, autodiff=true)

@NLconstraint(m, myConstraint[i in 1:N],   x[i]   == parCheck(myExogenousParameter[i],expression[i])  )

How about using ifelse?

See How to implement a piece-wise defined function in JuMP - #4 by odow

1 Like