How to store time value to multidict form a constraint in Gurobi
AnsweredHello
I need to create multidict variable that stores the arrival time of the truck in each node.
I expect values to store in a variable format something like this. TA = {(node,truck): time}
To find TA (arrival time) I have an equation (constraint): departure time truck at previous node + time between the previous node and this node
Part 1 = \( \sum_{ij}\ x_{ij}^h\ast d_{ij}^h/speed\ \ h\forall H \)
Waiting time = part2
Part 2 = \(\sum_{i}max\left(TA_i^h\right)–jTDjh+dij/speedxijhifoh!=ih∀H\)
\( x_{ij}^h \) decision variable if 1 truck travel at edge i,j otherwise 0
And start node and end node time of departure and arrival time = 0 for all truck
TA = model.addVars(node_list, truck, vtype=gp.GRB.CONTINUOUS, name="TA")
TD = model.addVars(node_list, truck, vtype=gp.GRB.CONTINUOUS, name="TD")
model.addConstrs(TA[origin[h],h] == 0 for h in truck)
model.addConstrs(TD[origin[h],h] == 0 for h in truck)
objective_second_part_1 = gp.LinExpr()
for edge in road_network.edges():
for h in truck:
objective_second_part_1 += x[edge][h] * road_network.get_edge_data(*edge)["weight"]/speed
objective_second_part_2 = gp.LinExpr()
for i in node_list:
for h in truck:
objective_second_part_2 += max(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])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-98-03a0f644063a> in <module>
3 for i in node_list:
4 for h in truck:
----> 5 objective_second_part_2 += max(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])
TypeError: 'Var' object is not iterable
-
Official comment
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 try Gurobot, our chatbot interface offering instant, expert-level support. -
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 += auxBest regards,
Jaromił0 -
Hello Jaromił,
TA[i,h] = arrival time of truck at node i. That is calculatemodel.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] = auxobjective_second_part_2 += (TD[i,h]-TA[i,h] for i in nodelist for j in nodelist)
this is what I need.
please help me0 -
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] = auxjust 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
Post is closed for comments.
Comments
4 comments