Skip to main content

Error while defining a variable

Answered

Comments

4 comments

  • Riley Clement
    Gurobi Staff 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 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

Please sign in to leave a comment.