Integer variable as index of input matrix

Hi people,

Can I formulate an expression in Julia and use an integer variable as an index of an input matrix?

@variable(m, 1 <= t[1:3, 1:3] <= 8, Int)
P = [ 90 80 60 70 50 30 10 0]

for i = 1:3
for j = 1:3
M[i,j] = P[t[i,j]] # M is a matrix 3x3 filled with P values based on t indices
end
end

and I am using M in an expression for optimization.

Thank you in advance!

Can I formulate an expression in Julia and use an integer variable as an index of an input matrix?

No.

But you can replicate the same thing using binary variables:

model = Model()
@variable(model, z[i=1:3, j=1:3, t=1:8], Bin)
@variable(model, M[i=1:3, j=1:3])
@constraint(model, [i=1:3, j=1:3], M[i,j] == sum(P[k] * z[i,j,k] for k in 1:8))
@constraint(model, [i=1:3, j=1:3], sum(z[i,j,:]) == 1)
1 Like

Perfect! thank you!