Skip to main content

Adding Quadratic Constraint python

Answered

Comments

5 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    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 try Gurobot, our chatbot interface offering instant, expert-level support.
  • Jaromił Najman
    • Gurobi Staff

    It looks like a parentheses issue. Could you try

    model.addConstrs((gurobipy.quicksum(Xijk[truck,order,position] * Sik[truck,position]) == Cj[order] for order in orders), name='Completion Time of Order3')
    0
  • Umurcan Vural
    • Gurobi-versary
    • First Comment


    Thank you for the response! Jaromił Najman
    As we got in our previous attempts, now we get a "Quad Exp. object is not iterable" error.

    0
  • Jaromił Najman
    • Gurobi Staff

    You are currently using 2 \(\texttt{for}\)-loops to generate the sums. However, the quicksum function does this for you already. The following code should work

    model.addConstrs((gurobipy.quicksum(Xijk[truck,order,position] * Sik[truck,position] for truck in trucks for position in positions) == Cj[order] for order in orders), name='Completion Time of Order3)

    The reason for the error is because the quicksum function requires iterable data as input argument, e.g., a list. However, the object \(\texttt{Xijk * Sik}\) is a QuadExpr. You could have used the 2 \(\texttt{for}\)-loops to construct the sums, but this would make the code more clumsy and harder to read. Thus, using the quicksum function is recommended here.

    Best regards, 
    Jaromił

    1
  • Umurcan Vural
    • Gurobi-versary
    • First Comment

    Hi again,

    Now it work smoothly, thank you for the detailed explanation!

    Regards,
    Umur.

    0

Post is closed for comments.