Skip to main content

How to acheive cumulative sum of cumulative 3D arrays in gurobi python

Awaiting user input


1 comment

  • Jaromił Najman
    Gurobi Staff Gurobi Staff


    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


    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,


Please sign in to leave a comment.