Skip to main content

Decision variables

Answered

Comments

8 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 why not try our AI Gurobot?.
  • Jaromił Najman
    • Gurobi Staff

    Hi Anne,

    The indentation of your Python code seems to be missing. Could you please update it for better understanding?

    I am not 100% sure what you are looking for here but one convenient way to achieve your goal would be to define too many variables and only use the ones of interest. Gurobi's presolving step will then delete the unnecessary variables from the problem. However, note that those useless variables will still attain some value in the solution vector. A possible code could look something like this

    import gurobipy as gp
    from gurobipy import GRB

    model = gp.Model("test")

    # we are only interested in variables
    # x[1,0], x[1,2], x[1,5]
    # x[2,0], x[2,2], x[2,5]
    # x[4,0], x[4,2], x[4,5]
    I = [1,2,4]
    J = [0,2,5]

    # define 6x6 variables
    x = model.addVars(6,6,vtype=GRB.CONTINUOUS, name = 'x')
    # from the 6x6 variables, only use the ones of interest
    model.addConstr( gp.quicksum(x[i,j] for i in I for j in J) == 0)
    # write LP file to analyze model for correctness
    model.write("myLP.lp")

    Is this what you are looking for?

    Best regards,
    Jaromił

    0
  • Anne Kreté
    • Gurobi-versary
    • First Comment
    • First Question

    Dear Jaromił,

    Thank you for your advice, I updated my question.
    The possible i,j,k,t,t' combinations of the variable is very large, which makes it impossible to solve it if we define all the possible x decision variables. Therefore, we were wondering if we could store all the right combinations of i,j,k,t,t' seperately (e.g. in a dictionary) and then create the x decision variables.

    Later on we want to model the following constraint:

    So is it possible to define specifc x decision variables and later on sum over specific x decision variables in my constraints? 


     

    0
  • Jaromił Najman
    • Gurobi Staff

    Dear Anne,

    Using a dictionary certainly works. An example code would be

    import gurobipy as gp
    from gurobipy import GRB

    model = gp.Model("test")

    # lists holding variable indices of interest
    I = [1,2,4]
    J = [0,3,5]
    K = [3,4,7,8]
    T = [1,2,3,4]
    TT = [0,6]

    x = {}
    # define IxJxKxTxTT variables
    for i in I:
    for j in J:
    for k in K:
    for t in T:
    for tt in TT:
    x[i,j,k,t,tt] = model.addVar(vtype = GRB.BINARY, name = "x_%d_%d_%d_%d_%d"% (i,j,k,t,tt))

    # add the constraint of interest
    model.addConstrs(gp.quicksum(x[i,j,k,t,tt] for j in J for tt in TT) == 1 for i in I for k in K for t in T if i == 1 if t==3)

    # write LP file to analyze model for correctness
    model.write("myLP.lp")

    Best regards,
    Jaromił

    0
  • Anne Kreté
    • Gurobi-versary
    • First Comment
    • First Question

    Dear Jaromił,

    Thank you for your response.
    We implemented your solution and adapted it to our situation, however we still get some errors.

    I = [0,1,2,3] #Set start locations
    J = [0,1,2,3] #Set end locations
    T = [0,1,2,3,4,5,6,7,8,9] #Time window
    K = [0,1] #Set of order numbers
    Orders = [[0,1,3,1,7],[1,0,1,2,8]] #List with orders [ID,Origin location, Destination location, Start Time, Deadline]
    TravelTime = [[1,2,3,2],[2,1,1,3],[3,1,1,2],[2,3,2,1]] #List with for each start location the travel time to the end location TravelTime[0][0]=travel time from i=0 to j=0

    model = grb.Model("TruckPlanning")
    model.modelSense = grb.GRB.MINIMIZE

    x = {}
    # define IxJxKxTxTT variables
    for k in K:
    for i in I:
    for j in J:
    for t in range(Orders[k][3],Orders[k][4]): #As t is dependent on k
    tt = t+TravelTime[i][j]
    x[k,i,j,t,tt] = model.addVar(vtype = grb.GRB.BINARY, name = "x_%d_%d_%d_%d_%d"% (k,i,j,t,tt))

    model.addConstrs(grb.quicksum(x[k,i,j,t,tt] for j in J for tt in T) == 1 for i in I for k in K for t in T if i == Orders[k][1] if t== Orders[k][3])

    We get the following error: KeyError: (1, 0, 0, 3, 0).
    We think it has to do with the part "for t in range(Orders[k][4],Orders[k][5])". 
    Do you have an idea to solve this error?

    0
  • Jaromił Najman
    • Gurobi Staff

    Hi Anne,

    The \(\texttt{for}\)-loop

    for t in range(Orders[k][4],Orders[k][5]):

    seems incorrect, because the lists \(\texttt{Orders[k]}\) only have 5 elements and not 6. Thus, e.g., \(\texttt{Orders[0][5]}\) throws an error.

    Additionally, you define your \(\texttt{x}\) variables as

    x[k,i,j,t,tt]

    but you access them as

    x[i,j,k,t,tt]

    in the \(\texttt{quicksum}\) term.

    Best regards,
    Jaromił

    0
  • Anne Kreté
    • Gurobi-versary
    • First Comment
    • First Question

    Dear Jaromił,

    Thank you for your quick response. Apologies, we made some errors copying the code into the comment.
    So, we updated our previous comment. However, we still get the following error:
    KeyError: (1, 0, 0, 2, 0)

    0
  • Jaromił Najman
    • Gurobi Staff

    Dear Anne,

    The KeyError occurs, because the \(\texttt{tt}\) index is defined over the indices \(\texttt{t, i, j}\) and not over the \(\texttt{T}\) list. The constraint should read

    model.addConstrs(gp.quicksum(x[k,i,j,t,t+TravelTime[i][j]] for j in J) == 1 for i in I for k in K for t in range(Orders[k][3],Orders[k][4])  if i == Orders[k][1] if t== Orders[k][3])

    Note that your \(\texttt{t}\) is dependent of the \(\texttt{Orders}\) list and thus cannot be directly iterated over the list \(\texttt{T}\). The \(\texttt{tt}\) index then depends on the indices \(\texttt{t, i, j}\) and cannot be just iterated over the list \(\texttt{T}\) as well.

    Best regards,
    Jaromił

    0

Post is closed for comments.