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

Objective code for summation

回答済み

コメント

4件のコメント

  • 正式なコメント
    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 why not try our AI Gurobot?.
  • Musa Dursun
    • Gurobi-versary
    • First Question
    • Conversationalist

    The code is as follow;

    import gurobipy as gp
    from gurobipy import GRB

    T = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
    K = ["k1","k2","k3","k4","k5","k6","k7","k8","k9"]
    z = gp.Model("MP")

    x = z.addVars (T, K, vtype =GRB.BINARY, name = "jf")

    Job_assigns = z.addConstrs ((x.sum("*", k) == 1 for k in K), name = "job_assign")

    # Objective
    Result = gp.quicksum (x[T,K]*T)
    z.setObjective(Result, GRB.MINIMIZE)
    0
  • Jaromił Najman
    • Gurobi Staff

    Hi Musa,

    The error \(\texttt{TypeError: unhashable type: 'list'}\) means that you are trying to use a list object as an index. You are doing this at \(\texttt{x[T,K]}\) where both \(\texttt{T}\) and \(\texttt{K}\) are lists.

    You can achieve the correct summation via

    Result = gp.quicksum (x[t,k] * t for t in T for k in K)

    Best regards,
    Jaromił

    0
  • Musa Dursun
    • Gurobi-versary
    • First Question
    • Conversationalist

    Thank you. It worked well.

    0

投稿コメントは受け付けていません。