Create picking.py
Browse files- tools/picking.py +51 -0
tools/picking.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import matplotlib.pyplot as plt
|
| 3 |
+
from ortools.constraint_solver import routing_enums_pb2
|
| 4 |
+
from ortools.constraint_solver import pywrapcp
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def optimize_picking_route(df: pd.DataFrame):
|
| 8 |
+
df = df.copy()
|
| 9 |
+
|
| 10 |
+
# Convert to points
|
| 11 |
+
coords = df[["Aisle", "Rack"]].values.tolist()
|
| 12 |
+
|
| 13 |
+
# Distance matrix (Manhattan)
|
| 14 |
+
size = len(coords)
|
| 15 |
+
dist = [[abs(a1 - a2) + abs(r1 - r2)
|
| 16 |
+
for (a2, r2) in coords] for (a1, r1) in coords]
|
| 17 |
+
|
| 18 |
+
# Solver
|
| 19 |
+
manager = pywrapcp.RoutingIndexManager(size, 1, 0)
|
| 20 |
+
routing = pywrapcp.RoutingModel(manager)
|
| 21 |
+
|
| 22 |
+
def dist_callback(i, j):
|
| 23 |
+
return dist[manager.IndexToNode(i)][manager.IndexToNode(j)]
|
| 24 |
+
|
| 25 |
+
transit_idx = routing.RegisterTransitCallback(dist_callback)
|
| 26 |
+
routing.SetArcCostEvaluatorOfAllVehicles(transit_idx)
|
| 27 |
+
|
| 28 |
+
params = pywrapcp.DefaultRoutingSearchParameters()
|
| 29 |
+
params.first_solution_strategy = routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC
|
| 30 |
+
|
| 31 |
+
solution = routing.SolveWithParameters(params)
|
| 32 |
+
|
| 33 |
+
order = []
|
| 34 |
+
idx = routing.Start(0)
|
| 35 |
+
while not routing.IsEnd(idx):
|
| 36 |
+
order.append(manager.IndexToNode(idx))
|
| 37 |
+
idx = solution.Value(routing.NextVar(idx))
|
| 38 |
+
|
| 39 |
+
ordered_df = df.iloc[order]
|
| 40 |
+
|
| 41 |
+
# Plot
|
| 42 |
+
plt.figure(figsize=(5, 5))
|
| 43 |
+
plt.plot(ordered_df["Aisle"], ordered_df["Rack"], marker="o")
|
| 44 |
+
plt.title("Optimized Picking Route")
|
| 45 |
+
plt.xlabel("Aisle")
|
| 46 |
+
plt.ylabel("Rack")
|
| 47 |
+
img_path = "/tmp/route_plot.png"
|
| 48 |
+
plt.savefig(img_path)
|
| 49 |
+
plt.close()
|
| 50 |
+
|
| 51 |
+
return img_path, ordered_df
|