Extracting the coefficient of a nonlinear term in a JuMP model

For a generic nonlinear constraint, is there a way for me to extract the coefficient of a nonlinear term (e.g., multilinear terms)?

e.g.,

model = Model()
@variable(model, x[1:3])
@constraint(model, (x[1] - (((3* ((x[2] ^ 2.0) + (x[3] ^ 2.0))))))<=0)

In above model, I wish to extract the coefficient of x[3]^2, which will be -3. I am not building the model myself, but rather reading it from an .nlp file, so even though the constraint can written with less parentheses, when I read in the model, the constraint will have a form like above.

The type of constraint is “ConstraintRef{Model, MathOptInterface.ConstraintIndex{MathOptInterface.ScalarNonlinearFunction, MathOptInterface.LessThan{Float64}}, ScalarShape}”.

Hi @handahye, welcome to the forum :smile:

The simple answer is “no”.

If you grab the constraint function:

model = Model()
@variable(model, x[1:3])
c = @constraint(model, (x[1] - (((3* ((x[2] ^ 2.0) + (x[3] ^ 2.0)))))) <= 0)
o = constraint_object(c)
o.func

You can play around to see what the fields are: Nonlinear Modeling · JuMP

julia> o.func.args[1].args[2].args
2-element Vector{Any}:
 3.0
  (x[2] ^ 2.0) + (x[3] ^ 2.0)

I don’t know what an .nlp file is, but if you had written the constraint as the quadratic:

c_quad = @constraint(model, x[1] - 3 * (x[2]^2 + x[3]^2) <= 0)
c_quad_object = constraint_object(c_quad)
coefficient(c_quad_object.func, x[2], x[2])

this would work.

We’re working on some features to automatically convert nonlinear constraints to quadratic, etc, that might help in a future release of JuMP: [Nonlinear] add SymbolicAD submodule by odow · Pull Request #2624 · jump-dev/MathOptInterface.jl · GitHub