Arrays of vector variables in definitions of nonlinear programs in JuMP

Is it possible to define arrays of vector variables while building a nonlinear programming problem in JuMP?

I know we can define a scalar variable through

@variable(model,x)

I also know we can define a vector variable through

@variable(model,x[1:10])

which allows us later refer to individual scalar components using, say, x[3].

This can also be extended to matrices and possibly higher-dimension arrays.

But can we also define arrays of vector variables? In the 1D case the notation x[3] would refer to a the 3rd vector variable of the array. The second element of the third vector would then be access through x[3][2]. Is this possible?

As an example, I would like to turn this (artificial) code

f(x) = cos(x)
N = 10
using JuMP, Ipopt
model = Model(Ipopt.Optimizer)
@variable(model, x[1:N])
for i in 1:N-1
    @NLconstraint(model, x[i+1] == f(x[i]))
end
@NLobjective(model, Min, abs(x[N])) 
optimize!(model)

into a vector/array version

f2(x::Vector) = cos.(x)
model2 = Model(Ipopt.Optimizer)
@variable(model2, x[1:2,1:N])          # Here I'd like to create an array of vectors variable, but this doesn't really work.
for i in 1:N-1
    @NLconstraint(model, x[:,i+1] == f2(x[:,i]))   # Related to the above comment, not functional.
end
@NLobjective(model, Min, norm(x[N])) 
optimize!(model)

Is it possible to define arrays of vector variables while building a nonlinear programming problem in JuMP?

JuMP variables are normal Julia objects, so you can build them into arbitrary data structures of your choosing.

model = Model()
x = [@variable(model, [1:10]) for _ in 1:5]
x[3]
x[3][2]

As an example, I would like to turn this (artificial) code
into a vector/array version

This is not supported.

See Tips and tricks · JuMP

1 Like