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

Error while defining a variable

回答済み

コメント

5件のコメント

  • 正式なコメント
    Simranjit Kaur
    • Gurobi Staff
    This post is more than two 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.
  • Riley Clement
    • Gurobi Staff

    Hi Alexandre,

    If E and F are just single variables then use Model.addVar.  The Model.addVars function is expecting one or more indices as the first argument.

    - Riley

    0
  • Alexandre Touzet
    • Gurobi-versary
    • First Comment
    • First Question

    Thank you!! It was that indeed. I have modified it as you proposed but I have other errors in the constraints that I don't know how to fix. Sorry that those are be simple questions, I am new to using gurobi.

    I suppose that it is a syntax issue, but I am not sure what to change.

    The problem is that for the constraints Workload, Min_Di, Tia_lowbound, Tia_upbound and Security I receive the message IndexError: list index out of range. Can you see what is wrong? 

    # Line specific constraints
    One_Train_per_line = m.addConstrs((gp.quicksum(x[i,k] for k in K) == 1 for i in I), name='One_Train_per_line')
    One_Train_at_a_time = m.addConstrs((gp.quicksum(x[i,k] for i in I) <= 1 for k in K), name='One_Train_at_a_time')


    # Workload specific constraints
    Workload = m.addConstrs((W[k] == gp.quicksum(w_i[i] * x[i,k] for i in I) for k in K), name='Workload')
    F_lowbound = m.addConstrs(((F >= W[k]) for k in K), name='F_lowbound')
    F_upbound = m.addConstrs(((F <= W[k] + M*(1-z[k])) for k in K),name='F_upbound')
    Nb_Lines_max_wk = m.addConstrs((gp.quicksum(z[k] for k in K) >= 1 for k in K), name='Nb_Lines_max_wk')
    E_upbound = m.addConstrs(((E <= W[k]) for k in K), name='E_lowbound')
    E_lowbound = m.addConstrs(((E >= W[k] + M*(1-y[k])) for k in K),name='E_upbound')
    Nb_Lines_min_wk = m.addConstrs((gp.quicksum(y[k] for k in K) >= 1 for k in K), name='Nb_Lines_min_wk')


    # Time specific constraints

    Min_Di = m.addConstrs((A[i] + Pta + Ptd + w_i[i] <= D[i] for i in I), name="Min_Di")
    Tia_lowbound = m.addConstrs((Tia[i] >= A[i] + Pta for i in I), name='Tia_lowbound')
    Tia_upbound = m.addConstrs((Tia[i] <= D[i] - Ptd - w_i[i] for i in I), name='Tia_upbound')
    Security = m.addConstrs((D[j] - A[i] >= t_buf - M*(2-x[i,k]-x[j,k]) for i in I for j in I if i!=j for k in K),name="Security")

    0
  • Riley Clement
    • Gurobi Staff

    Hi Alexandre,

    It is not easy for me to help you, since this is not a minimum reproducible example.

    Perhaps the simplest solution is to do some "print debugging".  For example, if there is an issue with Min_Di constraints then try finding the exact error by running the following code:

    for i in I:
    print("value of i", i)
    print(A[i])
    print(w_i[i])
    print(D[i])

    Knowing where exactly the code fails will help you figure out what is going wrong.

    - Riley

     

    0
  • Alexandre Touzet
    • Gurobi-versary
    • First Comment
    • First Question

    Thanks to your advice, I think I found where I was incorrect. Thank you a lot!

    0

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