Kuifje02 / vrpy

A python framework for solving the VRP and its variants with column generation.
MIT License
179 stars 43 forks source link

pickup and delivery with time windows #84

Closed jamilfitouri closed 3 years ago

jamilfitouri commented 3 years ago

First of all thank you very much for sharing this great project! Now i wanted to impliment a capacited VRP pickup/delivery with time windows but i got the error below:

Code `# Transform distance matrix to DiGraph A = np.array(DISTANCES, dtype=[("cost", int)]) G_d = from_numpy_matrix(A, create_using=DiGraph())

Transform time matrix to DiGraph

A = np.array(TRAVEL_TIMES, dtype=[("time", int)]) G_t = from_numpy_matrix(A, create_using=DiGraph())

Merge

G = compose(G_d, G_t)

Set time windows

set_node_attributes(G, values=TIME_WINDOWS_LOWER, name="lower") set_node_attributes(G, values=TIME_WINDOWS_UPPER, name="upper")

Set demands and requests

for (u1, v1) in PICKUPS_DELIVERIES: G.nodes[u1]["request"] = v1 G.nodes[u1]["demand"] = PICKUPS_DELIVERIES[(u1, v1)] G.nodes[v1]["demand"] = -PICKUPS_DELIVERIES[(u1, v1)]

Relabel depot

G = relabel_nodes(G, {0: "Source", 17: "Sink"})

if name == "main":

prob = VehicleRoutingProblem(G,
                             load_capacity=20,
                             pickup_delivery=True,
                             time_windows=True,
                             )

prob.solve(cspy= False) #pricing_strategy="BestPaths",exact=False,
print(prob.best_value)
print(prob.best_routes)`

Error message: KeyError Traceback (most recent call last)

in 41 ) 42 ---> 43 prob.solve(cspy= False) #pricing_strategy="BestPaths",exact=False, 44 print(prob.best_value) 45 print(prob.best_routes) ~\Desktop\vrpy-master\vrpy\vrp.py in solve(self, initial_routes, preassignments, pricing_strategy, cspy, exact, time_limit, solver, dive, greedy, max_iter, run_exact) 235 self._pre_solve() 236 # Initialization --> 237 self._initialize(solver) 238 self._solve(dive, solver) 239 ~\Desktop\vrpy-master\vrpy\vrp.py in _initialize(self, solver) 438 self._get_initial_solution() 439 # Initial routes are converted to digraphs --> 440 self._convert_initial_routes_to_digraphs() 441 # Init master problem 442 self.masterproblem = _MasterSolvePulp( ~\Desktop\vrpy-master\vrpy\vrp.py in _convert_initial_routes_to_digraphs(self) 847 edges = list(zip(r[:-1], r[1:])) 848 for (i, j) in edges: --> 849 edge_cost = self.G.edges[i, j]["cost"][0] 850 G.add_edge(i, j, cost=edge_cost) 851 total_cost += edge_cost ~\Anaconda3\envs\test1\lib\site-packages\networkx\classes\reportviews.py in __getitem__(self, e) 1014 def __getitem__(self, e): 1015 u, v = e -> 1016 return self._adjdict[u][v] 1017 1018 # EdgeDataView methods KeyError: 6
Kuifje02 commented 3 years ago

Could you try and see if the data set runs with time windows only (without pickup and delivery) and with pickup and delivery only (without time windows) ? I suspect there is an incompatibility between the two. I would not be surprised if the problem is infeasible (the error is raised while initializing, when building an initial trivial solution, which is probably infeasible).

Kuifje02 commented 3 years ago

What is happening is that the problem in not feasible. You have a request from node 1 to node 6 which cannot be done with the given time window constraints. I will update the code so that a more relevant error is raised, more understandable for the user.

Kuifje02 commented 3 years ago

cd504cd7e02320f72bc5379d8b12c58fbb8cbd00