-1

I'm struggling to work with 4 dimension array in python (to optimize then in Gurobi)

My objective function is:

model.setObjective((quicksum(r[i,j,k,l]*x[i,j,k,l] for i,j,k,l in XXX

Subject to a number of constraints:

With the following variable:

# Define variables
x = {}
for i in range(N):
for j in range(M):
for k in range(O):
for l in range(P):
x[i,j,k,l]

I need to input a 4 dimensional array for my r[i,j,k,l], which looks something like this:

SUPPLIER[i], CUSTOMER[j], COLOR[k], TRANSPORT[l],  r[i,j,k,l]

[1,           1,           1,         1,             12]
[1,           1,           1,         2,             34]
[1,           1,           2,         1,             34]
[1,           1,           2,         2,             23]
[1,           2,           1,         1,             67]
[1,           2,           1,         2,             34]
[1,           2,           2,         1,             87]
[1,           1,           2,         2,             53]
[2,           1,           1,         1,             12]
[2,           1,           1,         2,             34]
[2,           1,           2,         1,             23]
[2,           1,           2,         2,             12]
[2,           2,           1,         1,             24]
[2,           2,           1,         2,             78]
[2,           2,           2,         1,             09]
[2,           1,           2,         2,             12]

I have done it with 2D and 3D, but there is something I'm missing for 4D arrays.

Do you have a similar example to share so I can learn? Can you help me to do the syntax? I guess I need to use numpy, but still not sure.

Thanks!

Vinicius Placco
  • 1,683
  • 2
  • 14
  • 24
German
  • 1

1 Answers1

0

I think that you just have a problem of range, for i in range(N) meaning that i =0,1,2...N-1. You write for i range(1,N+1), also for j,k,l...

Issouf
  • 140
  • 12