# Recovering different form from JuMP model

**URL:** <https://discourse.julialang.org/t/recovering-different-form-from-jump-model/97401>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [April 12, 2023, 6:20pm UTC](https://discourse.julialang.org/t/recovering-different-form-from-jump-model/97401 "2023-04-12T18:20:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shakedregev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shakedregev/32/45821_2.png) [@shakedregev](https://discourse.julialang.org/u/shakedregev)\
**Post date:** [April 12, 2023, 6:20pm UTC](https://discourse.julialang.org/t/recovering-different-form-from-jump-model/97401/1 "2023-04-12T18:20:06Z")

</div>

If I have a model that I know represents the LP

```julia
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)[3]` (because there are no simple constraints on `x`) and I get `Ahat=._standard_form_matrix(model)[1]`. Then I just remove the columns corresponding to `A=Ahat[1:end-length(b)]`

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [April 12, 2023, 7:04pm UTC](https://discourse.julialang.org/t/recovering-different-form-from-jump-model/97401/2 "2023-04-12T19:04:35Z")

</div>

There’s no simple way.

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

> [@Connecting a simple first-order solver to solve standard form linear program to JuMP](https://discourse.julialang.org/t/connecting-a-simple-first-order-solver-to-solve-standard-form-linear-program-to-jump/95694/4):
>
> Try this: module SimplePDHG import MathOptInterface as MOI import SparseArrays """ solve\_pdhg( A::SparseArrays.SparseMatrixCSC{Float64,Int}, b::Vector{Float64}, c::Vector{Float64}, )::Tuple{MOI.TerminationStatusCode,Vector{Float64}} """ function solve\_pdhg( A::SparseArrays.SparseMatrixCSC{Float64,Int}, b::Vector{Float64}, c::Vector{Float64}, )::Tuple{MOI.TerminationStatusCode,Vector{Float64}} x = fill(NaN, size(A, 2)) return MOI.OTHER\_ERROR,…

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: [Suggestions for documentation improvements · Issue #2348 · jump-dev/JuMP.jl · GitHub](https://github.com/jump-dev/JuMP.jl/issues/2348#issuecomment-1458922737)

---

<div class="post-metadata">

**Author:** ![A\_M](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/a_m/32/46038_2.png) [@A\_M](https://discourse.julialang.org/u/A_M)\
**Post date:** [April 12, 2023, 7:21pm UTC](https://discourse.julialang.org/t/recovering-different-form-from-jump-model/97401/3 "2023-04-12T19:21:42Z")

</div>

I guess this may help. @odow

```julia
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))
        if haskey(objective_function.terms, var)
            C[i] = objective_function.terms[var]
        end
    end

    return A, B, C
end

```

---

<div class="post-metadata">

**Author:** ![A\_M](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/a_m/32/46038_2.png) [@A\_M](https://discourse.julialang.org/u/A_M)\
**Post date:** [April 12, 2023, 11:33pm UTC](https://discourse.julialang.org/t/recovering-different-form-from-jump-model/97401/4 "2023-04-12T23:33:31Z")

</div>

To cover QP and QCP problems as well:

```julia
function extract_matrices(model::Model)
    num_vars = length(all_variables(model))
    constraints = all_constraints(model, JuMP.AffExpr, MOI.LessThan{Float64})
    quad_constraints = all_constraints(model, JuMP.GenericQuadExpr{Float64, JuMP.VariableRef}, MOI.LessThan{Float64})
    num_cons = length(constraints)
    num_quad_cons = length(quad_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}}()
    b_constraints = spzeros(num_quad_cons)

    # 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

    # Extract quadratic constraints
    for (i, con) in enumerate(quad_constraints)
        quad_constraint_function = JuMP.constraint_object(con).func
        quad_constraint = spzeros(num_vars, num_vars)
        b_constraints[i] = JuMP.constraint_object(con).set.upper - quad_constraint_function.aff.constant
        for (quad_var, coeff) in quad_constraint_function.terms
            quad_constraint[index(getfield(quad_var, 1)).value, index(getfield(quad_var, 2)).value] = coeff
        end
        push!(Q_constraints, quad_constraint)
    end

    # Extract the cost vector (C) and the quadratic matrix (Q)
    objective_function = JuMP.objective_function(model)
    if isa(objective_function, JuMP.QuadExpr)
        for var in keys(objective_function.aff.terms)
            C[index(var).value] = objective_function.aff.terms[var]
        end
        for (quad_var, coeff) in objective_function.terms
            Q[index(getfield(quad_var, 1)).value, index(getfield(quad_var, 2)).value] = coeff
            Q[index(getfield(quad_var, 2)).value, index(getfield(quad_var, 1)).value] = coeff
        end
    else
        for var in keys(objective_function.terms)
            C[index(var).value] = objective_function.terms[var]
        end
    end

    return A, B, C, Q, Q_constraints, b_constraints
end

```
