How to model vehicle starting and ending formula in gurobi?
AnsweredHello,
For my project i have 2 constrains that . I need to ensure my vehicles starting early enough and reach the destination before the deadline. Here I attached latex code \[\begin{equation}
\sum_{t=0}^{t_{max}^k1}\sum_{e_{O_kj\epsilon E}} x_t^k,_{O_kj}=1\ \forall k\ \in\ K
\end{equation}\]
Reach their destination before deadline
They ensure that trucks reach their destination node before deadline
\[\begin{equation}
\sum_{t=0}^{t_{max}^k1}\sum_{e_{O_kj\epsilon E}} x_t^k,_{D_kj}=1\ \forall k\ \in\ K
\end{equation}\]
k = truck
O= orgin
D = destination
t =time
ij = nodes
I wrote something like this in python
for k in set_truck:
for t in set_time_truck:
if t == 0:
mdl.addConstrs(gp.quicksum(x[k,t]+ O[k,j])=1)
and
for k in set_truck:
for t in set_time_truck:
if t == 0:
mdl.addConstrs(gp.quicksum(x[k,t]+ D[k,j])=1)
I know this code totally wrong please help me .

Hi John,
It is not possible for the Community to help you, given the information you provided. The LaTeX code seems wrong, since you are adding \(x\) and \(O,D\) in your code snippet, while in the LaTeX code \(O,D\) are indices. You do not state whether \(x,O,D\) are variables or constants. In your code, there is an \(\texttt{if}\)clause which is not reflected in your LaTeX code. The index of the second summation sign \(e_{O_k j}\) does not appear anywhere. \(t_{\max}^k\) is not defined.
Please have a look at Posting to the Community Forum first before editing your post.
You might also want to have a look at our Gurobi Python Tutorial, the Python webinars and our Technician Routing and Scheduling Problem.
Best regards,
Jaromił 
Hi Jaromił,
I think my latex code is not wrong. I took that formula from this paper
I know my code is stupid. I'm very new to Gurobi and python. sorry for the incomplete question.\(O^k\) Origin of a truck
\(D^k\) Destination of a truck
\(t_{max}^k\) The deadline or due date of truck k to reach its destination no later than then
\(x^k_{t, ij}\) Binary variable=1, if truck a is traveling along edge \(e_{ij}\) between time steps t and \(t + 1\); otherwise it is= 0

Hi John,
Sorry for not getting back to you last week. You can model your first constraint as
import gurobipy as gp
from gurobipy import GRB
m = gp.Model('test')
nodes = ['n1','n2','n3','n4','n5']
trucks, tmax, origin, destination = gp.multidict( {
't1': [5,'n1', 'n4'],
't2': [4,'n2', 'n3'],
't3': [3,'n3', 'n5'] } )
edges, costs = gp.multidict({
('n1', 'n3'): 100,
('n1', 'n2'): 80,
('n1', 'n5'): 120,
('n2', 'n4'): 120,
('n2', 'n5'): 120,
('n3', 'n4'): 90,
('n4', 'n3'): 95,
('n4', 'n5'): 120 })
x = {}
for truck in trucks:
x[truck] = m.addVars(tmax[truck],edges,vtype=GRB.BINARY,name='x_'+str(truck)+'_')
for truck in trucks:
m.addConstr( gp.quicksum( gp.quicksum( x[truck][t,e[0],e[1]] for e in edges if origin[truck] == e[0] ) for t in range(tmax[truck])) == 1, name="constr(2)_"+str(truck))
m.write("myLP.lp")The above scenario is artificial and has to be adjusted to your application. Your variables have a top layer defined by trucks. Thus, the dicitionary \(\texttt{x}\) holds tupledict objects which can be accessed via truck names. Gurobi's multidict can be used to conveniently construct lists of values corresponding to different trucks, such as \(\texttt{tmax, origin,}\) and \(\texttt{destination}\) which can then be accessed via the \(\texttt{[]}\) operator. The quicksum function is used twice since you use a double sum in your formulation.
Writing your model to an LP file via the write function is great for analyzing the model you are constructing step by step.
You can construct the other constraint and the rest of the model in a similar way.
Best regards,
Jaromił
Please sign in to leave a comment.
Comments
4 comments