# Getting constraint matrix and bound from JuMP model (JuMP 0.19)

**URL:** <https://discourse.julialang.org/t/getting-constraint-matrix-and-bound-from-jump-model-jump-0-19/24197>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [May 14, 2019, 8:07am UTC](https://discourse.julialang.org/t/getting-constraint-matrix-and-bound-from-jump-model-jump-0-19/24197 "2019-05-14T08:07:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonathan\_Droxler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonathan_droxler/32/9451_2.png) [@Jonathan\_Droxler](https://discourse.julialang.org/u/Jonathan_Droxler)\
**Post date:** [May 14, 2019, 8:07am UTC](https://discourse.julialang.org/t/getting-constraint-matrix-and-bound-from-jump-model-jump-0-19/24197/1 "2019-05-14T08:07:21Z")

</div>

Hi everyone,

I just change to JuMP 0.19 and I get some problem with my code.

I construct a big JuMP model with a lot of specific variables and constraints, and I want to extract constraint matrix and vectors from the model constructed with JuMP:

Min c\*x  
s.t. l \<= Ax \<= u  
lb \<= x \<= ub

I write, 2 years ago the following piece of code, I don’t know if it was optimal, but it worked

```julia
JuMP.build(model)

c = MathProgBase.getobj(JuMP.internalmodel(model))
A = MathProgBase.getconstrmatrix(JuMP.internalmodel(model))
lb = MathProgBase.getvarLB(JuMP.internalmodel(model))
ub = MathProgBase.getvarUB(JuMP.internalmodel(model))
l = MathProgBase.getconstrLB(JuMP.internalmodel(model))
u = MathProgBase.getconstrUB(JuMP.internalmodel(model))

```

But know, with JuMP 0.19 and MathOptInterface, it doesn’t work anymore and I didn’t find anything in documentations or any topics related. Can anybody indicate me where to find information or what is the best thing to do?

Thank you in advance.

Regards

Jonathan

---

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [May 14, 2019, 12:49pm UTC](https://discourse.julialang.org/t/getting-constraint-matrix-and-bound-from-jump-model-jump-0-19/24197/2 "2019-05-14T12:49:24Z")

</div>

See this related discourse thread: [Solve minimization problem where constraint is the system of linear inequation with MathOptInterface efficiently](https://discourse.julialang.org/t/solve-minimization-problem-where-constraint-is-the-system-of-linear-inequation-with-mathoptinterface-efficiently/23571)

---

<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:** [May 14, 2019, 2:07pm UTC](https://discourse.julialang.org/t/getting-constraint-matrix-and-bound-from-jump-model-jump-0-19/24197/3 "2019-05-14T14:07:57Z")

</div>

The bigger question is: why do you want to do this?

---

<div class="post-metadata">

**Author:** ![Jonathan\_Droxler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonathan_droxler/32/9451_2.png) [@Jonathan\_Droxler](https://discourse.julialang.org/u/Jonathan_Droxler)\
**Post date:** [May 14, 2019, 2:52pm UTC](https://discourse.julialang.org/t/getting-constraint-matrix-and-bound-from-jump-model-jump-0-19/24197/4 "2019-05-14T14:52:33Z")

</div>

My problem is not the same.

I don’t know the matrix A and vector l,u,lb,ub and c. I use JuMP to create all my variables and constraints (I describe with them a unit commitment problem for hydro power plants) , then I used the code I wrote previously to get these information (A,…etc). The problem is to complicated to construct “by-hand” constraints matrices.

I need this because I construct these matrices A for several power plants with the same code (with different parameters) and then link all power plant by using all these A matrices and vectors.

If a have 2 power plants, my problem will become

Min c1_x1+c2_x2

s.t. [l1 l2] \<= [A1 0  
0 A2] \* [x1 x2] \<= [u1 u2]  
+ some constraints that link the two power plant depending on x1 and x2

If there exist a way to merge such optimization problem directly with JuMP and MathOptInterface I will be happy.

Thank you

Regards

---

<div class="post-metadata">

**Author:** ![Jonathan\_Droxler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonathan_droxler/32/9451_2.png) [@Jonathan\_Droxler](https://discourse.julialang.org/u/Jonathan_Droxler)\
**Post date:** [May 14, 2019, 2:53pm UTC](https://discourse.julialang.org/t/getting-constraint-matrix-and-bound-from-jump-model-jump-0-19/24197/5 "2019-05-14T14:53:29Z")

</div>

Sorry for ugly mathematical formulation, don’t find the way to include LateX formulation.

---

<div class="post-metadata">

**Author:** ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)\
**Post date:** [May 14, 2019, 3:39pm UTC](https://discourse.julialang.org/t/getting-constraint-matrix-and-bound-from-jump-model-jump-0-19/24197/6 "2019-05-14T15:39:35Z")

</div>

See [Constraints · JuMP](http://www.juliaopt.org/JuMP.jl/v0.19.1/constraints/#Accessing-constraints-from-a-model-1). It requires a bit more code than before because JuMP 0.19 supports a lot of new constraints and has no specific concept of a constraint matrix anymore.

---

<div class="post-metadata">

**Author:** ![joaquimg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joaquimg/32/223_2.png) [@joaquimg](https://discourse.julialang.org/u/joaquimg)\
**Post date:** [May 14, 2019, 4:39pm UTC](https://discourse.julialang.org/t/getting-constraint-matrix-and-bound-from-jump-model-jump-0-19/24197/7 "2019-05-14T16:39:46Z")

</div>

You should, think carefully about what you what and whats the best way to achieve that, sometimes getting the matrices is a simple but not optimal method. I recommend thinking twice before extracting those matrices.

In any case, I started the following repo:  
[https://github.com/joaquimg/MatrixOptInterface.jl](https://github.com/joaquimg/MatrixOptInterface.jl)

mostly for teaching purposes.

It not tested at all, but its one (and not the only) way to get matrices from LP´s expressed in JuMP or MOI.

---

<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:** [May 14, 2019, 6:20pm UTC](https://discourse.julialang.org/t/getting-constraint-matrix-and-bound-from-jump-model-jump-0-19/24197/8 "2019-05-14T18:20:28Z")

</div>

Can’t you just write this directly in one go? With the plant as an index in variables and constraints?

```nohighlight
P = 3 # Number of plants
U = [2, 2, 4] # Number of units at each plant

model = Model()
@variable(model, unit_commit[p=1:P, u=1:U[p]], Bin)
@objective(model, Min, sum(
    (p + u) * unit_commit[p, u] for p=1:P for u=1:U[p]))
@constraint(model, [p=1:P, i=2:P[p]], unit_commit[p, i-1] <= unit_commit[p, i])
@constraint(model, sum(unit_commit) >= 4)

```
