# Recovering different form from JuMP model

If I have a model that I know represents the LP

``````min c'x
s.t. Ax<=b
``````

how can I extract `(A, b, c)` from the model?
I know `JuMP._nlp_objective_function` will give me the objective function, but I actually need the vector `c` because I later use it explicitly when calculating gradients.

I think I know how I extract `(A, b)` in this case, but just to confirm: `b= ._standard_form_matrix(model)` (because there are no simple constraints on `x`) and I get `Ahat=._standard_form_matrix(model)`. Then I just remove the columns corresponding to `A=Ahat[1:end-length(b)]`

There’s no simple way.

This question has some code to help. The `solve_pdhg` function provides `A, b, c` (for `Ax = b`):

You shouldn’t rely on th `JuMP._standard_form_matrix` function. Function beginning with `_` are private and may change or be delete at any point.

This questio keeps coming up, so I should add an answer to the documentation: https://github.com/jump-dev/JuMP.jl/issues/2348#issuecomment-1458922737

1 Like

I guess this may help. @odow

``````using JuMP
import MathOptInterface

const MOI = MathOptInterface

function extract_matrices(model::JuMP.Model)
num_vars = length(all_variables(model))
constraints = all_constraints(model, JuMP.AffExpr, MOI.LessThan{Float64})
num_cons = length(constraints)

A = zeros(num_cons, num_vars)
B = zeros(num_cons)
C = zeros(num_vars)

# Extract the constraint matrix (A) and the right-hand side vector (B)
for (i, con) in enumerate(constraints)
constraint_object = JuMP.constraint_object(con)
constraint_function = constraint_object.func
for (var, coeff) in constraint_function.terms
A[i, index(var).value] = coeff
end
B[i] = constraint_object.set.upper
end

# Extract the cost vector (C)
objective_function = JuMP.objective_function(model)
for (i, var) in enumerate(all_variables(model))
C[i] = objective_function.terms[var]
end
end

return A, B, C
end
``````
1 Like

To cover QP and QCP problems as well:

``````function extract_matrices(model::Model)
num_vars = length(all_variables(model))
constraints = all_constraints(model, JuMP.AffExpr, MOI.LessThan{Float64})
num_cons = length(constraints)

A = spzeros(num_cons, num_vars)
B = spzeros(num_cons)
C = spzeros(num_vars)
Q = spzeros(num_vars, num_vars)
Q_constraints = Vector{SparseMatrixCSC{Float64, Int}}()

# Extract the constraint matrix (A) and the right-hand side vector (B)
for (i, con) in enumerate(constraints)
constraint_function = JuMP.constraint_object(con).func
for var in keys(constraint_function.terms)
A[i, index(var).value] = constraint_function.terms[var]
end
B[i] = JuMP.constraint_object(con).set.upper
end

end
end

# Extract the cost vector (C) and the quadratic matrix (Q)
objective_function = JuMP.objective_function(model)
for var in keys(objective_function.aff.terms)
C[index(var).value] = objective_function.aff.terms[var]
end