JuMP--how to take the gradient of quadratic constraints?

I need to get the constraint Jacobian for a model which contains linear, quadratic, and nonlinear constraints. For the nonlinear constraints, I can use the NLPEvaluator with function eval_constraint_jacobian. For the linear constraints, my current strategy is to get the constraint coefficients as follows:

linear_constraint_references = all_constraints(model, AffExpr, MOI.EqualTo{Float64})
x = all_variables(model)
linear_jacobian = zeros(length(linear_constraint_references), length(x))
for r = 1:length(linear_constraint_references)
    for v = 1:length(x)
        linear_jacobian[r,v] = normalized_coefficient(linear_constraint_references[r], x[v])
    end
end

However, I’m getting stuck on the quadratic constraints. I can get the coefficient for any linear terms in the quadratic expression using the normalized_coefficient method, but I don’t know how to query the quadratic constraint to find the coefficient of any quadratic terms. Is there a way to do this? Or is there another method to take the gradient of the quadratic constraint?

You can do something similar, and get the coefficient of a quadratic expression: Expressions · JuMP

func_set = constraint_object(r)
c = coefficient(func_set.func, x, y)  # quadratic coefficient of x * y
c = coefficient(func_set.func, x)     # affine coefficient of x

See also Computing Hessians · JuMP

1 Like

Got it, that’s exactly what I needed! Thank you.

1 Like