Skip to main content

How to store time value to multidict form a constraint in Gurobi

Answered

Comments

3 comments

  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Hi John,

    I guess that the term

    max(TA[i,h])

    shall be used to model the maximum of all \(\texttt{TA[i,h]}\) for a fixed \(\texttt{i}\).

    Note that Gurobi's max_ function has an additional underscore and it requires a list of variables and not just a single one. Moreover, it has to be modeled via an auxiliary variable and an equality constraint. So very likely, your \(\texttt{for}\)-loop should read something like

    for i in node_list:
    max_list = []
        for h in truck:
          objective_second_part_2 -= quicksum(TD[j,h]+road_network.get_edge_data(j,i)["weight"]/speed for j in node_list if j != origin[h])
    max_list.append(TA[i,h])
    aux = model.addVar(lb=-gp.GRB.INFINITY, name="auxVar_%d"%(i))
    model.addConstr(aux == gp.max_(max_list), name="auxConstr_%d"%(i))
    objective_second_part_2 += aux

    Best regards, 
    Jaromił

    0
  • John Raphy Karippery
    Gurobi-versary
    Investigator
    Collaborator

    Hello Jaromił,
    TA[i,h] = arrival time of truck at node i. That is calculate 

    model.addConstrs(TA[origin[h],h] == 0 for h in truck)
    model.addConstrs(TD[origin[h],h] == 0 for h in truck)

    for j in nodelist:
    for h in truck:
    TA[i,h] = quicksum(TD[j,h]+road_network.get_edge_data(j,i)["weight"]/speed for j in node_list if j != origin[h])

    And to find departure time of truck h at node i  =  max arrival time of all truck at node i 

    for i in nodelist:
    for h in tuck:
    model.addConstr(aux== gp.max_(TA[i,h]), name="auxConstr_%d"%(i))
    TD[i,h] = aux

    objective_second_part_2 += (TD[i,h]-TA[i,h] for i in nodelist for j in nodelist)

    this is what I need.

    please help me 

    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Hi John,

    As explained in my previous comment, the constraint

    model.addConstr(aux== gp.max_(TA[i,h]), name="auxConstr_%d"%(i))

    will not work because \(\texttt{TA[i,h]}\) is a single variable. However, to my understanding, you want to compute the maximum value of all truck at node \(\texttt{i}\). Thus the \(\texttt{for}\)-loop should be

    aux = {}
    for i in node_list:
    max_list = []
        for h in truck:
    max_list.append(TA[i,h])
    aux[i] = model.addVar(lb=-gp.GRB.INFINITY, name="auxVar_%d"%(i))
    # add constraint aux_i = max(TA[i,0], TA[i,1],...)
    model.addConstr(aux[i] == gp.max_(max_list), name="auxConstr_%d"%(i))

    The \(\texttt{for}\)-loop you are currently using

    for i in nodelist:
    for h in tuck:
    model.addConstr(aux== gp.max_(TA[i,h]), name="auxConstr_%d"%(i))
    TD[i,h] = aux

    just sets \(TD_{i,h} = TA_{i,h}\) because \(\texttt{TA[i,h]}\) is one element and thus \(\max(TA_{i,h}) = TA_{i,h}\).

    Best regards, 
    Jaromił

    0

Please sign in to leave a comment.