How to model all truck in same speed in a path?
AnsweredHello,
Here I am trying to model a constrain that "ensure all truck Drive at the same speed throughout platoon". Here I have a set of parameters like location and set_speed
location, set_speed= gp.multidict({("A", "B"):100,
("A","C"): 70,
("B","D"): 82,
("C","D"): 95,
("C","E"): 100,
("E","F"): 77,
("F","C"): 80,
("E","B"): 85,
("F","C"): 50})
path_trucks = {'truck1': ['A', 'B', 'D'],
'truck2': ['A', 'C', 'D'],
'truck3': ['A', 'C', 'E', 'B', 'D']}
\(x_{ijv}^k\) Binary variable=1, if truck \(k\) traverses edge \(e_{i,j}\) at speed \(v\)
\[\begin{equation}
\sum_{v\ \in\ V} x_{ijv}^k\le1\ \ \forall k\ \in\ K;\ p_{i,j}\ \in G
\end{equation}\]
syntax I tried
for v in set_speed.values():
mdl.addConstrs(gp.quicksum(x[i,j,v] <= 1 for i,j in location))
Error
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-229-331f7d8385ad> in <module>
2 #mdl.addConstrs(gp.quicksum(x[i,j] <= 1 for i,j in set_speed )) 3 for v in set_speed.values():
----> 4 mdl.addConstrs(gp.quicksum(x[i,j,v] <= 1 for i,j in location ) ) gurobi.pxi in gurobipy.quicksum()
<ipython-input-229-331f7d8385ad> in <genexpr>(.0)
2 #mdl.addConstrs(gp.quicksum(x[i,j] <= 1 for i,j in set_speed )) 3 for v in set_speed.values():
----> 4 mdl.addConstrs(gp.quicksum(x[i,j,v] <= 1 for i,j in location ) ) KeyError: ('A', 'B', 100)
Please help me
-
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 why not try our AI Gurobot?. -
Hi John,
Could you also share the way how you define your variables? It seems like the answer in How to model vehicle starting and ending formula in gurobi? is also applicable to model the constraint here.
You are getting the error because the quicksum function requires a list of terms and you provide a list of constraints. You could try
mdl.addConstrs( gp.quicksum( x[i,j,v] for v in set_speed ) <= 1 for i,j in location)
This way, the \(\texttt{set_speed for}\)-loop constructs the data list for the quicksum function and the outer \(\texttt{location for}\)-loop constructs the list for the addConstrs function.
Best regards,
Jaromił0 -
Hi Jaromil,
Thank you so much for your reply. Yes, you are right both questions for the same task. You and this support page help a lot. I got an error when I try your code.
mdl.addConstrs( gp.quicksum( x[i,j,v] for v in set_speed ) <= 1 for i,j in location)Error
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-204-f81e101f85a2> in <module>
----> 1 mdl.addConstrs( gp.quicksum( x[i,j,v] for v in set_speed ) <= 1 for i,j in location)
model.pxi in gurobipy.Model.addConstrs()
<ipython-input-204-f81e101f85a2> in <genexpr>(.0)
----> 1 mdl.addConstrs( gp.quicksum( x[i,j,v] for v in set_speed ) <= 1 for i,j in location)
gurobi.pxi in gurobipy.quicksum()
<ipython-input-204-f81e101f85a2> in <genexpr>(.0)
----> 1 mdl.addConstrs( gp.quicksum( x[i,j,v] for v in set_speed ) <= 1 for i,j in location)
KeyError: ('A', 'B', ('A', 'B'))I tried another way, but I am not sure this model is correct. could you please check that this code?
mdl.addConstrs(x.sum("*", v)<=1 for v in set_speed)Variable
x = mdl.addVars(location, vtype=GRB.BINARY, name="locate")I think error because my variable initialization is wrong.
Help me, please.
Thank you
John0 -
Hi John,
I think it is best to first clarify what you want to do with the set of parameters \(\texttt{location}\) and \(\texttt{set_speed}\).
In your constraint, the variables \(x\) are indexed over \(i,j,\) and \(v\), where \(i,j,\) can be treated as node indices or an edge index. The index \(v\) shall depict a specific velocity. I will assume, that you want to treat \(i,j\) as node indices in the following. First, you have to properly define your variables.
nodes = ["A","B","C","D","E","F"]
V = [100, 70, 82, 95, 77, 80, 85, 50]
trucks, tmax, origin, destination = gp.multidict( { # taken from your other post
't1': [5, 'A', 'D'],
't2': [4, 'B', 'C'],
't3': [3, 'C', 'E'] } )
x = {}
for truck in trucks:
x[truck] = m.addVars(nodes, nodes, V, vtype=GRB.BINARY, name="locate_"+str(truck)+"_")Similar to your other post How to model vehicle starting and ending formula in gurobi?, you can add an additional layer to your variables defined by trucks. You can now access variable \(x^{t1}_{A,B,100}\) via
print(x["t1"]["A","B",100])
Now, to construct your constraint, you have to iterate over a given set of trucks and edges given as locations I suppose (?). Additionally, you want to sum over all possible velocities. This can be done via
m.addConstrs( gp.quicksum( x[truck][l[0],l[1],v] for v in V ) <= 1 for l in location for truck in trucks)
I am still puzzled about the parameters \(\texttt{set_speed}\). Could you try to clarify what exactly you want to do with \(\texttt{set_speed}\) and \(\texttt{path_trucks}\)?
Best regards,
Jaromił0 -
Hi Jaromił,
My task minimizes the cost and time of the Vehicle platooning problem. currently, I am using random input for making the model. so there is no importance for parameters. we can change base on our model.I am doing this task from scratch. so it is very flexible.
In my model: All truck drives same speed at the platoon. The speed of a truck is positive and smaller than allowed.
In my Point :
set_truck = currently we have 3 trucks(truck1,truck2, truck3)
location = all edges in the graph (road)
set_speed = every edge have limited speed. then trucks only can travel that speed when they're in the platoon.
path_trucks = mean path of the truck from start to destination.(I think I can ignore this parameter)
when I explain the next step maybe you can understand more.
ensure is any trucks share the same road segment will make platoon (at the same time, same edge, same segment)
the equation below
\[\begin{equation}
\sum_{e_{ij}\in E}^{k}{\ x_{i,j,t\ }^k==\ \sum_{e_{ij\ \in\ E}}^{w}{\ x_{i,j,t\ }^w\rightarrow\ \ \ q_{i,j}^{k,w}\ =1}}
\end{equation}\]\(q_{i,j}^{k,w}\) Set a binary variable is true if tuck k and w on platoon on edge \(e_{ij}\)
0 -
Hi John,
I think I understand. You then most likely only need to define your variables over the set of your edges, i.e.,
for truck in trucks:
x[truck] = m.addVars(location, V, vtype=GRB.BINARY, name="locate_"+str(truck)+"_")Note that you can still only access a variable \(\texttt{x}\) via
for l in location:
print(locate[truck][l[0],l[1],set_speed[l]])i.e., you have to access the nodes of each edge instead of simply \(\texttt{locate[truck][l,set_speed[l]]}\).
Best regards,
Jaromił1 -
Hi
That means the constrain will be.m.addConstrs( gp.quicksum( x[truck][l,v] for v in V ) <= 1 for l in location for truck in trucks)
0 -
Hi John,
The constraint has to remain the same, since you have to access the nodes via \(\texttt{l[0]}\) and \(\texttt{l[1]}\)
m.addConstrs( gp.quicksum( x[truck][l[0],l[1],v] for v in V ) <= 1 for l in location for truck in trucks)
The number of variables will be less, since you no longer have a variable for every node combination.
Best regards,
Jaromił0 -
Hello Jaromil,
Now I am facing a problem. I used so many variables in my model that is not a good idea.
How can use the same variable in different constarins?
for example:
I used p_ij variables for constarin for: ensure form platoon with tuck count under platoon 5.p_ij = {}
for k in trucks:
p_ij[k] = mdl.addVars(location, vtype=GRB.BINARY, name="platoon_"+str(k)+"_")
mdl.addConstrs(gp.quicksum(p_ij[k][l[0],l[1]] for l in location) <= 5 for k in trucks )now I need to use this same variable to my other constarin: Each vehicle can have at most one speed per edge.
mdl.addConstrs( gp.quicksum( p_ij[k][l[0],l[1],v] for v in V ) <= 1 for l in location for truck in trucks)
Please help me
0 -
Hi John,
You can try to use the same variables \(p_{ij,v}^k\) for both constraints. You have to think of a way to model the first "truck count" constraint in a way that you use the variables \(p_{ij,v}^k\) and you are not forced to introduce new variables \(p_{ij}^k\). Think of the question: Does the constraint have to hold for all \(v \in V\) or do you have to sum over \(V\) or maybe something else? When you introduce new variables like that to just omit an index, you have to guarantee a relationship of both variables which, in general, makes the problem unnecessary difficult.
As already mentioned in your other post, I think that you should first solely focus on the modelling part (on paper) of your problem instead of the implementation part. You should start the implementation part when your model is at a satisfying state. Otherwise you might waste time going back and forth too much.Best regards,
Jaromił0
Post is closed for comments.
Comments
10 comments