Collection of JuMP variables

Hello,

I would like to define an array of jump variables, e.g something like

for c:1:C
    @variable(model, mu[c][1:foo(c)] >= 0)
end

or maybe

for c:1:C
    mu[c] = @variable(model, [1:foo(c)] >= 0)
end

but I was not able to find the right syntax,

My goal is later to be able to define constraints on the different mu[c].
I cannot simply use mu = @variable(model,[c=1:C,1:foo(c)]) as in fact the dimension of mu[c] might depends on c.

Any pointer on the right way to do this ?

Are you sure it won’t work the simple way? It seems to for me:

julia> f(i) = 2i -1
f (generic function with 1 method)

julia> using JuMP

julia> m = Model()
Feasibility problem with:
 * 0 linear constraints
 * 0 variables
Solver is default solver

julia> @variable(m, y[i = 1:3, j = 1:f(i)] >= 0, start = i + j)
y[i,j] >= 0 for all i in {1,2,3}, j in {..}

julia> getvalue(y)
y: 2 dimensions, 9 entries:
 [1,1] = 2.0
 [2,1] = 3.0
 [2,2] = 4.0
 [2,3] = 5.0
 [3,1] = 4.0
 [3,2] = 5.0
 [3,3] = 6.0
 [3,4] = 7.0
 [3,5] = 8.0

Do you get an error if you try that?

In the second case you’re probably looking for the syntax:

for c in 1:C
    mu[c] = @variable(model, [1:foo(c)], lowerbound=0)
end 

Thanks for the answer, unfortunately this mgiht not be enough, indeed for some c I want to have mu[c] as a one dimensional variable, and for other as a multi-dimensional variable. Nevertheless your code snippet is helpful to me :slight_smile:

One question : what is the start keyword exactly doing ?

Thanks, actually this might work but unfortunately I don’t know how to initialize mu ?

mu = Variable[] require to use push!(mu,@variable(model,[1:foo(c), lowerbound=0) which fails.

More generically, later on, I would like to have c as a multi index c=[1,3,4].

Best,

Maybe just a dictionary then?

mu = Dict()
mu[1] = @variable(...)

The start keyword lets you initialize a variable to have a particular value before the solving process begins. Generally this is required for nonlinear problems and ignored for linear problems, but that depends on the solver used.