NLconstraint: equality, absolute value



I have a nonlinear, nonconvex equality constraint, and I only care about its magnitude. I saw this thread but wasn’t sure how to best handle the constraint in the nonconvex case. For example, I want something like:

m = Model(solver=IpoptSolver())
@variable(m, -pi/2 <= x <= pi/2)
@objective(m, Min, x)
@NLconstraint(m, absval_constraint, abs(sin(x)) == 0.5)

I tried adding constraints like:

@NLconstraint(m, c1, sin(x) <= 0.5)
@NLconstraint(m, c2, sin(x) >= -0.5)
@NLconstraint(m, c3, sin(x) >= 0.5)
@NLconstraint(m, c4, sin(x) <= -0.5)

but it makes sense that when solving, it’s infeasible unless I start from x = -pi/6 or x = pi/6.

I only have one constraint like this, so I could solve the problem twice separately with equality constraints (1) sin(x) == 0.5 and (2) sin(x) == -0.5, but is this what’s typically done? Is there a better way?


The issue you linked to is more for linear problems. When I tried, Ipopt found the correct answer for your original problem. What’s the issue?

Also, I don’t think your constraints are doing that you think they’re doing. In particular, c3 and c4 make the problem infeasible regardless of x.