File size: 1,716 Bytes
4f8afd1
3bf4c1f
4f8afd1
 
3bf4c1f
 
4f8afd1
3bf4c1f
4f8afd1
3bf4c1f
 
 
 
 
4f8afd1
3bf4c1f
4f8afd1
3bf4c1f
 
 
 
 
4f8afd1
3bf4c1f
4f8afd1
3bf4c1f
 
 
 
 
4f8afd1
 
 
3bf4c1f
 
 
 
 
4f8afd1
 
3bf4c1f
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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