Capacity constraint in optimization problem
AnsweredHi, I have a problem that I haven't been able to work out in Gurobi.
Prodtypes=[] #Product types (i)
Machines=[] #Different machines (ma)
capacity = {}
cost = {}
demand = {}
x={} #number of units
for i in Prodtypes:
m.addConstr(x[i,ma] for ma in Machines)<=capacity[i,ma]
The error I receive is: TypeError: unsupported operand type(s) for -: 'generator' and 'NoneType'
-
Official comment
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?. -
Hi Isabella,
In order to construct a constraint, you need for formulate a linear expression. At the moment your code is creating just a sequence of variables which go into the constraint, not a summation of those variables to create an expression. You should use quicksum here to create the sum of x variables, which is then compared with the capacity constraint:
for i in Prodtypes:
m.addConstr(quicksum(x[i,ma] for ma in Machines) <= capacity[i])This should solve the error, however you should carefully inspect the result. This code creates one constraint for each product, and that constraint involves variables for different machines. From your description however it sounds like this constraint should relate to meeting demand for a product, rather than respecting capacity limits for a machine.
0 -
Hi, thank you very much for your reply.
Each product will have a constraint depending on product type and machine. If the demand were 60 units, machine A or B could still only produce a maximum of 50 products each (of product type 1). So as you said, the constraint is there to respect the capacity of each machine for each product type. Your advice solved the error, however i got a KeyError referring to my product types. Hopefully I'll find a way to fix it.
Again, thank you very much for the advice.
Best regards, Isabella
0
Post is closed for comments.
Comments
3 comments