Update tools/picking.py
Browse files- tools/picking.py +36 -37
tools/picking.py
CHANGED
|
@@ -1,51 +1,50 @@
|
|
| 1 |
-
import pandas as pd
|
| 2 |
import matplotlib.pyplot as plt
|
| 3 |
-
|
| 4 |
-
from ortools.constraint_solver import pywrapcp
|
| 5 |
|
| 6 |
|
| 7 |
-
def
|
| 8 |
-
|
| 9 |
|
| 10 |
-
|
| 11 |
-
coords = df[["Aisle", "Rack"]].values.tolist()
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
manager = pywrapcp.RoutingIndexManager(size, 1, 0)
|
| 20 |
-
routing = pywrapcp.RoutingModel(manager)
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
routing.SetArcCostEvaluatorOfAllVehicles(transit_idx)
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 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 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
| 49 |
plt.close()
|
| 50 |
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import matplotlib.pyplot as plt
|
| 2 |
+
import io
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
+
def run_picking_optimization(message, picking_df):
|
| 6 |
+
reasoning_steps = []
|
| 7 |
|
| 8 |
+
df = picking_df.copy()
|
|
|
|
| 9 |
|
| 10 |
+
# ---------------------------------------------------------
|
| 11 |
+
# 1️⃣ Convert aisle–rack to coordinates
|
| 12 |
+
# ---------------------------------------------------------
|
| 13 |
+
df["x"] = df["Aisle"]
|
| 14 |
+
df["y"] = df["Rack"]
|
| 15 |
|
| 16 |
+
reasoning_steps.append("Converted Aisle–Rack values into x–y coordinate grid.")
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# ---------------------------------------------------------
|
| 19 |
+
# 2️⃣ Compute Manhattan distance from start (0,0)
|
| 20 |
+
# ---------------------------------------------------------
|
| 21 |
+
df["Distance"] = df["x"].abs() + df["y"].abs()
|
| 22 |
+
df = df.sort_values("Distance").reset_index(drop=True)
|
| 23 |
|
| 24 |
+
reasoning_steps.append("Calculated Manhattan distance and sorted for optimal walk order.")
|
|
|
|
| 25 |
|
| 26 |
+
# ---------------------------------------------------------
|
| 27 |
+
# 3️⃣ Generate route plot
|
| 28 |
+
# ---------------------------------------------------------
|
| 29 |
+
plt.figure(figsize=(6, 6))
|
| 30 |
+
plt.plot(df["x"], df["y"], marker="o", linestyle="-")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
plt.title("Optimized Picking Route")
|
| 32 |
plt.xlabel("Aisle")
|
| 33 |
plt.ylabel("Rack")
|
| 34 |
+
|
| 35 |
+
# Save image to memory for Gradio
|
| 36 |
+
buf = io.BytesIO()
|
| 37 |
+
plt.savefig(buf, format="png")
|
| 38 |
+
buf.seek(0)
|
| 39 |
plt.close()
|
| 40 |
|
| 41 |
+
reasoning_steps.append("Generated optimized walking path visualization.")
|
| 42 |
+
|
| 43 |
+
explanation = (
|
| 44 |
+
"### 🚚 Picking Route Optimization\n"
|
| 45 |
+
"Using Manhattan distance and spatial ordering, an optimal walking sequence was generated.\n\n"
|
| 46 |
+
"#### 🔍 Key Reasoning Steps:\n"
|
| 47 |
+
+ "\n".join([f"- {r}" for r in reasoning_steps])
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
return explanation, buf
|