Update agents/reasoner.py
Browse files- agents/reasoner.py +31 -15
agents/reasoner.py
CHANGED
|
@@ -1,14 +1,26 @@
|
|
| 1 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
| 2 |
|
| 3 |
# -------------------------------------------------------------
|
| 4 |
-
#
|
| 5 |
# -------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
def run_slotting_analysis(message, slotting_df):
|
| 8 |
reasoning = []
|
| 9 |
reasoning.append("Interpreting slotting optimization request.")
|
| 10 |
|
| 11 |
-
#
|
| 12 |
strategy = "Move slow movers outward to free prime space"
|
| 13 |
reasoning.append(f"Selected strategy: {strategy}")
|
| 14 |
|
|
@@ -24,24 +36,25 @@ def run_slotting_analysis(message, slotting_df):
|
|
| 24 |
return explanation, optimized
|
| 25 |
|
| 26 |
|
|
|
|
| 27 |
# -------------------------------------------------------------
|
| 28 |
-
# PICKING ROUTE OPTIMIZATION (WITH
|
| 29 |
# -------------------------------------------------------------
|
| 30 |
-
|
| 31 |
def run_picking_optimization(message, picking_df):
|
| 32 |
reasoning = []
|
| 33 |
reasoning.append("Analyzing picking request…")
|
| 34 |
|
|
|
|
| 35 |
coords = list(zip(
|
| 36 |
picking_df["Aisle"].astype(int).tolist(),
|
| 37 |
picking_df["Rack"].astype(int).tolist()
|
| 38 |
))
|
| 39 |
|
| 40 |
-
# --- Nearest neighbour route ---
|
| 41 |
ordered = [coords.pop(0)]
|
| 42 |
while coords:
|
| 43 |
last = ordered[-1]
|
| 44 |
-
next_point = min(coords, key=lambda c: abs(c[0]-last[0]) + abs(c[1]-last[1]))
|
| 45 |
ordered.append(next_point)
|
| 46 |
coords.remove(next_point)
|
| 47 |
|
|
@@ -58,19 +71,22 @@ def run_picking_optimization(message, picking_df):
|
|
| 58 |
|
| 59 |
explanation = " ".join(reasoning)
|
| 60 |
|
| 61 |
-
#
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
|
| 65 |
# -------------------------------------------------------------
|
| 66 |
-
# FULL WAREHOUSE REPORT (
|
| 67 |
# -------------------------------------------------------------
|
| 68 |
-
|
| 69 |
def run_full_report(message, slotting_df, picking_df):
|
| 70 |
reasoning = []
|
| 71 |
reasoning.append("Building unified warehouse operations report…")
|
| 72 |
|
| 73 |
-
#
|
| 74 |
fast = (slotting_df["Velocity"] == "Fast").sum()
|
| 75 |
med = (slotting_df["Velocity"] == "Medium").sum()
|
| 76 |
slow = (slotting_df["Velocity"] == "Slow").sum()
|
|
@@ -84,8 +100,8 @@ def run_full_report(message, slotting_df, picking_df):
|
|
| 84 |
|
| 85 |
explanation = " ".join(reasoning) + "\n" + summary
|
| 86 |
|
| 87 |
-
#
|
| 88 |
-
_,
|
| 89 |
|
| 90 |
-
#
|
| 91 |
-
return explanation,
|
|
|
|
| 1 |
import matplotlib.pyplot as plt
|
| 2 |
+
import io
|
| 3 |
+
from matplotlib.figure import Figure
|
| 4 |
|
| 5 |
# -------------------------------------------------------------
|
| 6 |
+
# UTILITY: Convert Matplotlib Figure → PNG bytes (Gradio compatible)
|
| 7 |
# -------------------------------------------------------------
|
| 8 |
+
def fig_to_png_bytes(fig: Figure):
|
| 9 |
+
"""Convert a matplotlib figure to PNG bytes for Gradio Image()."""
|
| 10 |
+
buf = io.BytesIO()
|
| 11 |
+
fig.savefig(buf, format="png", dpi=120, bbox_inches="tight")
|
| 12 |
+
buf.seek(0)
|
| 13 |
+
return buf.getvalue()
|
| 14 |
+
|
| 15 |
|
| 16 |
+
# -------------------------------------------------------------
|
| 17 |
+
# SLOTING ANALYSIS
|
| 18 |
+
# -------------------------------------------------------------
|
| 19 |
def run_slotting_analysis(message, slotting_df):
|
| 20 |
reasoning = []
|
| 21 |
reasoning.append("Interpreting slotting optimization request.")
|
| 22 |
|
| 23 |
+
# Example strategy:
|
| 24 |
strategy = "Move slow movers outward to free prime space"
|
| 25 |
reasoning.append(f"Selected strategy: {strategy}")
|
| 26 |
|
|
|
|
| 36 |
return explanation, optimized
|
| 37 |
|
| 38 |
|
| 39 |
+
|
| 40 |
# -------------------------------------------------------------
|
| 41 |
+
# PICKING ROUTE OPTIMIZATION (WITH PNG OUTPUT)
|
| 42 |
# -------------------------------------------------------------
|
|
|
|
| 43 |
def run_picking_optimization(message, picking_df):
|
| 44 |
reasoning = []
|
| 45 |
reasoning.append("Analyzing picking request…")
|
| 46 |
|
| 47 |
+
# Convert to integer pairs
|
| 48 |
coords = list(zip(
|
| 49 |
picking_df["Aisle"].astype(int).tolist(),
|
| 50 |
picking_df["Rack"].astype(int).tolist()
|
| 51 |
))
|
| 52 |
|
| 53 |
+
# --- Nearest neighbour route selection ---
|
| 54 |
ordered = [coords.pop(0)]
|
| 55 |
while coords:
|
| 56 |
last = ordered[-1]
|
| 57 |
+
next_point = min(coords, key=lambda c: abs(c[0] - last[0]) + abs(c[1] - last[1]))
|
| 58 |
ordered.append(next_point)
|
| 59 |
coords.remove(next_point)
|
| 60 |
|
|
|
|
| 71 |
|
| 72 |
explanation = " ".join(reasoning)
|
| 73 |
|
| 74 |
+
# Convert FIG → PNG bytes
|
| 75 |
+
png_bytes = fig_to_png_bytes(fig)
|
| 76 |
+
|
| 77 |
+
# return explanation + PNG bytes (NO slotting table)
|
| 78 |
+
return explanation, png_bytes
|
| 79 |
+
|
| 80 |
|
| 81 |
|
| 82 |
# -------------------------------------------------------------
|
| 83 |
+
# FULL WAREHOUSE REPORT (WITH GRAPH + TABLE)
|
| 84 |
# -------------------------------------------------------------
|
|
|
|
| 85 |
def run_full_report(message, slotting_df, picking_df):
|
| 86 |
reasoning = []
|
| 87 |
reasoning.append("Building unified warehouse operations report…")
|
| 88 |
|
| 89 |
+
# Summary counts
|
| 90 |
fast = (slotting_df["Velocity"] == "Fast").sum()
|
| 91 |
med = (slotting_df["Velocity"] == "Medium").sum()
|
| 92 |
slow = (slotting_df["Velocity"] == "Slow").sum()
|
|
|
|
| 100 |
|
| 101 |
explanation = " ".join(reasoning) + "\n" + summary
|
| 102 |
|
| 103 |
+
# Include picking graph in the full report
|
| 104 |
+
_, fig_bytes = run_picking_optimization(message, picking_df)
|
| 105 |
|
| 106 |
+
# Return: explanation, PNG route graph, AND slotting table
|
| 107 |
+
return explanation, fig_bytes, slotting_df.copy()
|