Assign variables as array of matrices in JuMP

Hello to everyone!

I have a question concernig a possible way to provide the problem variables in JuMP.
I would like to assign the variables in a 3-dimensional matrix, but one of the three dimensions is not constant. To clarify the concept, I add a simplified code that shows my case study.

using JuMP
using CPLEX

mod = Model(CPLEX.Optimizer)
@variable(mod, x[1:4, i=1:6, 1:i])

Now, JuMP accepts the variables written with the form above, but the resulting type of x is: JuMP.Containers.SparseAxisArray{VariableRef,3,Tuple{Int64,Int64,Int64}}. However, being a container, it is not possible to call x with a range in the index (x[1, :, 1] or x[1, 1:6, 1] return both errors). The easiest way that I know to call the variables in this context is exploiting the for:

[x[1, i, 1] for i=1:6]

but, clearly, this form is less compact with respect to the one used for the arrays (x[1, 1:6, 1]) and, in my opinion, makes the script more confusing when it is necessary to write several operations between arrays.

Now, I would ask if it is possible to assign the variables not as a container, but as an array of matrices. In this way, it would be possible to exploit the range in the index to call the variables:

x[1][ :, 1]

I hope that I have been sufficiently clear, thank you in advance for your support.
Bye!

[x[1, i, 1] for i=1:6]

This is the recommended way.

As you found out, slicing SparseAxisArrays has some issues.

You don’t need to use JuMP’s arrays. You can always do something like:

model = Model()
x = [@variable(model) for i = 1:3, j = 2:3]

Hi odow!

Thank you for your answer. I’m very sorry but I do not think to have completely understood your suggestion.
What would be the advantages in defining the variables as you proposed? Does this alternative allow to assign the variables as an array of matrices?

Yes, you could go

x = [@variable(model, [1:2, 1:2]) for i = 1:3]

Ok, I tried and it perfectly works.
Thank you very much!