Multi-period VRP Inventory stuck
AnsweredHi Gurobi Team,
I am working on my undergraduate thesis on multi period VRP in collection scheduling and routing. The inventory will be the fundamental point to trigger the multiperiod method. I want to make sure in scheduling selection will consider the demand satisfaction.
Indexes:
- k is index for set period [T]
- i is index for set vehicle [m]
- j is index for set nodes [n]
Notes:
- I[k] = inventory in period k
- e[i,j,k] = amount of goods picked up by vehicle i, from node j, in period k
- d[k] = demand in period k
- initial_inventory = initial inventory in the beginning of period
- ss = safety stock
This multi period will work in 2 days period of pickup routing. But, the demand (d[k]) will need to be 3 period data because d[k] is needed for calculating I[k] and e[i,j, k-1]. It is like this because the pickup today is aiming to fulfill the next day's demand.
Hence, in period T=3 there will be no outcome of e[i,j,3] because there's no data of d[4]. Also, d[1] is needed because we need to calculate I[1]. So, I'm confused in calling list 'T', because variable 'e' and 'd' are having different number of period (e only have 2, d have 3).
This is the code that I'm using:
"constraint pickup minimal quantity - considering multi period inventory"
for k in range (t+1):
if k == 0:
M.addConstr(gb.quicksum(gb.quicksum(e[i, j, k] for i in range (m)) for j in range (n)) >= initial_inventory - d[k] - ss) #apakah ga masalah d[k] aja?
if k <= t:
M.addConstr(gb.quicksum(gb.quicksum(e[i, j, k] for i in range (m)) for j in range (n)) >= I[k-1] - d[k] - ss) #not really sure penggunaannya
M.update()
Now I'm confused in using:
for t in range (T+1): #or for t in range (T):
Also, in the variable definiton I put it like this for calling the list 't'. From the df_dm, the T is having length 3 period.
T = df_dm.shape[0] -1 # number of days to run --> because collection only needs 2 periods
Here's the code (also inputting code that are corelated):
"constraint pickup minimal quantity" for k in range (T+1): if k == 0: M.addConstr(gb.quicksum(gb.quicksum(e[i, j, k] for i in range (m)) for j in range (n)) >= d[k+1] + ss - initial_inventory) if k <= T: M.addConstr(gb.quicksum(gb.quicksum(e[i, j, k] for i in range (m)) for j in range (n)) >= d[k+1] + ss - I[k]) M.update() "constraint inventory" for k in range (T): if k == 0: M.addConstr((I[k] == initial_inventory - d[k] + gb.quicksum(gb.quicksum(e[i, j, k] for i in range (m)) for j in range (n))),name="inventory 1") else: M.addConstr((I[k] == I[k-1] - d[k] + gb.quicksum(gb.quicksum(e[i, j, k] for i in range (m)) for j in range (n))), name= "inventory 2") M.update()
We get this error code:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-22-440a50de8d8b> in <module>
1 decisionVariables(M, gb, m, n, t, e, I, y, z, s) # add decision variables to model
2 objectiveFunction(M, gb, z, n, m, t, grid) # add objective function to model
----> 3 Constraints(M, gb, e, I, y, z, s, m, n, t, et, lt, sd, grid, bigM, d, P, initial_inventory, ss, df_V, tao) # add constraints to model
4
5 # M.computeIIS()
<ipython-input-18-1ab10697b5ae> in Constraints(M, gb, e, I, y, z, s, m, n, t, et, lt, sd, grid, bigM, d, P, initial_inventory, ss, df_V, tao)
83 M.addConstr(gb.quicksum(gb.quicksum(e[i, j, k] for i in range (m)) for j in range (n)) >= d[k+1] + ss - initial_inventory) #apakah ga masalah d[k] aja?
84 if k <= t:
---> 85 M.addConstr(gb.quicksum(gb.quicksum(e[i, j, k] for i in range (m)) for j in range (n)) >= d[k+1] + ss - I[k]) #not really sure penggunaannya
86 M.update()
87
src\gurobipy\gurobi.pxi in gurobipy.gurobipy.quicksum()
<ipython-input-18-1ab10697b5ae> in <genexpr>(.0)
83 M.addConstr(gb.quicksum(gb.quicksum(e[i, j, k] for i in range (m)) for j in range (n)) >= d[k+1] + ss - initial_inventory) #apakah ga masalah d[k] aja?
84 if k <= t:
---> 85 M.addConstr(gb.quicksum(gb.quicksum(e[i, j, k] for i in range (m)) for j in range (n)) >= d[k+1] + ss - I[k]) #not really sure penggunaannya
86 M.update()
87
src\gurobipy\gurobi.pxi in gurobipy.gurobipy.quicksum()
<ipython-input-18-1ab10697b5ae> in <genexpr>(.0)
83 M.addConstr(gb.quicksum(gb.quicksum(e[i, j, k] for i in range (m)) for j in range (n)) >= d[k+1] + ss - initial_inventory) #apakah ga masalah d[k] aja?
84 if k <= t:
---> 85 M.addConstr(gb.quicksum(gb.quicksum(e[i, j, k] for i in range (m)) for j in range (n)) >= d[k+1] + ss - I[k]) #not really sure penggunaannya
86 M.update()
87
KeyError: (0, 0, 2)
Can you help to check where the possible error comes from?
Thank you very much. I hope it's quite clear.
-
Official comment
This post is more than two 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 Maura,
First you don't need two \(\texttt{quicksum}\)s, a single one is fine:
M.addConstr(
gb.quicksum(e[i, j, k] for i in range(m) for j in range(n)) >= d[k + 1] + ss - I[k]
)The error states that the variable key (0, 0, 2) does not exist i.e. \(\texttt{e[0,0,2]}\).
You can try to debug this by creating the linear expression in for loops and printing the results something like:
for k in range(t + 1):
expr = 0
for i in range(m):
for j in range(n):
print(f"{i=},{j=},{k=}")
expr += e[i, j, k]
print(f"{expr=}")Cheers,
David0
Post is closed for comments.
Comments
2 comments