Skip to content

Commit c75d734

Browse files
Add AI-based discount suggestion and simulation
Implemented a discount recommendation system using AI and reinforcement learning. Signed-off-by: Gilbert Algordo <69397216+gilbertalgordo@users.noreply.github.com>
1 parent 9eb4100 commit c75d734

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

discount_ai.yaml

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import pandas as pd
2+
import numpy as np
3+
from sklearn.ensemble import RandomForestRegressor
4+
from sklearn.model_selection import train_test_split
5+
6+
# --- STEP 1: GENERATE SYNTHETIC DATA ---
7+
def generate_data(n_samples=1000):
8+
np.random.seed(42)
9+
base_price = np.random.uniform(50, 500, n_samples)
10+
demand = np.random.uniform(10, 100, n_samples)
11+
inventory = np.random.uniform(5, 200, n_samples)
12+
comp_price = base_price * np.random.uniform(0.8, 1.1, n_samples)
13+
14+
# Logic: Higher inventory + Lower demand + Lower comp_price = Higher Discount needed
15+
optimal_discount = (inventory / 200) * 0.2 + (1 - demand / 100) * 0.15
16+
optimal_discount += np.where(comp_price < base_price, 0.1, 0)
17+
optimal_discount = np.clip(optimal_discount, 0, 0.5) # Max 50% discount
18+
19+
df = pd.DataFrame({
20+
'base_price': base_price,
21+
'demand_score': demand,
22+
'inventory_level': inventory,
23+
'competitor_price': comp_price,
24+
'optimal_discount': optimal_discount
25+
})
26+
return df
27+
28+
# --- STEP 2: TRAIN THE AI ---
29+
df = generate_data()
30+
X = df.drop('optimal_discount', axis=1)
31+
y = df['optimal_discount']
32+
33+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
34+
35+
# Using Random Forest for non-linear reasoning
36+
model = RandomForestRegressor(n_estimators=100)
37+
model.fit(X_train, y_train)
38+
39+
# --- STEP 3: PREDICTION FUNCTION ---
40+
def suggest_discount(base, demand, stock, comp):
41+
features = pd.DataFrame([[base, demand, stock, comp]],
42+
columns=['base_price', 'demand_score', 'inventory_level', 'competitor_price'])
43+
prediction = model.predict(features)[0]
44+
return round(prediction * 100, 2)
45+
46+
# Example Usage
47+
current_discount = suggest_discount(199.99, 30, 150, 185.00)
48+
49+
print(f"--- AI DISCOUNT HUD ---")
50+
print(f"Base Price: $199.99 | Competitor: $185.00")
51+
print(f"Demand: Low (30/100) | Stock: High (150 units)")
52+
print(f"------------------------")
53+
print(f"RECOMMENDED DISCOUNT: {current_discount}%")
54+
print(f"NEW SALES PRICE: ${round(199.99 * (1 - current_discount/100), 2)}")
55+
56+
57+
58+
import numpy as np
59+
import pandas as pd
60+
61+
class DiscountAI:
62+
def __init__(self, states_range, actions):
63+
# Actions: 0 = No Discount, 1 = 10% Off, 2 = 25% Off, 3 = 50% Off
64+
self.q_table = np.zeros((states_range, len(actions)))
65+
self.actions = actions
66+
self.learning_rate = 0.1
67+
self.discount_factor = 0.95
68+
self.epsilon = 0.2 # Exploration rate
69+
70+
def get_state(self, inventory, demand):
71+
# Convert continuous variables into discrete 'Instances' for the AI
72+
inv_bin = min(inventory // 20, 4) # 5 levels of inventory
73+
dem_bin = min(demand // 20, 4) # 5 levels of demand
74+
return int(inv_bin * 5 + dem_bin)
75+
76+
def choose_action(self, state):
77+
if np.random.uniform(0, 1) < self.epsilon:
78+
return np.random.choice(len(self.actions)) # Explore
79+
return np.argmax(self.q_table[state]) # Exploit
80+
81+
def update_ai(self, state, action, reward, next_state):
82+
# The Bellman Equation: Scientific reasoning for value iteration
83+
predict = self.q_table[state, action]
84+
target = reward + self.discount_factor * np.max(self.q_table[next_state])
85+
self.q_table[state, action] += self.learning_rate * (target - predict)
86+
87+
# --- SIMULATION INSTANCE ---
88+
ai = DiscountAI(states_range=25, actions=[0, 0.10, 0.25, 0.50])
89+
90+
# Scenario: High Stock (100), Low Demand (10)
91+
current_inv = 100
92+
current_dem = 10
93+
state = ai.get_state(current_inv, current_dem)
94+
95+
# AI Decides Action
96+
action_idx = ai.choose_action(state)
97+
chosen_discount = ai.actions[action_idx]
98+
99+
# Calculate Reward (Logic: High discount on low demand = moving stock, but lower margin)
100+
sales_volume = (current_dem * (1 + chosen_discount * 2))
101+
reward = sales_volume * (1 - chosen_discount)
102+
103+
print(f"--- ADVANCED AI HUD ---")
104+
print(f"Current State ID: {state}")
105+
print(f"AI Decision: {chosen_discount*100}% Discount")
106+
print(f"Projected Reward Score: {round(reward, 2)}")

0 commit comments

Comments
 (0)