MBG0903 commited on
Commit
3bf4c1f
·
verified ·
1 Parent(s): 48c47f7

Update tools/picking.py

Browse files
Files changed (1) hide show
  1. 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
- 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
 
 
 
 
 
 
 
 
 
 
 
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