メインコンテンツへスキップ

Adding Quadratic Constraint python

回答済み

コメント

4件のコメント

  • Jaromił Najman
    Gurobi Staff 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 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

サインインしてコメントを残してください。