Search code examples
pythonlinear-programming

How to create objective function in pywraplp when looping through multiple variables


I have a linear program with the following variables.

X[i, j, k] for i,j,k in range(0,100)
Y[j, k] for j, k in range(0,100)

I am wondering how I can create the following constraint for X and objective function from Y.

Constraint: (creates 100 constraints, 1 for each i)
for i in range(0,100):
    solver.Add( sum( X[i,j,k] for j, k in range(0,100) ) == 1)

Objective:
solver.Minimize(solver.Sum(Y[j, k] for j,k in range(0,100) ))

These work if I am only iterating through j, not j and k. I am wondering if there is a way to iterate through multiple lists on a single line sum. If not, is there a way I can store the LP variables into a variable and then create the constraints/objective by summing those variables together? Or what would be the best approach for this? Thanks


Solution

  • replace

      for j,k in range(0,100)
    

    by

      for j in range(0,100) for k in range(0,100)