import matplotlib.pyplot as plt import io def run_picking_optimization(message, picking_df): reasoning_steps = [] df = picking_df.copy() # --------------------------------------------------------- # 1️⃣ Convert aisle–rack to coordinates # --------------------------------------------------------- df["x"] = df["Aisle"] df["y"] = df["Rack"] reasoning_steps.append("Converted Aisle–Rack values into x–y coordinate grid.") # --------------------------------------------------------- # 2️⃣ Compute Manhattan distance from start (0,0) # --------------------------------------------------------- df["Distance"] = df["x"].abs() + df["y"].abs() df = df.sort_values("Distance").reset_index(drop=True) reasoning_steps.append("Calculated Manhattan distance and sorted for optimal walk order.") # --------------------------------------------------------- # 3️⃣ Generate route plot # --------------------------------------------------------- plt.figure(figsize=(6, 6)) plt.plot(df["x"], df["y"], marker="o", linestyle="-") plt.title("Optimized Picking Route") plt.xlabel("Aisle") plt.ylabel("Rack") # Save image to memory for Gradio buf = io.BytesIO() plt.savefig(buf, format="png") buf.seek(0) plt.close() reasoning_steps.append("Generated optimized walking path visualization.") explanation = ( "### 🚚 Picking Route Optimization\n" "Using Manhattan distance and spatial ordering, an optimal walking sequence was generated.\n\n" "#### 🔍 Key Reasoning Steps:\n" + "\n".join([f"- {r}" for r in reasoning_steps]) ) return explanation, buf