I am creating an optimization model with 2 sets of binary decision variables. The first, site, is regarding which of 380 cities to place manufacturing sites in, and the second, ASSG is regarding which of 12 cities each manufacturing site will be assigned to service. There will be 3 manufacturing sites total.
I need to add a constraint that the sum of all cities serviced by a potential manufacturing site is the number of cities that need serviced, 12, but this limit needs to be 0 if a manufacturing site is not placed in that location.
This is difficult for me to explain so I've included a screenshot of the Excel model I am trying to scale-up using Python. The "Logical constraint" is what I am trying to code into Python:
I know this code is a bit of a mess, I'm not great with dictionaries and there's too much going on here for me to keep track of it, but this is what I have so far:
site = m.addVars(siteLoc, vtype=GRB.BINARY, name='site') # siteLoc is a list of 380 potential mfg site locations
ASSG = m.addVars(siteASSG, vtype=GRB.BINARY, name='ASSG') # siteASSG is a gurobi tuplelist of the format: (potential mfg site location, one of 12 cities mfg site will service)
m.update()
m.addConstr(sum(val for key, val in ASSG.items() if key[0] == k for k in site.keys()) <= 12 * val for key, val in site.items())

if key[0] ==kbut you usekin the second for loop. If this is your problem, you may want to change that order in yoursumi.e.sum(val for key, val in ASSG.items() for k in site.keys() if key[0] == k) <= ...– EhsanK Oct 16 '19 at 05:250or12depending on that item's value insite. – Jacob Myer Oct 16 '19 at 05:40<= 12 * k for k in site.keys()on the right-hand side but that gives meTypeError: unsupported operand type(s) for -: 'generator' and 'NoneType'– Jacob Myer Oct 16 '19 at 06:08vtype=GRB.BINARYto be sure. – Jacob Myer Oct 16 '19 at 14:22