# Getting constraint matrix from gurobi

**URL:** <https://discourse.julialang.org/t/getting-constraint-matrix-from-gurobi/79107>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [April 6, 2022, 3:36pm UTC](https://discourse.julialang.org/t/getting-constraint-matrix-from-gurobi/79107 "2022-04-06T15:36:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![shsh](https://avatars.discourse-cdn.com/v4/letter/s/5daacb/32.png) [@shsh](https://discourse.julialang.org/u/shsh)\
**Post date:** [April 6, 2022, 3:36pm UTC](https://discourse.julialang.org/t/getting-constraint-matrix-from-gurobi/79107/1 "2022-04-06T15:36:44Z")

</div>

Is there an easy way to obtain the current constraint matrix from gurobi (or the original constraint matrix and inverse of the basis matrix) using MathOptInterface.  
I have been searching google and only found how to do it with the old MathBaseInterface, which does not work with the new versions.  
Would appreciate any pointers.

---

<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 6, 2022, 9:55pm UTC](https://discourse.julialang.org/t/getting-constraint-matrix-from-gurobi/79107/2 "2022-04-06T21:55:25Z")

</div>

Hi there,

(I’ve moved your question to the “Optimization (Mathematical)” section.)

Unfortunately there’s no easy way to do this using MathOptInterface, because we don’t represent the problem in matrix form.

You can use Gurobi’s C API to achieve this, although it’s a little complicated. For example, to call [GRBgetBasisHead](https://www.gurobi.com/documentation/9.5/refman/c_getbasishead.html), do:

```julia
using JuMP, Gurobi
model = direct_model(Gurobi.Optimizer())
# build model
grb = unsafe_backend(model)
num_constraints = ... # Number of rows in constraint matrix
basis = Vector{Cint}(undef, num_constraints)
GRBgetBasisHead(grb, basis)

```

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [April 7, 2022, 1:45am UTC](https://discourse.julialang.org/t/getting-constraint-matrix-from-gurobi/79107/3 "2022-04-07T01:45:21Z")

</div>

There is a “private” function `_standard_form_matrix` (i.e. shouldn’t be depended on not to change) within JuMP that may get you where you need to go:

```julia
using JuMP
model = Model();
@variable(model, x[1:2], lower_bound=0)
@objective(model, Min, -x[1] - x[2])
cons = @constraints(model, begin
                -5x[1] + 11x[2] <= 22
                 4x[1] - 6x[2] <= 8
end)
print(model)

s = JuMP._standard_form_matrix(model)
keys(s)
# = (:columns, :lower, :upper, :A, :bounds, :constraints)
# Here is the docstring:
r"""
    _standard_form_matrix(model::Model)

Given a problem:

    r_l <= Ax <= r_u
    c_l <= x <= c_u

Return the standard form:

           [A -I] [x, y] = 0
    [c_l, r_l] <= [x, y] <= [c_u, r_u]

`columns` maps the variable references to column indices.
"""
A = s.A
s.lower
s.upper
# If n is the number of variables in the original problem
# and m is the number of affine constraints in the problem. Then
m, p = size(s.A)
# where p == n + m.
n = p - m
# Hence, we can recover the "original" constraint matrix as
cA = A[:,1:n]
# and the "identity" portion as
cI = A[:,(n+1):end]

```

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [April 7, 2022, 2:01am UTC](https://discourse.julialang.org/t/getting-constraint-matrix-from-gurobi/79107/4 "2022-04-07T02:01:22Z")

</div>

If you want a Gurobi + MathOptInterface level example, let me know, it’s a bit long to post here.

---

<div class="post-metadata">

**Author:** ![shsh](https://avatars.discourse-cdn.com/v4/letter/s/5daacb/32.png) [@shsh](https://discourse.julialang.org/u/shsh)\
**Post date:** [April 12, 2022, 10:17am UTC](https://discourse.julialang.org/t/getting-constraint-matrix-from-gurobi/79107/5 "2022-04-12T10:17:05Z")

</div>

Thank you!  
Is there a way to get the mapping between the indexes in Gurobi model and JuMP variables anywhere?

---

<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, 2022, 7:58pm UTC](https://discourse.julialang.org/t/getting-constraint-matrix-from-gurobi/79107/6 "2022-04-12T19:58:41Z")

</div>

You can use `Gurobi.column(grb, x[1])` to retrieve the column.
