Defining variable model as x != 0 in JuMP

I am trying to run my first example of optimization using JuMP.

I have a problem with the definition of the values of x. I want to do x != 0 but it gives me an error. It only works for x >= 0 or x <= 0.

using JuMP
import Ipopt
model = Model(Ipopt.Optimizer)
@variable(model, y >= 0)
@variable(model, x != 0) # x >= 0 works
@NLobjective(model, Max, (x^2)*y)
@NLconstraint(model, x^2 + y^2 == 1)
print(model)
optimize!(model)
@show value(x) # sqrt(2/3)
@show value(y) # sqrt(1/3)

We may be able to model your problem as a mixed-integer problem. Can you be more specific? Why do you need x \neq 0?

1 Like

It’s a weird thing to ask for a continuous variable to be not equal to a value. If x == 0 is optimal and you forbid it with a constraint, then 0.00000000000000001 is probably the next best value.

It is not possible in JuMP just because it’s not a thing one does in continuous optimization.
You’re not getting x==0 as a solution anyway.

1 Like

Well, i’s possible that I am not thinking right. I know that the results are two points P1(sqrt(2/3); sqrt(1/3)) and P2(-sqrt(2/3); sqrt(1/3)). If I make x >= 0 I only have as a result P1 and if I do x <= 0 I have P2. If I don’t give any restriction to x I have a wrong result. So I thought That the solution could be x != 0.

You don’t have to add a bound constraint. Just leave it as a free variable.

What do you mean by wrong result?

I tried something like @variable(model, x) but the results are wrong: (0;0).

You need to check the problem status
Solutions · JuMP?

Your problem is nonconvex, so ipopt struggled to solve it.

Provide a different starting point

https://jump.dev/JuMP.jl/stable/manual/variables/#Start-values

Well, I tried several starting points 0.2, 1, 2, 10 but I don’t get the negative value for x. I tried for ex. @variable(model, x, start = 10).

You’ll have to set a negative starting point close to the solution. You’ll also have to provide a starting point for y that makes the solution feasible.

I tried

@variable(model, y >= 0, start = 1)
@variable(model, x, start = -1)

But I got only one point:

julia> @show value(x) # sqrt(2/3)
value(x) = -0.8164965800409512
-0.8164965800409512

julia> @show value(y) # sqrt(1/3)
value(y) = 0.5773502704448548
0.5773502704448548

Ipopt only finds a single solution. It does not find all solutions.

1 Like

Thanks. Can you suggest an alternative solver for this problem?

To find all solutions? None of the JuMP solvers support this.

Why do you need it?

I am only studying a simple example, which I know the solutions previously. Is there an alternative package in Julia that gives all solutions?

I tried Wolfram Alpha with maximize x^2*y on x^2+y^2=1 and I got immediately the two solution points!

Try: https://github.com/JuliaIntervals/IntervalConstraintProgramming.jl

In general, mathematical optimization is the wrong tool to compute all solutions. Almost all solvers focus on providing an optimal solution, rather than all optimal solutions.

2 Likes

But Wolfram Alpha is based on Mathematica… Maybe the reason is that the solution is symbolic, not numerical.

Gurobi can find all solutions, I believe for quadratic problems as well, so need to split the higher order terms

1 Like