5

I have an optimization problem and I'm using Python and Gurobi to optimize it. In my problem formulation there is a constraint that has a nested sum.

constraint

I've recently started learning python and I searched in gurobi documentation and example codes and I couldn't find any example of nested sum.

I was wondering if anyone could help me solve this problem. Thanks in advance!

nanika
  • 71
  • 2
  • 2
  • 8
  • Something like for FxT check `sum(a*b for a, b in itertools.product(A, B))` is less than z? – Moberg May 05 '17 at 14:04

1 Answers1

9

Use two for statements inside the quicksum() function and two for statements in the generator expression:

mycts = m.addConstrs((quicksum(x[i,f,p]*y[i,f,p,t]
                               for i in I for p in P[i,f]) <= z[f,t]
                               for f in F for t in T), "myconstraint")

where m is your Model object.

Greg Glockner
  • 5,433
  • 2
  • 20
  • 23