# Extracting data vectors in constraint from sum notation in JuMP

Dear All,

I am working on an optimization problem where I have a variable y_{i,j} where i,j \in [1:n] = \{1,2,\ldots,n\}. I have a constraint of the form

\sum_{j \in [1:n]} y_{j,i} - \sum_{j \in [1:n]} y_{i,j} \geq b_i \ldots \ldots \texttt{con}[i]

for i \in \{1, 2, \ldots ,n\}. This is very easy to construct in JuMP. However, to create a robust formulation for this problem it is beneficial for me to concatenate all the y_{i,j} in one variable x \equiv \{y_{i,j}\}_{i\in [1:n], j \in [1:n] }, and also write down the constraint \texttt{con}[i] in the form

\langle v_i^{\textrm{in}} - v_i^{\textrm{out}}; x\rangle \geq b_i

where \sum_{j \in [1:n]} y_{j,i} = \langle v_i^{\textrm{in}} ; x \rangle, and \sum_{j \in [1:n]} y_{i,j} = \langle v_i^{\textrm{out}} ; x\rangle. I was wondering in JuMP if we can (i) create a concatenated decision variable x \equiv \{y_{i,j}\}_{i\in [1:n], j \in [1:n] }, and (ii) extract the vectors v_i^{\textrm{in}}, v_i^{\textrm{out}} if we create the constraints \texttt{con}[i] for i\in [1:n] in JuMP. Any help will be much appreciated!

I’m not sure I fully understand the question.

If you use constraint_object(con[I]).func you can get the AffExpr of the constraint. That contains the coefficients. But it isn’t a vector. You’d need to write that conversion yourself.

Thanks for your response @odow ! What I meant is that if we concatenate all the variables into one in x \triangleq \{y_{i,j}\}_{i,\in [1:n]j\in[1:n]}, then all the constraints of the form

\sum_{j \in [1:n]} y_{j,i} - \sum_{j \in [1:n]} y_{i,j} \geq b_i \ldots \ldots \texttt{con}[i]

can be written more compactly as

A x \geq b

for some matrix A \in \mathbb{R}^{n^2 \times n}. My question was is there a way to get the matrix A if I create the JuMP model using the constraints in the first form and is there a way to creat x \triangleq \{y_{i,j}\}_{i,\in [1:n]j\in[1:n]} programmatically in JuMP.

You can get the matrix following your PDHG question: Connecting a simple first-order solver to solve standard form linear program to JuMP

You can use x = all_variables(model).

1 Like

This is great, thanks @odow !