File size: 10,271 Bytes
48c47f7 61854d8 c7f64e3 a9c1106 61854d8 3882ecb a9c1106 48c47f7 e3d30ae 48c47f7 e3d30ae 48c47f7 e3d30ae 48c47f7 61854d8 e3d30ae 61854d8 e3d30ae 61854d8 c7f64e3 61854d8 c7f64e3 61854d8 e3d30ae 61854d8 e3d30ae 61854d8 c7f64e3 6e36c38 defccd4 6e36c38 e3d30ae 6e36c38 e3d30ae 6e36c38 e3d30ae 6e36c38 e3d30ae 6e36c38 e3d30ae 6e36c38 c7f64e3 6e36c38 c7f64e3 6e36c38 e3d30ae 6e36c38 c7f64e3 e3d30ae c7f64e3 e3d30ae defccd4 c7f64e3 defccd4 | 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import io
from PIL import Image # β REQUIRED FOR IMAGE FIX
# ============================================================
# SLOTING OPTIMIZATION β PHASE 1
# ============================================================
def run_slotting_analysis(message, slotting_df):
reasoning_steps = []
df = slotting_df.copy()
velocity_map = {
"fast": 3,
"medium": 2,
"slow": 1
}
df["VelocityNorm"] = df["Velocity"].str.lower().map(velocity_map)
reasoning_steps.append("Mapped velocity categories to numerical weights.")
df["FreqNorm"] = (df["Frequency"] - df["Frequency"].min()) / (
df["Frequency"].max() - df["Frequency"].min() + 1e-8
)
reasoning_steps.append("Normalized frequency to 0β1 scale.")
df["Score"] = (0.6 * df["VelocityNorm"]) + (0.4 * df["FreqNorm"])
reasoning_steps.append("Computed weighted slotting score.")
df = df.sort_values("Score", ascending=False).reset_index(drop=True)
df["Aisle"] = np.arange(1, len(df) + 1)
df["Rack"] = np.linspace(1, 20, len(df)).astype(int)
reasoning_steps.append("Assigned optimal aisle & rack positions.")
explanation = (
"### π¦ Slotting Optimization\n"
"High-velocity & high-frequency SKUs placed in prime aisles.\n\n"
"#### π Reasoning\n" + "\n".join([f"- {r}" for r in reasoning_steps])
)
return explanation, df
# ============================================================
# PICKING ROUTE OPTIMIZATION β PHASE 1
# ============================================================
def run_picking_optimization(message, picking_df):
reasoning_steps = []
df = picking_df.copy()
df["x"] = df["Aisle"]
df["y"] = df["Rack"]
reasoning_steps.append("Converted AisleβRack to coordinate grid.")
df["Distance"] = df["x"].abs() + df["y"].abs()
df = df.sort_values("Distance").reset_index(drop=True)
reasoning_steps.append("Calculated Manhattan distance & sorted sequence.")
# --- Generate 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")
buffer = io.BytesIO()
plt.savefig(buffer, format="png")
plt.close()
buffer.seek(0)
# β Convert BytesIO β PIL (Gradio requirement)
image = Image.open(buffer)
reasoning_steps.append("Generated walking route visualization.")
explanation = (
"### π Picking Route Optimization\n"
"Manhattan-distance-based walk path generated.\n\n"
"#### π Reasoning\n" + "\n".join([f"- {r}" for r in reasoning_steps])
)
return explanation, image
# ============================================================
# DEMAND FORECASTING β MODULE 1
# ============================================================
def run_demand_forecast(message, slotting_df):
reasoning_steps = []
df = slotting_df.copy()
if "Frequency" not in df.columns:
return "Frequency missing β cannot forecast.", None, None
demand = df["Frequency"].astype(float)
reasoning_steps.append("Used SKU picking frequency as demand signal.")
moving_avg = demand.mean()
reasoning_steps.append(f"Computed moving average: {moving_avg:.2f}")
weights = np.linspace(0.1, 1.0, len(demand))
trend = np.sum(demand * weights) / np.sum(weights)
reasoning_steps.append(f"Weighted trend adjustment: {trend:.2f}")
forecast_value = (moving_avg * 0.6) + (trend * 0.4)
next_7_days = [forecast_value * (1 + 0.05 * i) for i in range(7)]
forecast_df = pd.DataFrame({
"Day": [f"Day {i+1}" for i in range(7)],
"Forecasted_Demand": next_7_days
})
reasoning_steps.append("Generated 7-day demand projection.")
# --- Plot forecast ---
plt.figure(figsize=(6, 4))
plt.plot(forecast_df["Day"], forecast_df["Forecasted_Demand"], marker="o")
plt.title("7-Day Demand Forecast")
plt.xlabel("Day")
plt.ylabel("Forecasted Demand")
buffer = io.BytesIO()
plt.savefig(buffer, format="png")
plt.close()
buffer.seek(0)
# β Convert BytesIO β PIL
image = Image.open(buffer)
explanation = (
"### π Demand Forecasting\n"
"Trend-weighted moving average model applied.\n\n"
"#### π Reasoning\n" + "\n".join([f"- {r}" for r in reasoning_steps])
)
return explanation, image, forecast_df
# ============================================================
# REPLENISHMENT ANALYSIS β MODULE 2
# ============================================================
def run_replenishment_analysis(message, slotting_df):
reasoning_steps = []
df = slotting_df.copy()
if "Frequency" not in df.columns:
return "Frequency missing β cannot run replenishment.", None
BIN_CAPACITY = 200
df["CurrentStock"] = BIN_CAPACITY * 0.5
reasoning_steps.append("Assumed current stock = 50% bin capacity.")
df["SafetyStock"] = df["Frequency"] * 3
reasoning_steps.append("Safety stock = 3 days of demand.")
df["DaysUntilStockout"] = df["CurrentStock"] / df["Frequency"].replace(0, 0.1)
reasoning_steps.append("Estimated days until stock-out.")
df["ReplenishmentQty"] = (BIN_CAPACITY - df["CurrentStock"]).clip(lower=0)
reasoning_steps.append("Calculated replenishment quantity needed.")
df["Risk"] = df["DaysUntilStockout"].apply(
lambda x: "π΄ HIGH" if x < 3 else ("π‘ MEDIUM" if x < 7 else "π’ LOW")
)
reasoning_steps.append("Assigned risk level based on depletion rate.")
explanation = (
"### π Replenishment Analysis\n"
"Replenishment needs evaluated using bin capacity, demand & stock-out timing.\n\n"
"#### π Reasoning\n" + "\n".join([f"- {r}" for r in reasoning_steps])
)
return explanation, df
# ============================================================
# INVENTORY REBALANCING β MODULE 3
# ============================================================
def run_rebalancing_analysis(message, slotting_df):
reasoning = []
df = slotting_df.copy()
if "Frequency" not in df.columns:
return "Frequency missing β cannot rebalance.", None
if "Aisle" not in df.columns:
df["Aisle"] = np.arange(1, len(df) + 1)
reasoning.append("Aisle data missing β assigned aisles automatically.")
velocity_map = {"fast": 3, "medium": 2, "slow": 1}
df["VelScore"] = df["Velocity"].str.lower().map(velocity_map)
df["LoadScore"] = df["VelScore"] * 0.6 + df["Frequency"] * 0.4
reasoning.append("Calculated SKU load score (velocity + frequency).")
aisle_load = df.groupby("Aisle")["LoadScore"].sum().reset_index()
avg_load = aisle_load["LoadScore"].mean()
aisle_load["Congestion"] = aisle_load["LoadScore"].apply(
lambda x: "π΄ High" if x > avg_load * 1.25
else ("π‘ Medium" if x > avg_load * 0.75 else "π’ Low")
)
df = df.merge(aisle_load[["Aisle", "Congestion"]], on="Aisle", how="left")
reasoning.append("Assigned congestion levels to aisles.")
high_aisles = aisle_load[aisle_load["Congestion"] == "π΄ High"]["Aisle"].tolist()
low_aisles = aisle_load[aisle_load["Congestion"] == "π’ Low"]["Aisle"].tolist()
move_plan = []
if high_aisles and low_aisles:
for aisle in high_aisles:
congested_skus = df[df["Aisle"] == aisle].sort_values("LoadScore", ascending=False)
top_to_move = congested_skus.head(2)
for i, row in top_to_move.iterrows():
target_aisle = low_aisles[i % len(low_aisles)]
move_plan.append({
"SKU": row["SKU"],
"FromAisle": row["Aisle"],
"ToAisle": target_aisle,
"LoadScore": round(row["LoadScore"], 2),
"Reason": "Reduce congestion"
})
reasoning.append("Generated SKU redistribution plan.")
else:
reasoning.append("No congestion found β no rebalancing needed.")
move_df = pd.DataFrame(move_plan)
explanation = (
"### π Inventory Rebalancing\n"
"SKU redistribution plan to reduce aisle congestion.\n\n"
"#### π Reasoning\n" + "\n".join([f"- {r}" for r in reasoning])
)
return explanation, move_df
# ============================================================
# WORKFORCE OPTIMIZATION β MODULE 4
# ============================================================
def run_workforce_optimization(message, slotting_df):
reasoning = []
df = slotting_df.copy()
if "Frequency" not in df.columns:
return "Cannot calculate workforce β missing Frequency column.", None
df["Workload"] = df["Frequency"] * 1.2
total_workload = df["Workload"].sum()
workers_needed = max(1, int(total_workload // 150))
reasoning.append(f"Total workload: {total_workload:.2f}")
reasoning.append(f"Workers required (estimated): {workers_needed}")
result = pd.DataFrame({
"Metric": ["Total Workload", "Estimated Workers Needed"],
"Value": [total_workload, workers_needed]
})
explanation = (
"### π· Workforce Optimization\n"
"Estimated staffing requirement based on SKU workload.\n\n"
"#### π Reasoning\n" + "\n".join([f"- {r}" for r in reasoning])
)
return explanation, result
# ============================================================
# DOCK SCHEDULING OPTIMIZATION β MODULE 5
# ============================================================
def run_dock_scheduling(message, slotting_df):
reasoning = []
df = slotting_df.copy()
df["Priority"] = df["Frequency"].rank(ascending=False)
df["AssignedDock"] = df["Priority"].apply(lambda x: int((x - 1) % 3) + 1)
reasoning.append("Assigned SKUs to 3 docks based on priority rank.")
explanation = (
"### π Dock Scheduling Optimization\n"
"SKUs allocated to dock doors based on priority.\n\n"
"#### π Reasoning\n" + "\n".join([f"- {r}" for r in reasoning])
)
return explanation, df[["SKU", "Frequency", "Priority", "AssignedDock"]]
|