# How to get the variables/constraint names using MathProgBase?

**URL:** <https://discourse.julialang.org/t/how-to-get-the-variables-constraint-names-using-mathprogbase/40511>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [May 31, 2020, 3:23pm UTC](https://discourse.julialang.org/t/how-to-get-the-variables-constraint-names-using-mathprogbase/40511 "2020-05-31T15:23:43Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![sugyeong-jo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sugyeong-jo/32/15380_2.png) [@sugyeong-jo](https://discourse.julialang.org/u/sugyeong-jo)\
**Post date:** [May 31, 2020, 3:23pm UTC](https://discourse.julialang.org/t/how-to-get-the-variables-constraint-names-using-mathprogbase/40511/1 "2020-05-31T15:23:43Z")

</div>

Is there any method for getting the variables/constraint name not a just index from the MPS file format?  
I just want to make a dictionary for “variable name - index, constraint name - index”

```julia
model = MathProgBase.LinearQuadraticModel(ClpSolver())
MathProgBase.loadproblem!(model,"test.mps")
c = MathProgBase.getobj(model)
A = MathProgBase.getconstrmatrix(model)
nrow,ncol = size(A)
xlb = MathProgBase.getvarLB(model)
xub = MathProgBase.getvarUB(model)
l = MathProgBase.getconstrLB(model)
u = MathProgBase.getconstrUB(model)
t = MathProgBase.getvartype(model)

```

- `wirteLP` package is not available in Julia ver 1.0\<

or

I want to make a sparse matrix for constraint from the `JuMP.read_from_file("test.mps")`

---

<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 31, 2020, 7:44pm UTC](https://discourse.julialang.org/t/how-to-get-the-variables-constraint-names-using-mathprogbase/40511/2 "2020-05-31T19:44:43Z")

</div>

From the model returned by `read_from_file`, you can get the list of variables with [`all_variables`](http://www.juliaopt.org/JuMP.jl/v0.21.1/variables/#JuMP.all_variables) and then for each one you can get the name wirh [`name`](http://www.juliaopt.org/JuMP.jl/v0.21.1/variables/#JuMP.name-Tuple%7BVariableRef%7D).  
Then you can get the list of constraint types with [`list_of_constraint_types`](https://www.juliaopt.org/JuMP.jl/v0.21.1/constraints/#JuMP.list_of_constraint_types) and the list of constraints for given types with [`all_constraints`](https://www.juliaopt.org/JuMP.jl/v0.21.1/constraints/#JuMP.all_constraints). For each constraint, you can get the name with [`name`](https://www.juliaopt.org/JuMP.jl/v0.21.1/constraints/#JuMP.name-Tuple%7BConstraintRef%7BModel,#s313,Shape%7D%20where%20Shape%3C:AbstractShape%20where%20#s313%3C:MathOptInterface.ConstraintIndex%7D).

---

<div class="post-metadata">

**Author:** ![sugyeong-jo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sugyeong-jo/32/15380_2.png) [@sugyeong-jo](https://discourse.julialang.org/u/sugyeong-jo)\
**Post date:** [June 1, 2020, 1:37am UTC](https://discourse.julialang.org/t/how-to-get-the-variables-constraint-names-using-mathprogbase/40511/3 "2020-06-01T01:37:30Z")

</div>

Thank you for answering.

I already know the `read_from_file` and I have tried that…  
However, I couldn’t make a constraint sparse matrix.  
Exactly, I couldn’t find the way to make a list for each constraint as like this, [coefficient, variable].

```julia
m = read_from_file("example.mps")
optimizer=optimizer_with_attributes(Cbc.Optimizer, "maxSolutions" => 1, "logLevel "=>1, "seconds" => 300,"allowableGap "=>70)
set_optimizer(m, optimizer)

```

The type of constraint is followed

```julia
julia> list_of_constraint_types(m)
7-element Array{Tuple{DataType,DataType},1}:
 (GenericAffExpr{Float64,VariableRef}, MathOptInterface.EqualTo{Float64})    
 (GenericAffExpr{Float64,VariableRef}, MathOptInterface.GreaterThan{Float64})
 (GenericAffExpr{Float64,VariableRef}, MathOptInterface.LessThan{Float64})   
 (VariableRef, MathOptInterface.EqualTo{Float64})                            
 (VariableRef, MathOptInterface.Interval{Float64})                           
 (VariableRef, MathOptInterface.Integer)                                     
 (VariableRef, MathOptInterface.ZeroOne) 

```

If I get one of `MOI.LessThan{Float64}` constraint,

```julia
julia> less_than_constraints = all_constraints(m, GenericAffExpr{Float64,VariableRef}, MOI.LessThan{Float64})

julia>less_than_constraints[1]
HSBAL_CNYIT1701W_DEHAM1701E_004_OUTER : -1.0e6 HSBAL_CNYIT1701W_DEHAM1701E_004_OUTER + BSBAL_CNYIT1701W_DEHAM_004C1 + BSBAL_CNYIT1701W_DEHAM_004S1 + BSBAL_CNYIT1701W_DEHAM_004C2 ≤ 0.0

```

Here, I couldn’t extract the constraint name, coefficient and variable, sense, rhs.  
I want to make a sparse matrix like as

```julia
[HSBAL_CNYIT1701W_DEHAM1701E_004_OUTER, HSBAL_CNYIT1701W_DEHAM1701E_004_OUTER] = -1.0e6
[HSBAL_CNYIT1701W_DEHAM1701E_004_OUTER, BSBAL_CNYIT1701W_DEHAM_004C1] = 1.0
[HSBAL_CNYIT1701W_DEHAM1701E_004_OUTER, BSBAL_CNYIT1701W_DEHAM_004S1] = 1.0
[HSBAL_CNYIT1701W_DEHAM1701E_004_OUTER, BSBAL_CNYIT1701W_DEHAM_004C2] = 1.0

```

I want to handle the `ConstraintRef`

- 
  1. Extracting the constraint name

- 
  1. Extraicting the coefficient and variables

```julia
julia> typeof(less_than_constraints[1])
ConstraintRef{Model,MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64},MathOptInterface.LessThan{Float64}},ScalarShape}

julia>less_than_constraints[1]
HSBAL_CNYIT1701W_DEHAM1701E_004_OUTER : -1.0e6 HSBAL_CNYIT1701W_DEHAM1701E_004_OUTER + BSBAL_CNYIT1701W_DEHAM_004C1 + BSBAL_CNYIT1701W_DEHAM_004S1 + BSBAL_CNYIT1701W_DEHAM_004C2 ≤ 0.0

# Extracting the name
julia> name(less_than_constraints[1])
"HSBAL_CNYIT1701W_DEHAM1701E_004_OUTER"

# How can extract the variables and coefficient?
julia> constraint_object(less_than_constraints[1]).func
-1.0e6 HSBAL_CNYIT1701W_DEHAM1701E_004_OUTER + BSBAL_CNYIT1701W_DEHAM_004C1 + BSBAL_CNYIT1701W_DEHAM_004S1 + BSBAL_CNYIT1701W_DEHAM_004C2

julia> constraint_object(less_than_constraints[1]).func[1]
ERROR: MethodError: no method matching getindex(::GenericAffExpr{Float64,VariableRef}, ::Int64)
Stacktrace:
 [1] top-level scope at none:0

```

I think MathProgBase package is the better option for me…  
So I want to know the extract the “variable name” from the MathProgBase.

---

<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:** [June 1, 2020, 9:59am UTC](https://discourse.julialang.org/t/how-to-get-the-variables-constraint-names-using-mathprogbase/40511/4 "2020-06-01T09:59:18Z")

</div>

MathProgBase is deprecated and as a matter of fact its support has been removed from Cbc.jl  
We are adding the possibility to build JuMP models from matrix format and to retrieve matrix formats from JuMP in [https://github.com/JuliaOpt/MatrixOptInterface.jl](https://github.com/JuliaOpt/MatrixOptInterface.jl)  
In your example, you do `f[1]` where `f = constraint_object(less_than_constraints[1]).func`.  
This does not work but you can do `terms[1]` where `terms = collect(linear_terms(f))`

---

<div class="post-metadata">

**Author:** ![sugyeong-jo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sugyeong-jo/32/15380_2.png) [@sugyeong-jo](https://discourse.julialang.org/u/sugyeong-jo)\
**Post date:** [June 1, 2020, 11:07am UTC](https://discourse.julialang.org/t/how-to-get-the-variables-constraint-names-using-mathprogbase/40511/5 "2020-06-01T11:07:12Z")

</div>

Oh…! Thanks a lot!!!  
Thank you for appreciate!!! 😂

---

<div class="post-metadata">

**Author:** ![sugyeong-jo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sugyeong-jo/32/15380_2.png) [@sugyeong-jo](https://discourse.julialang.org/u/sugyeong-jo)\
**Post date:** [June 1, 2020, 3:13pm UTC](https://discourse.julialang.org/t/how-to-get-the-variables-constraint-names-using-mathprogbase/40511/6 "2020-06-01T15:13:46Z")

</div>

Unfortunately… I have one more question…  
Then how can I get value for `interval`…?

```julia
julia> constraint_object(all_constraints(m, VariableRef, MathOptInterface.Interval{Float64})[1]).set
MathOptInterface.Interval{Float64}(0.0, 2.0)

```

sorry… 😢  
It is too difficult to me…

---

<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:** [June 1, 2020, 3:45pm UTC](https://discourse.julialang.org/t/how-to-get-the-variables-constraint-names-using-mathprogbase/40511/7 "2020-06-01T15:45:37Z")

</div>

It has fields `.lower` and `.upper`. You can see what fields a type has using `fieldnames`, e.g., `fieldnames(MOI.Interval{Float64})`.

---

<div class="post-metadata">

**Author:** ![sugyeong-jo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sugyeong-jo/32/15380_2.png) [@sugyeong-jo](https://discourse.julialang.org/u/sugyeong-jo)\
**Post date:** [June 1, 2020, 3:47pm UTC](https://discourse.julialang.org/t/how-to-get-the-variables-constraint-names-using-mathprogbase/40511/8 "2020-06-01T15:47:22Z")

</div>

> [@odow](#):
>
> Interval

Thanks a lot! 🥰

---

<div class="post-metadata">

**Author:** ![sugyeong-jo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sugyeong-jo/32/15380_2.png) [@sugyeong-jo](https://discourse.julialang.org/u/sugyeong-jo)\
**Post date:** [June 3, 2020, 7:11am UTC](https://discourse.julialang.org/t/how-to-get-the-variables-constraint-names-using-mathprogbase/40511/9 "2020-06-03T07:11:04Z")

</div>

Thanks a lot.  
Finally, It was success!

I uploaded my code at Github!

> <https://github.com/sugyeong-jo/Tech/blob/master/julia/JuMP/MPS_read.jl>

I want to share my code for everybody 😊

---

<div class="post-metadata">

**Author:** ![mtanneau](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mtanneau/32/17787_2.png) [@mtanneau](https://discourse.julialang.org/u/mtanneau)\
**Post date:** [June 6, 2020, 5:36pm UTC](https://discourse.julialang.org/t/how-to-get-the-variables-constraint-names-using-mathprogbase/40511/10 "2020-06-06T17:36:48Z")

</div>

You can also use the [`QPSReader`](https://github.com/JuliaSmoothOptimizers/QPSReader.jl) package.

Read an MPS file with

```julia
using QPSReader

qps = readqps("example.mps")

```

and you can then access all the problem data, for instance:

```julia
using SparseArrays

# Constraint matrix
A = sparse(qps.arows, qps.acols, qps.avals, qps.ncon, qps.nvar)

# Index --> variable name 
qps.varnames

# Variable name --> index
qps.varindices

```

---

<div class="post-metadata">

**Author:** ![meet.saiya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/meet.saiya/32/49312_2.png) [@meet.saiya](https://discourse.julialang.org/u/meet.saiya)\
**Post date:** [April 27, 2023, 6:00am UTC](https://discourse.julialang.org/t/how-to-get-the-variables-constraint-names-using-mathprogbase/40511/11 "2023-04-27T06:00:08Z")

</div>

@mtanneau Thanks for the reply above with QPS Reader. I was trying to extract constraint matrix and this helped. Is there a way to extract RHS as well?  
I actually have a solver to which I have to pass A, b, C, d according to constraint sets Ax == b and Cx \<= d where A, C are matrices and b, d are vectors. Is there a way I can extract all 4 from a mps file using QPS or any other library?  
Sorry if the question seems dumb, I’m new to julia and still figuring out syntaxes…

---

<div class="post-metadata">

**Author:** ![mtanneau](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mtanneau/32/17787_2.png) [@mtanneau](https://discourse.julialang.org/u/mtanneau)\
**Post date:** [April 27, 2023, 10:27pm UTC](https://discourse.julialang.org/t/how-to-get-the-variables-constraint-names-using-mathprogbase/40511/12 "2023-04-27T22:27:59Z")

</div>

Hi!

QPSReader represents a problem in the form

\begin{array}{rl} \min \quad & \frac{1}{2} x^{\top} Q x + c^{\top} x + c\_{0}\\ s.t. \quad & b\_{l} \leq A x \leq b\_{u},\\ & l ≤ x ≤ u, \end{array}

You can get the values of A, b\_{l}, b\_{u}, l, u from the `QPSData` struct that is returned by `readqps`.  
It is documented here: [GitHub - JuliaSmoothOptimizers/QPSReader.jl: A reader for MPS and QPS files](https://github.com/JuliaSmoothOptimizers/QPSReader.jl#problem-representation).

To get to the form you mentioned, you will need to extract equality constraints (that’s the easy part because they have the same RHS).  
Then, replace all two-side constraints (which have finite lower bound and a finite upper bound) with two inequalities, and re-arrange all the inequalities to match the Cx \leq d form.  
Don’t forget variable bounds int he process.

---

<div class="post-metadata">

**Author:** ![meet.saiya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/meet.saiya/32/49312_2.png) [@meet.saiya](https://discourse.julialang.org/u/meet.saiya)\
**Post date:** [April 29, 2023, 1:51pm UTC](https://discourse.julialang.org/t/how-to-get-the-variables-constraint-names-using-mathprogbase/40511/13 "2023-04-29T13:51:05Z")

</div>

Hi!  
Thanks for the response. It helped a lot. I found a much simple workaround, although computationally it would cost. The constraints are of the form

```julia
b_l ≤ Ax ≤ b_u

```

So I convert them to

```julia
Ax ≤ b_u and -Ax ≤ -b_l

```

And then use `vcat(A,-A)` and `vcat(b_u, -b_l)` in just inequalities. This would implicitly capture all the equalities and inequalities, although there are twice the number of equations. But for small problems, this seems to be working for me.
