Step 1: Import Libraries
python
Copy code
import numpy as np
import pandas as pd
import [Link] as plt
from [Link] import MinMaxScaler
import random
Step 2: Simulate Environment
This simulation models demand based on price, competitor price, and seasonal factors.
python
Copy code
class PricingEnvironment:
def __init__(self, price_range, competitor_prices, seasonal_effects):
self.price_range = price_range
self.competitor_prices = competitor_prices
self.seasonal_effects = seasonal_effects
self.state_space = len(self.price_range)
def reset(self):
# Reset environment state
self.current_season = [Link](self.seasonal_effects.keys())
self.current_competitor_price = [Link](self.competitor_prices)
return self.get_state()
def get_state(self):
# Combine season and competitor price into a state
return (self.current_season, self.current_competitor_price)
def step(self, action):
price = self.price_range[action]
seasonality = self.seasonal_effects[self.current_season]
# Calculate demand (simplified for simulation)
demand = max(0, 100 - price + 10 * seasonality - 0.5 * (price - self.current_competitor_price))
# Revenue as reward
revenue = price * demand
# Transition to a new state
self.current_season = [Link](list(self.seasonal_effects.keys()))
self.current_competitor_price = [Link](self.competitor_prices)
return self.get_state(), revenue
Step 3: Define Q-Learning Agent
python
Copy code
class QLearningAgent:
def __init__(self, state_space, action_space, learning_rate=0.1, discount_factor=0.9, epsilon=0.2):
self.state_space = state_space
self.action_space = action_space
self.q_table = [Link]((state_space, action_space))
self.learning_rate = learning_rate
self.discount_factor = discount_factor
[Link] = epsilon
def choose_action(self, state):
if [Link]() < [Link]: # Exploration
return [Link](self.action_space)
else: # Exploitation
return [Link](self.q_table[state])
def update(self, state, action, reward, next_state):
best_next_action = [Link](self.q_table[next_state])
td_target = reward + self.discount_factor * self.q_table[next_state, best_next_action]
td_error = td_target - self.q_table[state, action]
self.q_table[state, action] += self.learning_rate * td_error
Step 4: Train the Model
python
Copy code
# Define price range, competitor prices, and seasonality
price_range = [Link](50, 150, 11)
competitor_prices = [60, 70, 80, 90, 100]
seasonal_effects = {"low": -1, "medium": 0, "high": 1}
# Initialize environment and agent
env = PricingEnvironment(price_range, competitor_prices, seasonal_effects)
state_space = len(seasonal_effects) * len(competitor_prices)
action_space = len(price_range)
agent = QLearningAgent(state_space, action_space)
# Training parameters
episodes = 5000
rewards_per_episode = []
# State encoding
state_map = {state: idx for idx, state in enumerate([(s, p) for s in seasonal_effects.keys() for p in
competitor_prices])}
# Train agent
for episode in range(episodes):
state = state_map[[Link]()]
total_reward = 0
for step in range(10): # Steps per episode
action = agent.choose_action(state)
next_state_raw, reward = [Link](action)
next_state = state_map[next_state_raw]
[Link](state, action, reward, next_state)
state = next_state
total_reward += reward
rewards_per_episode.append(total_reward)
# Plot training rewards
[Link](rewards_per_episode)
[Link]("Episode")
[Link]("Total Reward")
[Link]("Training Progress")
[Link]()
Step 5: Evaluate the Model
python
Copy code
# Test the trained agent
test_episodes = 100
test_rewards = []
for episode in range(test_episodes):
state = state_map[[Link]()]
total_reward = 0
for step in range(10):
action = [Link](agent.q_table[state]) # Always exploit in evaluation
next_state_raw, reward = [Link](action)
next_state = state_map[next_state_raw]
state = next_state
total_reward += reward
test_rewards.append(total_reward)
print(f"Average Test Reward: {[Link](test_rewards):.2f}")
Step 6: Save and Load the Model
python
Copy code
# Save Q-table
[Link]("q_table.npy", agent.q_table)
# Load Q-table
loaded_q_table = [Link]("q_table.npy")
agent.q_table = loaded_q_table
Project Enhancements
1. Add Inventory Constraints: Limit actions based on stock availability.
2. Expand State Space: Include features like customer segments or time of day.
3. Upgrade to DQN: Replace Q-table with a neural network using libraries like PyTorch or
TensorFlow.
4. Real Data: Replace the simulated environment with real e-commerce data.
This code provides a strong foundation for developing and experimenting with dynamic pricing
models using RL. Let me know if you'd like further elaboration or an upgrade to DQN!