Skip to main content

Skipping non-existing decision variables in for loop



1 comment

  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    You could define a list or a dictionary of indices that are not present and add an \(\texttt{if}\)-clause to your quicksum call.

    import gurobipy as gp

    m = gp.Model()

    xI = 1
    xJ = 2
    x  = m.addVars(xI, xJ, name="x")
    yI = 3
    yJ = 4
    y  = m.addVars(yI, yJ, name="y")

    present = {}
    # loop over the bigger set yI x yJ
    for i in range(yI):
      for j in range(yJ):
        if i < xI and j < xJ:
          present[i,j] = True
          present[i,j] = False

                  for i in range(yI)
                  for j in range(yI) 
                  if present[i,j]))

    Note that the above snippet works only if \(x_I \times x_J\) is a subset of \(y_I \times y_J\) and might require more work if you are using other indices.


Please sign in to leave a comment.