Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
# app.py
|
| 2 |
-
# @title Beer Game Final Version (v4.25 - Based on v4.21 Logic + UI Fixes v3
|
| 3 |
|
| 4 |
# -----------------------------------------------------------------------------
|
| 5 |
# 1. Import Libraries
|
|
@@ -121,9 +121,8 @@ def get_llm_order_decision(prompt: str, echelon_name: str) -> (int, str):
|
|
| 121 |
st.error(f"API call failed for {echelon_name}: {e}. Defaulting to 4.")
|
| 122 |
return 4, f"API_ERROR: {e}"
|
| 123 |
|
| 124 |
-
# =============== NEW PROMPT FUNCTION (v2) ===============
|
| 125 |
def get_llm_prompt(echelon_state_decision_point: dict, week: int, llm_personality: str, info_sharing: str, all_echelons_state_decision_point: dict) -> str:
|
| 126 |
-
# This function's logic
|
| 127 |
e_state = echelon_state_decision_point
|
| 128 |
base_info = f"Your Current Status at the **{e_state['name']}** for **Week {week}** (Before Shipping):\n- On-hand inventory: {e_state['inventory']} units.\n- Backlog (total unfilled orders): {e_state['backlog']} units.\n- Incoming order this week (just received): {e_state['incoming_order']} units.\n"
|
| 129 |
if e_state['name'] == 'Factory':
|
|
@@ -132,10 +131,6 @@ def get_llm_prompt(echelon_state_decision_point: dict, week: int, llm_personalit
|
|
| 132 |
else:
|
| 133 |
task_word = "order quantity"
|
| 134 |
base_info += f"- Shipments In Transit To You (arriving next week onwards): {list(e_state['incoming_shipments'])}"
|
| 135 |
-
|
| 136 |
-
# --- PERFECT RATIONAL (NORMATIVE) PROMPTS ---
|
| 137 |
-
# (These prompts are already good and remain unchanged)
|
| 138 |
-
|
| 139 |
if llm_personality == 'perfect_rational' and info_sharing == 'full':
|
| 140 |
stable_demand = 8
|
| 141 |
if e_state['name'] == 'Factory': total_lead_time = FACTORY_LEAD_TIME
|
|
@@ -152,7 +147,6 @@ def get_llm_prompt(echelon_state_decision_point: dict, week: int, llm_personalit
|
|
| 152 |
inv_pos_components = f"(Inv={e_state['inventory']} - Backlog={e_state['backlog']} + InTransitShip={sum(e_state['incoming_shipments'])} + OrderToSupplier={order_in_transit_to_supplier})"
|
| 153 |
optimal_order = max(0, int(target_inventory_level - inventory_position))
|
| 154 |
return f"**You are a perfectly rational supply chain AI with full system visibility.**\nYour only goal is to maintain stability and minimize costs based on mathematical optimization.\n**System Analysis:**\n* **Known Stable End-Customer Demand:** {stable_demand} units/week.\n* **Your Current Total Inventory Position:** {inventory_position} units. {inv_pos_components}\n* **Optimal Target Inventory Level:** {target_inventory_level} units (Target for {total_lead_time} weeks lead time).\n* **Mathematically Optimal {task_word.title()}:** The optimal decision is **{optimal_order} units**.\n**Your Task:** Confirm this optimal {task_word}. Respond with a single integer."
|
| 155 |
-
|
| 156 |
elif llm_personality == 'perfect_rational' and info_sharing == 'local':
|
| 157 |
safety_stock = 4; anchor_demand = e_state['incoming_order']
|
| 158 |
inventory_correction = safety_stock - (e_state['inventory'] - e_state['backlog'])
|
|
@@ -166,78 +160,29 @@ def get_llm_prompt(echelon_state_decision_point: dict, week: int, llm_personalit
|
|
| 166 |
calculated_order = anchor_demand + inventory_correction - supply_line
|
| 167 |
rational_local_order = max(0, int(calculated_order))
|
| 168 |
return f"**You are a perfectly rational supply chain AI with ONLY LOCAL information.**\nYou must use a logical heuristic to make a stable decision. A proven method is \"Anchoring and Adjustment\".\n\n{base_info}\n\n**Rational Calculation (Anchoring & Adjustment):**\n1. **Anchor on Demand:** Your best guess for future demand is your last incoming order: **{anchor_demand} units**.\n2. **Adjust for Inventory:** You want to hold a safety stock of {safety_stock} units. Your current stock (before shipping) is {e_state['inventory'] - e_state['backlog']}. You need to order an extra **{inventory_correction} units** to correct this.\n3. **Account for {supply_line_desc}:** You already have **{supply_line} units** being processed. These should be subtracted from your new decision.\n\n**Final Calculation:**\n* Decision = (Anchor Demand) + (Inventory Adjustment) - ({supply_line_desc})\n* Decision = {anchor_demand} + {inventory_correction} - {supply_line} = **{rational_local_order} units**.\n**Your Task:** Confirm this locally rational {task_word}. Respond with a single integer."
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
if info_sharing == 'local':
|
| 193 |
-
return f"""
|
| 194 |
-
**You are a reactive supply chain manager for the {e_state['name']}.** You have a limited (local) view.
|
| 195 |
-
You tend to make **reactive, 'gut-instinct' decisions** (like the classic Sterman 1989 model) that cause the Bullwhip Effect.
|
| 196 |
-
|
| 197 |
-
{base_info}
|
| 198 |
-
|
| 199 |
-
**Your Flawed 'Human' Heuristic:**
|
| 200 |
-
Your gut tells you to fix your entire inventory problem *right now*, and you're afraid of your backlog.
|
| 201 |
-
A 'rational' player would account for their {supply_line_desc} (which is {supply_line} units), but you're too busy panicking to trust that.
|
| 202 |
-
|
| 203 |
-
**Your 'Panic' Calculation (Ignoring the Supply Line):**
|
| 204 |
-
1. **Anchor on Demand:** You just got an order for **{anchor_demand}** units. You'll order *at least* that.
|
| 205 |
-
2. **Correct for Stock:** Your desired 'safe' inventory is {DESIRED_INVENTORY}. Your current net inventory is {net_inventory}. You need to order **{stock_correction}** more units to feel safe again.
|
| 206 |
-
3. **Ignore Supply Line:** You'll ignore the **{supply_line} units** already in your pipeline.
|
| 207 |
-
|
| 208 |
-
**Final Panic Order:** (Your Incoming Order) + (Your Stock Correction)
|
| 209 |
-
* Order = {panicky_order_calc} = **{panicky_order} units**.
|
| 210 |
-
|
| 211 |
-
**Your Task:** Confirm this 'gut-instinct' {task_word}. Respond with a single integer.
|
| 212 |
-
"""
|
| 213 |
-
|
| 214 |
-
elif info_sharing == 'full':
|
| 215 |
-
# Build the "Full Info" string just for context
|
| 216 |
-
full_info_str = f"\n**Full Supply Chain Information (State Before Shipping):**\n- End-Customer Demand this week: {get_customer_demand(week)} units.\n"
|
| 217 |
-
for name, other_e_state in all_echelons_state_decision_point.items():
|
| 218 |
-
if name != e_state['name']: full_info_str += f"- {name}: Inv={other_e_state['inventory']}, Backlog={other_e_state['backlog']}\n"
|
| 219 |
-
|
| 220 |
-
return f"""
|
| 221 |
-
**You are a supply chain manager ({e_state['name']}) with full system visibility.**
|
| 222 |
-
{base_info}
|
| 223 |
-
{full_info_str}
|
| 224 |
-
|
| 225 |
-
**A "Human-like" Flawed Decision:**
|
| 226 |
-
Even though you have full information, you are judged by *your own* performance (your inventory, your backlog).
|
| 227 |
-
You tend to react to your *local* situation (like the classic Sterman 1989 model) instead of using the complex full-system data.
|
| 228 |
-
A 'rational' player would use the end-customer demand (8) and account for the *entire* system, but your gut-instinct is to panic about *your* numbers.
|
| 229 |
-
|
| 230 |
-
**Your 'Panic' Calculation (Ignoring Full Info and Your Supply Line):**
|
| 231 |
-
1. **Anchor on *Your* Demand:** You just got an order for **{anchor_demand}** units. You react to this, not the end-customer demand.
|
| 232 |
-
2. **Correct for *Your* Stock:** Your desired 'safe' inventory is {DESIRED_INVENTORY}. Your current net inventory is {net_inventory}. You need to order **{stock_correction}** more units.
|
| 233 |
-
3. **Ignore *Your* Supply Line:** You'll ignore the **{supply_line} units** in your own pipeline ({supply_line_desc}).
|
| 234 |
-
|
| 235 |
-
**Final Panic Order:** (Your Incoming Order) + (Your Stock Correction)
|
| 236 |
-
* Order = {panicky_order_calc} = **{panicky_order} units**.
|
| 237 |
-
|
| 238 |
-
**Your Task:** Confirm this 'gut-instinct', locally-focused {task_word}. Respond with a single integer.
|
| 239 |
-
"""
|
| 240 |
-
# =========================================================
|
| 241 |
|
| 242 |
def step_game(human_final_order: int, human_initial_order: int, ai_suggestion: int):
|
| 243 |
# This is the correct logic from v4.17
|
|
@@ -260,8 +205,8 @@ def step_game(human_final_order: int, human_initial_order: int, ai_suggestion: i
|
|
| 260 |
factory_q = state['factory_production_pipeline']
|
| 261 |
if factory_q:
|
| 262 |
arrived_this_week["Factory"] = factory_q[0] # Read before pop
|
| 263 |
-
|
| 264 |
-
|
| 265 |
|
| 266 |
# R, W, D
|
| 267 |
for name in ["Retailer", "Wholesaler", "Distributor"]:
|
|
@@ -291,10 +236,8 @@ def step_game(human_final_order: int, human_initial_order: int, ai_suggestion: i
|
|
| 291 |
|
| 292 |
for name in ["Retailer", "Wholesaler", "Distributor"]:
|
| 293 |
arrived_shipment = 0
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
if echelons[name]['incoming_shipments']: # Still need to pop it, even if we captured the value
|
| 297 |
-
echelons[name]['incoming_shipments'].popleft()
|
| 298 |
inventory_after_arrival[name] = echelons[name]['inventory'] + arrived_shipment
|
| 299 |
|
| 300 |
# (Rest of game logic)
|
|
|
|
| 1 |
# app.py
|
| 2 |
+
# @title Beer Game Final Version (v4.25 - Based on v4.21 Logic + UI Fixes v3 - Log Fix)
|
| 3 |
|
| 4 |
# -----------------------------------------------------------------------------
|
| 5 |
# 1. Import Libraries
|
|
|
|
| 121 |
st.error(f"API call failed for {echelon_name}: {e}. Defaulting to 4.")
|
| 122 |
return 4, f"API_ERROR: {e}"
|
| 123 |
|
|
|
|
| 124 |
def get_llm_prompt(echelon_state_decision_point: dict, week: int, llm_personality: str, info_sharing: str, all_echelons_state_decision_point: dict) -> str:
|
| 125 |
+
# This function's logic remains correct (from v4.21).
|
| 126 |
e_state = echelon_state_decision_point
|
| 127 |
base_info = f"Your Current Status at the **{e_state['name']}** for **Week {week}** (Before Shipping):\n- On-hand inventory: {e_state['inventory']} units.\n- Backlog (total unfilled orders): {e_state['backlog']} units.\n- Incoming order this week (just received): {e_state['incoming_order']} units.\n"
|
| 128 |
if e_state['name'] == 'Factory':
|
|
|
|
| 131 |
else:
|
| 132 |
task_word = "order quantity"
|
| 133 |
base_info += f"- Shipments In Transit To You (arriving next week onwards): {list(e_state['incoming_shipments'])}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
if llm_personality == 'perfect_rational' and info_sharing == 'full':
|
| 135 |
stable_demand = 8
|
| 136 |
if e_state['name'] == 'Factory': total_lead_time = FACTORY_LEAD_TIME
|
|
|
|
| 147 |
inv_pos_components = f"(Inv={e_state['inventory']} - Backlog={e_state['backlog']} + InTransitShip={sum(e_state['incoming_shipments'])} + OrderToSupplier={order_in_transit_to_supplier})"
|
| 148 |
optimal_order = max(0, int(target_inventory_level - inventory_position))
|
| 149 |
return f"**You are a perfectly rational supply chain AI with full system visibility.**\nYour only goal is to maintain stability and minimize costs based on mathematical optimization.\n**System Analysis:**\n* **Known Stable End-Customer Demand:** {stable_demand} units/week.\n* **Your Current Total Inventory Position:** {inventory_position} units. {inv_pos_components}\n* **Optimal Target Inventory Level:** {target_inventory_level} units (Target for {total_lead_time} weeks lead time).\n* **Mathematically Optimal {task_word.title()}:** The optimal decision is **{optimal_order} units**.\n**Your Task:** Confirm this optimal {task_word}. Respond with a single integer."
|
|
|
|
| 150 |
elif llm_personality == 'perfect_rational' and info_sharing == 'local':
|
| 151 |
safety_stock = 4; anchor_demand = e_state['incoming_order']
|
| 152 |
inventory_correction = safety_stock - (e_state['inventory'] - e_state['backlog'])
|
|
|
|
| 160 |
calculated_order = anchor_demand + inventory_correction - supply_line
|
| 161 |
rational_local_order = max(0, int(calculated_order))
|
| 162 |
return f"**You are a perfectly rational supply chain AI with ONLY LOCAL information.**\nYou must use a logical heuristic to make a stable decision. A proven method is \"Anchoring and Adjustment\".\n\n{base_info}\n\n**Rational Calculation (Anchoring & Adjustment):**\n1. **Anchor on Demand:** Your best guess for future demand is your last incoming order: **{anchor_demand} units**.\n2. **Adjust for Inventory:** You want to hold a safety stock of {safety_stock} units. Your current stock (before shipping) is {e_state['inventory'] - e_state['backlog']}. You need to order an extra **{inventory_correction} units** to correct this.\n3. **Account for {supply_line_desc}:** You already have **{supply_line} units** being processed. These should be subtracted from your new decision.\n\n**Final Calculation:**\n* Decision = (Anchor Demand) + (Inventory Adjustment) - ({supply_line_desc})\n* Decision = {anchor_demand} + {inventory_correction} - {supply_line} = **{rational_local_order} units**.\n**Your Task:** Confirm this locally rational {task_word}. Respond with a single integer."
|
| 163 |
+
elif llm_personality == 'human_like' and info_sharing == 'full':
|
| 164 |
+
full_info_str = f"\n**Full Supply Chain Information (State Before Shipping):**\n- End-Customer Demand this week: {get_customer_demand(week)} units.\n"
|
| 165 |
+
for name, other_e_state in all_echelons_state_decision_point.items():
|
| 166 |
+
if name != e_state['name']: full_info_str += f"- {name}: Inv={other_e_state['inventory']}, Backlog={other_e_state['backlog']}\n"
|
| 167 |
+
return f"""
|
| 168 |
+
**You are a supply chain manager ({e_state['name']}) with full system visibility.**
|
| 169 |
+
You can see everyone's current inventory and backlog before shipping, and the real customer demand.
|
| 170 |
+
{base_info}
|
| 171 |
+
{full_info_str}
|
| 172 |
+
**Your Task:** Your primary responsibility is to meet the demand from your direct customer (your `Incoming order this week`: **{e_state['incoming_order']}** units), which contributes to your total current backlog of {e_state['backlog']}.
|
| 173 |
+
While you can see the stable end-customer demand ({get_customer_demand(week)} units), your priority is to fulfill the order you just received and manage your inventory/backlog.
|
| 174 |
+
You are still human and might get anxious about your own stock levels.
|
| 175 |
+
What {task_word} should you decide on this week? Respond with a single integer.
|
| 176 |
+
"""
|
| 177 |
+
elif llm_personality == 'human_like' and info_sharing == 'local':
|
| 178 |
+
return f"""
|
| 179 |
+
**You are a reactive supply chain manager for the {e_state['name']}.** You have a limited view and tend to over-correct based on fear.
|
| 180 |
+
Your top priority is to NOT have a backlog.
|
| 181 |
+
{base_info}
|
| 182 |
+
**Your Task:** You just received an incoming order for **{e_state['incoming_order']}** units, adding to your total backlog.
|
| 183 |
+
Your gut instinct is to panic and {task_word.split(' ')[0]} enough to ensure you are never caught with a backlog again, considering your current inventory.
|
| 184 |
+
**React emotionally.** What is your knee-jerk {task_word}? Respond with a single integer.
|
| 185 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
def step_game(human_final_order: int, human_initial_order: int, ai_suggestion: int):
|
| 188 |
# This is the correct logic from v4.17
|
|
|
|
| 205 |
factory_q = state['factory_production_pipeline']
|
| 206 |
if factory_q:
|
| 207 |
arrived_this_week["Factory"] = factory_q[0] # Read before pop
|
| 208 |
+
if len(factory_q) > 1:
|
| 209 |
+
opening_arriving_next_week_UI_VALUE["Factory"] = factory_q[1] # Read [1] before pop
|
| 210 |
|
| 211 |
# R, W, D
|
| 212 |
for name in ["Retailer", "Wholesaler", "Distributor"]:
|
|
|
|
| 236 |
|
| 237 |
for name in ["Retailer", "Wholesaler", "Distributor"]:
|
| 238 |
arrived_shipment = 0
|
| 239 |
+
if echelons[name]['incoming_shipments']:
|
| 240 |
+
arrived_shipment = echelons[name]['incoming_shipments'].popleft()
|
|
|
|
|
|
|
| 241 |
inventory_after_arrival[name] = echelons[name]['inventory'] + arrived_shipment
|
| 242 |
|
| 243 |
# (Rest of game logic)
|