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

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

``````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?

Regards

Jonathan

See this related discourse thread: Solve minimization problem where constraint is the system of linear inequation with MathOptInterface efficiently

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

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 c1x1+c2x2

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

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

See 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.

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:

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.

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

``````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)
``````
1 Like