Skip to main content

Capacity constraint in optimization problem




  • Simon Bowly
    Gurobi Staff Gurobi Staff

    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.

  • Isabella Olsson
    First Comment
    First Question

    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



Please sign in to leave a comment.