How to acheive cumulative sum of cumulative 3D arrays in gurobi python
Awaiting user inputSay I have X1 and X2 which is based on some operations between variables and parameters of the model and so X1 and X2 will be variables.
x1=
[[[1, 6], [2, 7], [3, 8], [4, 9], [5, 10]],
[[1, 6], [2, 7], [3, 8], [4, 9], [5, 10]],
[[1, 6], [2, 7], [3, 8], [4, 9], [5, 10]],
[[1, 6], [2, 7], [3, 8], [4, 9], [5, 10]]]
x2=
[[[0, 0], [1, 6], [2, 7], [3, 8], [4, 9]],
[[0, 0], [1, 6], [2, 7], [3, 8], [4, 9]],
[[0, 0], [1, 6], [2, 7], [3, 8], [4, 9]],
[[0, 0], [1, 6], [2, 7], [3, 8], [4, 9]]]
Then I need the following, that is to check the cumulative sum of cumulative sum of arrays y1>=y2
y1=
[[[1, 21], [3, 28], [6, 36], [10, 45], [15, 55]],
[[1, 21], [3, 28], [6, 36], [10, 45], [15, 55]],
[[1, 21], [3, 28], [6, 36], [10, 45], [15, 55]],
[[1, 21], [3, 28], [6, 36], [10, 45], [15, 55]]]
y2=
[[[0, 10], [1, 16], [3, 23], [6, 31], [10, 40]],
[[0, 10], [1, 16], [3, 23], [6, 31], [10, 40]],
[[0, 10], [1, 16], [3, 23], [6, 31], [10, 40]],
[[0, 10], [1, 16], [3, 23], [6, 31], [10, 40]]]
I have tried
for l in range(2):
for j in range (5):
for k in range (4):
o.addConstr(grb.quicksum(grb.quicksum(x1[i,jj,ll] for jj in n if jj <=j)for ll in d if ll<=l) >= (grb.quicksum(grb.quicksum(x2[i,jj,ll] for jj in n if jj <=j)for ll in d if ll<=l)))
but that is not correct, Can you help? This is a simple snippet of my complex model. If my complete model needed for clear picture, I can provide those too.
-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
Hi,
I am not sure what you are trying to achieve. A rather standard way of summing over all values of a multidimensional list would be the use of \(\texttt{for}\)-loops
res = 0
for i in x1:
for j in i:
for k in j:
res += k
print(res)However, this will not work when \(\texttt{x1}\) is an optimization variable.
You can introduce \(\texttt{x1,x2}\) as a 3 dimensional optimization variable via the addVars function
x1 = model.addVars(2,3,4)
x2 = model.addVars(4,5,6)and then sum over all variable using the quicksum function
model.addConstr(gp.quicksum(x1[i,j,k] for i in range(2) for j in range(3) for k in range(4)
>= gp.quicksum(x2[i,j,k] for i in range(4) for j in range(5) for k in range(6))Does this answer you question?
Best regards,
Jaromił0
Post is closed for comments.
Comments
2 comments