How to do division in a linear model with JuMP

There is a constraint “P=Q./R”, where P and Q are vector{variableRef}, Q is array{Float64,2}
JuMP model is as follow:

using JuMP,Gurobi,LinearAlgebra
model=Model(Gurobi.Optimizer)
Q=[1,2,3]                                                      
@variable(model, P[1:3])
@variable(model, R[1:3])
@expression(model, expr1[i=1:n],Q[i]/R[i])
@constraint(model, con1[i=1:3], P[i]== expr1[i])

Error: / is not defined?!

How could I solve it using @expression not @NLexpression and with Gurobi solver?

Your constraint is in fact nonlinear, so you should use @NLconstraint as suggested by the error message.

4 Likes

If the constraint@constraint(model, con1[i=1:3], P[i]== expr1[i]) is written as:

@constraint(model, con1[i=1:3], R[i]*P[i]== Q[i])
@constraint(model, con2[i=1:3],R[i]>0)
@constraint(model, con3[i=1:3],R[i]<0)

May equivalent to the original constraint?

You can use R[i] * P[i]. This is still nonlinear, but the special case of quadratic expressions is supported by @constraint.

The other two constraints using < and > contradict each other and can not be satisfied simultaneously. Further, strict inequalities are not supported by solvers such as Gurobi.

4 Likes

Ok. Thank you for you answer!

I still have some question. For example

using JuMP, Gurobi
m=Model(Gurobi.Optimizer)

@variables m begin
    fp
    fq
    a
    v
end
@NLconstraint(m, exp(fp) <= 40)

@NLconstraint(m, exp(1/v) <= 40)

@constraint(m,fq+a <= 40)
optimize!(m)

Can’t set optimizer_attribute or other setting to solve the problem? I saw Gurobi’s official website that it can solve NLP.
Thank you for your help.

Gurobi doesn’t support arbitrary nonlinear. It is restricted to quadratic programs.

1 Like

OK, I see. I need to change the nonlinear model to a linear one.
Thank you for your answer again.

1 Like

You can also use a nonlinear optimizer like Ipopt (GitHub - jump-dev/Ipopt.jl: Julia interface to the Ipopt nonlinear solver).

1 Like

Nice! It is also a good method.