How to model vehicle starting and ending formula in gurobi?

Answered

Comments

4 comments

  • Jaromił Najman

    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ł

    1
    Comment actions Permalink
  • John Raphy Karippery

    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

    0
    Comment actions Permalink
  • Jaromił Najman

    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ł

    0
    Comment actions Permalink
  • John Raphy Karippery

    Hello Jaromił,

            Thank you so much. you solve my 2 weeks' Error in an hour. :D  I was kind of stuck, now I see light in the corner. 

    Thank you
    John
     

    0
    Comment actions Permalink

Please sign in to leave a comment.

Powered by Zendesk