Skip to main content

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

Awaiting user input

Comments

2 comments

  • Official comment
    Simranjit Kaur
    Gurobi Staff Gurobi Staff
    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?.
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    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.