Skip to content

An interactive F1 race simulator using Reinforcement Learning (PPO, Q-learning) and Streamlit to optimize pit stop strategies based on dynamic conditions.

Notifications You must be signed in to change notification settings

rembertdesigns/pit-stop-simulator

Repository files navigation

🏎️ F1 Pit Stop Strategy Simulator

Launch Simulator

F1 Pit Stop Strategy Simulator - AI-powered Formula 1 race strategy optimization tool with reinforcement learning for tire management and pit timing decisions

An advanced, interactive Formula 1 pit stop strategy simulator leveraging Reinforcement Learning and Machine Learning to optimize race strategies through dynamic simulation. This project demonstrates AI-driven decision-making for pit stop timing and tire compound selection in realistic race environments.


πŸ“‹ Table of Contents


🎯 Overview

F1 Pit Stop Strategy Simulator Overview - AI-powered Formula 1 race strategy optimization with reinforcement learning for tire management and pit timing

This AI-powered F1 pit stop simulator creates a high-fidelity Formula 1 race environment where reinforcement learning agents optimize pit stop timing and tire strategy decisions. The system models realistic race dynamics using machine learning to predict optimal compound selection and pit windows.

🏎️ Race Dynamics Simulation

Tire Degradation Models - Compound-specific wear algorithms:

  • Soft Compound: Maximum grip, aggressive degradation (1.5Γ— base wear rate)
  • Medium Compound: Balanced performance and longevity (1.0Γ— base wear rate)
  • Hard Compound: Extended stint capability (0.7Γ— base wear rate)
  • Wet/Intermediate: Dynamic performance based on track conditions

🌦️ Environmental Simulation

Weather & Track Conditions:

  • Probabilistic rain forecast system with intensity modeling
  • Track temperature simulation (20-50Β°C operational range)
  • Dynamic grip evolution (rubber buildup, track cleaning effects)

⚠️ Race Incident Modeling

Safety Protocols:

  • Safety Car (SC) deployments with configurable duration
  • Virtual Safety Car (VSC) period simulation
  • Strategic pit window optimization during caution phases

πŸ“Š Performance Variables

Realistic Race Physics:

  • Traffic simulation with time penalties (1-10 seconds per lap)
  • Fuel load effects on lap times (~0.03s per kilogram)
  • FIA technical regulation compliance (mandatory tire compound rules)

⬆ Back to Table of Contents


🧠 Core Innovation

Core Innovation - Dual-Agent Reinforcement Learning Architecture for F1 Pit Stop Strategy Optimization using Q-Learning and PPO algorithms

Dual-Agent Architecture

This F1 strategy simulator implements two complementary AI racing agents using cutting-edge reinforcement learning algorithms for optimal pit stop decision-making.

1️⃣ Q-Learning Agent - Tabular Reinforcement Learning

State Space Representation (6-Dimensional Discretization)

State = (
    lap_bucket,           # 0-3 (race phase)
    tire_wear_bucket,     # 0-9 (0-100% in 10% increments)
    traffic_bucket,       # 0-2 (low/medium/high)
    rain_status,          # 0-1 (binary)
    safety_car_status,    # 0-1 (binary)
    vsc_status           # 0-1 (binary)
)

Action Space - 6 Discrete Pit Stop Strategies

  • Action 0: Continue racing (no pit stop)
  • Actions 1-5: Execute pit stop with tire selection:
    • Soft compound (maximum grip)
    • Medium compound (balanced)
    • Hard compound (durability)
    • Intermediate (light rain)
    • Wet (heavy rain)

Q-Learning Hyperparameters

Parameter Value Purpose
Learning Rate (Ξ±) 0.1 Q-value update step size
Discount Factor (Ξ³) 0.99 Future reward importance
Exploration Rate (Ξ΅) 1.0 β†’ 0.01 Decay over 2000 episodes

2️⃣ PPO Agent - Deep Reinforcement Learning

Framework: Stable-Baselines3 Proximal Policy Optimization (PyTorch backend)

Continuous Observation Space (7 Features)

obs = [
    current_lap / total_laps,        # Normalized race progress
    tire_wear / 100.0,               # Normalized tire condition
    traffic,                         # Traffic intensity [0, 1]
    fuel_weight / initial_fuel,      # Normalized fuel load
    rain_intensity,                  # Rain strength [0, 1]
    safety_car_active,               # Binary flag
    vsc_active                       # Binary flag
]

Neural Network Architecture

Policy Network (Actor):

  • Multi-Layer Perceptron (MLP)
  • 2 hidden layers Γ— 64 neurons
  • Tanh activation function
  • Outputs: Probability distribution over 6 actions

Value Network (Critic):

  • Shared feature extraction with policy network
  • Estimates state value function V(s)

PPO Training Configuration

Hyperparameter Value Description
Total Timesteps 300,000 Training iterations
Batch Size 64 Samples per gradient update
Learning Rate 2.5e-4 Adam optimizer step size
GAE Lambda (Ξ») 0.95 Advantage estimation decay
Clip Range 0.2 Policy update constraint (PPO)

πŸ†š Agent Comparison

Feature Q-Learning PPO
State Space Discrete (6D buckets) Continuous (7D vector)
Policy Type Tabular lookup Neural network
Training Speed Fast (minutes) Moderate (hours)
Generalization Limited to seen states Excellent extrapolation
Best For Well-defined scenarios Complex, variable conditions

⬆ Back to Table of Contents


✨ Key Features

Key Features - AI-Powered F1 Race Simulation with Q-Learning and PPO Agents, Real-Time Analytics, and Machine Learning Predictions

πŸ€– AI Strategy Agents - Dual Reinforcement Learning System

Q-Learning Agent - Fast Tabular RL

  • Lightning-fast inference: ~0.1ms per pit stop decision
  • Interpretable AI: Clear state-action mapping for strategy analysis
  • Specialized agents: 15 pre-trained models (5 F1 teams Γ— 3 driver profiles)
  • Optimal use case: Discrete, well-defined race scenarios

PPO Agent - Advanced Deep Learning

  • Continuous learning: Neural network adapts to novel race conditions
  • Superior generalization: Handles unseen weather patterns and traffic scenarios
  • Universal model: Single agent adapts to all team/driver combinations
  • State-of-the-art: Proximal Policy Optimization (Stable-Baselines3)

Performance Comparison Tools

  • Head-to-Head mode: Direct Q-Learning vs PPO agent benchmarking
  • Statistical rigor: Analysis over 100+ race simulations per strategy
  • Visual analytics: Distribution plots for race times and pit stop frequency

🏁 Race Simulation Engine - Complete F1 Weekend Experience

Session Types - Full Motorsport Simulation

  1. Practice Session: 3 stints Γ— 3 laps with randomized tire compound testing
  2. Qualifying Session: 3 flying laps, best-time recording (Q1/Q2/Q3 format)
  3. Race Session: Full distance (20-80 laps) with strategic pit stop windows
  4. Full Weekend Mode: Complete Practice β†’ Qualifying β†’ Race progression
  5. Statistical Comparison: Batch simulations (10-100 runs) for strategy validation

Circuit Library - 9 Authentic F1 Tracks

Realistic track characteristics with accurate pit times and tire wear rates:

Track Pit Loss Tire Wear Traffic Penalty Base Lap Time
Monza (Italy) 28s 1.1Γ— 3.0s 80.0s
Spa-Francorchamps (Belgium) 32s 1.2Γ— 4.0s 105.0s
Monaco (Monte Carlo) 25s 1.4Γ— 7.5s 71.0s
Bahrain (Sakhir) 30s 2.0Γ— 5.0s 92.0s
Silverstone (UK) 29s 1.8Γ— 4.5s 88.0s

Additional tracks: Austin (COTA), Suzuka, Singapore, Interlagos

πŸ“Š Analytics Dashboard - Professional Race Telemetry

Real-Time Visualizations (Plotly Interactive Charts)

  • Animated lap-by-lap metrics: Adjustable replay speed (0.001-0.5s intervals)
  • Tire degradation tracking: Rolling average smoothing for trend analysis
  • Traffic intensity heatmaps: Visual representation of congestion patterns
  • Fuel consumption curves: Weight-adjusted performance modeling

Post-Race Analysis Tools

  • Lap time delta charts: Performance vs. baseline (first valid lap reference)
  • Strategic event timeline: Visual race narrative with emoji markers:
    • πŸ…ΏοΈ Pit stops
    • 🌧️ Weather changes
    • ⚠️ Safety Car deployments
    • 🚦 VSC periods
  • Tire compound distribution: Usage statistics across race distance
  • Environmental tracking: Track temperature and grip factor evolution

ML Insights - Predictive Analytics

  • RandomForest predictions: AI-forecasted vs. actual lap times comparison
  • Feature importance rankings: Identify key performance drivers
  • Error analysis: RMSE and RΒ² model performance metrics

🧠 Machine Learning Pipeline - Predictive Lap Time Model

Lap Time Predictor Specifications

Model Architecture:

  • Algorithm: RandomForestRegressor with 150 decision trees
  • Input features: 14-dimensional feature vector
    • 7 numeric features (tire wear, fuel load, traffic, etc.)
    • 5 tire compound dummies (one-hot encoded)
    • 2 boolean flags (weather, safety car status)

Training Configuration:

  • Data source: Aggregated simulation logs from race history
  • Performance benchmarks:
    • RΒ² Score: >0.85 (85%+ variance explained)
    • RMSE: <2.0 seconds (prediction accuracy)

Use Cases:

  • Pre-race strategy optimization
  • In-race lap time forecasting
  • Post-race performance analysis

⬆ Back to Table of Contents


πŸ› οΈ Technical Architecture

Technical Architecture - F1 Pit Stop Simulator Technology Stack with Python, Streamlit, PyTorch, and Stable-Baselines3 for AI Racing Strategy

Technology Stack - Production-Grade AI Racing Platform

F1 Pit Stop Simulator Technology Stack Diagram showing Python ecosystem with Streamlit web framework, Gymnasium RL environment, Stable-Baselines3 PPO agents, PyTorch neural networks, Scikit-learn ML models, and Plotly data visualization

Core Dependencies - Enterprise Python Stack

Foundation & Framework Layer

Package Version Purpose Key Features
Python 3.10+ Base runtime environment Type hints, pattern matching, async support
Streamlit 1.28+ Interactive web application framework Real-time data visualization, responsive UI

Reinforcement Learning Infrastructure

Package Version Purpose Key Features
Gymnasium 0.29+ OpenAI Gym successor for RL environments Standardized API, action/observation spaces
Stable-Baselines3 2.1+ State-of-the-art RL algorithms PPO, DQN, SAC implementations
PyTorch 2.0+ Deep learning backend GPU acceleration, automatic differentiation

Machine Learning & Analytics

Package Version Purpose Key Features
Scikit-learn 1.3+ Classical ML algorithms RandomForest, preprocessing pipelines
Plotly 5.17+ Interactive data visualization Animated charts, 3D plots, responsive design
NumPy 1.24+ Numerical computing library Array operations, linear algebra, statistics
Pandas 2.0+ Data manipulation and analysis DataFrame operations, CSV handling, time series

Architecture Highlights

πŸš€ Performance Optimizations:

  • PyTorch GPU acceleration for neural network training
  • Vectorized NumPy operations for simulation speed
  • Streamlit caching for responsive user experience

πŸ”§ Development Stack:

  • Python 3.10+ for modern language features
  • Type hints for code quality and IDE support
  • Modular architecture for easy testing and maintenance

πŸ“Š Data Pipeline:

  1. Simulation Engine β†’ Gymnasium environment
  2. RL Training β†’ Stable-Baselines3 PPO/Q-Learning
  3. ML Prediction β†’ Scikit-learn RandomForest
  4. Visualization β†’ Plotly interactive charts
  5. Web Interface β†’ Streamlit dashboard

⬆ Back to Table of Contents


πŸš€ Installation

Installation Guide - Step-by-Step Setup for F1 Pit Stop Simulator with Python 3.10+, Git, and Machine Learning Dependencies

Prerequisites - System Requirements

Before installing the F1 Pit Stop Simulator, ensure your system meets these requirements:

Software Requirements:

  • Python 3.10 or newer (3.11+ recommended for performance)
  • Git version control system
  • pip package manager (included with Python)

Hardware Requirements:

  • Storage: ~2GB disk space (models + dependencies)
  • RAM: 4GB+ recommended for PPO training (8GB+ optimal)
  • GPU: Optional (CUDA-compatible for faster training)

Quick Setup - 5-Minute Installation

Step 1: Clone Repository from GitHub

Download the F1 simulator source code:

git clone https://github.com/rembertdesigns/pit-stop-simulator.git
cd pit-stop-simulator

Alternative: Download ZIP from GitHub releases

Step 2: Create Virtual Environment (Recommended)

Isolate project dependencies using Python's venv:

macOS / Linux:

python3 -m venv venv
source venv/bin/activate

Windows:

python -m venv venv
venv\Scripts\activate

Why virtual environments? Prevents dependency conflicts and ensures reproducibility.

Step 3: Install Python Dependencies

Install all required packages via pip:

pip install --upgrade pip
pip install -r requirements.txt

What gets installed:

  • Streamlit (web framework)
  • Stable-Baselines3 (RL algorithms)
  • PyTorch (deep learning)
  • Gymnasium (RL environments)
  • Plotly, NumPy, Pandas (analytics)

Installation time: ~2-5 minutes depending on internet speed

Step 4: Verify Installation

Test that all critical dependencies are installed correctly:

python -c "import streamlit; import gymnasium; import stable_baselines3; print('βœ… All dependencies installed')"

Expected output:

βœ… All dependencies installed

Next Steps

After successful installation:

  1. Launch the simulator: streamlit run streamlit_app.py
  2. Train models: Follow the Model Training Pipeline guide
  3. Run your first race: See the Usage Guide

Troubleshooting Common Issues

Issue: ModuleNotFoundError: No module named 'torch'
Solution: Reinstall PyTorch: pip install torch --upgrade

Issue: Installation fails on Windows
Solution: Install Microsoft Visual C++ Build Tools

Issue: Out of memory during training
Solution: Reduce batch size in train_ppo.py or use Q-Learning agents

Need help? Open an issue on GitHub

⬆ Back to Table of Contents


πŸŽ“ Model Training Pipeline

Model Training Pipeline - Complete Guide for Training Q-Learning and PPO Agents for F1 Pit Stop Strategy Optimization with Python Machine Learning

⚠️ Important Notice

Pre-trained models are not included in the repository. You must train them yourself using the provided scripts.

Training Workflow - 4-Step ML Pipeline

1. Generate Data β†’ 2. Train ML Predictor β†’ 3. Train Q-Agents β†’ 4. Train PPO Agent

Step 1: Generate Initial Data

Run 2-3 race simulations to create training data:

streamlit run streamlit_app.py

Action Items:

  • Navigate to the app (http://localhost:8501)
  • Configure race settings (50+ laps recommended)
  • Run simulations with varied conditions (rain, SC, different tracks)
  • Verify logs/gym_race_lap_data.csv contains data (should have 100+ rows)

Step 2: Train Lap Time Predictor

python train_lap_model.py

What This Does:

  • Loads data from logs/gym_race_lap_data.csv
  • Preprocesses features (one-hot encoding, normalization)
  • Trains RandomForestRegressor
  • Saves model to models/lap_time_predictor.pkl
  • Prints evaluation metrics (RMSE, RΒ², feature importances)

Expected Output:

Training set size: 800 samples, Test set size: 200 samples
Training RandomForestRegressor model with 14 features...
Model training complete.

--- Model Evaluation on Test Set ---
  Root Mean Squared Error (RMSE): 1.847
  R-squared (R2 Score):           0.891

βœ… Model retrained and saved to: models/lap_time_predictor.pkl

Step 3: Train Q-Learning Agents

python main.py

Configuration (in main.py):

TEAMS_TO_TRAIN = ["Ferrari", "Red Bull", "Mercedes", "McLaren", "Aston Martin"]
PROFILES_TO_TRAIN = ["Aggressive", "Balanced", "Conservative"]
TRAINING_EPISODES = 2000

What This Does:

  • Creates 15 agents (5 teams Γ— 3 profiles)
  • Trains each agent for 2000 episodes (~30 min on modern CPU)
  • Saves agents to saved_agents/{Team}_{Profile}_q.pkl
  • Generates training plots in training_figures/

Expected Output (per agent):

--- Initializing training for: Ferrari - Aggressive ---
Training Ferrari Aggressive: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 2000/2000 [05:42<00:00, 5.84it/s]
--- Training for Ferrari - Aggressive complete ---
βœ… Agent successfully saved to: saved_agents/Ferrari_Aggressive_q.pkl

Step 4: Train PPO Agent

python train_ppo.py

What This Does:

  • Creates vectorized training environment
  • Trains PPO agent for 300,000 timesteps (~2-3 hours on CPU, ~30 min on GPU)
  • Saves checkpoints every 10,000 steps
  • Evaluates and saves best model
  • Final model saved to models/ppo_pit_stop.zip

Expected Output:

Creating training environment...
PPO Model Created. Observation Space: Box(7,), Action Space: Discrete(6)
Starting PPO training for 300000 timesteps...
---------------------------------
| rollout/           |          |
|    ep_len_mean     | 58.0     |
|    ep_rew_mean     | -5247.32 |
| time/              |          |
|    fps             | 1247     |
|    iterations      | 146      |
|    time_elapsed    | 240      |
|    total_timesteps | 299008   |
...
βœ… Training complete. Final model saved to: models/ppo_pit_stop.zip

Verifying Training Success

After training, verify all models exist:

ls -lh models/
# Should show:
# lap_time_predictor.pkl
# ppo_pit_stop.zip

ls -lh saved_agents/
# Should show 15 .pkl files for Q-agents

⬆ Back to Table of Contents


πŸ“– Usage Guide

Usage Guide - How to Use F1 Pit Stop Simulator with Q-Learning and PPO AI Agents for Racing Strategy Optimization and Analysis

Launch Application

bashstreamlit run streamlit_app.py

Access at: http://localhost:8501

Basic Simulation Workflow

  1. Select Strategy (Sidebar)

    • Q-Learning: Uses trained Q-table agent
    • PPO: Uses neural network policy
    • Custom: Manual pit lap selection
  2. Configure Race Parameters

    • Total Laps: 20-80 (default: 58)
    • Track: Select from 9 circuits or define custom
    • Team & Driver Profile: Affects pit thresholds and tire wear
  3. Add Race Events (Optional)

    • Rain Forecast: Define probability windows
    • Safety Car: Select specific laps
    • Initial Tire: Soft/Medium/Hard
  4. Run Simulation

    • Click "▢️ Start Simulation"
    • Watch animated lap metrics
    • Review post-race analytics
  5. Analyze Results

    • Lap time deltas
    • Strategic event timeline
    • ML predictions vs. actual
    • Download PDF report

Advanced Features

Head-to-Head Mode

Compare Q-Learning vs. PPO directly:

  • Session Type: "Head-to-Head"
  • Side-by-side race visualization
  • Comparative summary table

Statistical Comparison

Robust strategy evaluation:

  • Session Type: "Statistical Comparison"
  • Select 2-3 strategies
  • Configure 10-100 runs per strategy
  • View distribution plots (box plots, histograms)

Full Weekend Mode

Experience P β†’ Q β†’ R progression:

  • Session Type: "Full Weekend"
  • Simulates Practice (9 laps), Qualifying (3 laps), Race (full distance)
  • Carries strategic insights between sessions

⬆ Back to Table of Contents


πŸ“ Project Structure

Project Structure - Complete F1 Pit Stop Simulator Codebase Architecture with Python Scripts, Machine Learning Models, and Training Pipelines

Repository Organization - Modular Python Architecture

pit-stop-simulator/
β”‚
β”œβ”€β”€ streamlit_app.py              # πŸ–₯️  Main web application (3500+ lines)
β”‚   β”œβ”€β”€ Model downloader (Hugging Face Hub integration)
β”‚   β”œβ”€β”€ Simulation orchestration engine
β”‚   β”œβ”€β”€ Plotly interactive visualizations
β”‚   └── PDF race report generation
β”‚
β”œβ”€β”€ env/
β”‚   └── gym_race_env.py           # 🏁 Gymnasium-compliant F1 environment
β”‚       β”œβ”€β”€ State space definition (7D observation vector)
β”‚       β”œβ”€β”€ Reward function (lap time optimization + penalties)
β”‚       β”œβ”€β”€ Weather/Safety Car/VSC simulation logic
β”‚       └── Tire degradation & fuel consumption models
β”‚
β”œβ”€β”€ rl/
β”‚   └── q_learning_agent.py       # 🧠 Tabular Q-Learning implementation
β”‚       β”œβ”€β”€ State discretization (6D continuous β†’ discrete buckets)
β”‚       β”œβ”€β”€ Epsilon-greedy exploration strategy
β”‚       └── Q-table updates (Bellman equation solver)
β”‚
β”œβ”€β”€ train_ppo.py                  # πŸš‚ PPO agent training script
β”‚   β”œβ”€β”€ Vectorized environment setup (parallel simulation)
β”‚   β”œβ”€β”€ Hyperparameter configuration (learning rate, batch size)
β”‚   β”œβ”€β”€ Checkpoint/evaluation callbacks (auto-save best model)
β”‚   └── TensorBoard logging integration
β”‚
β”œβ”€β”€ train_lap_model.py            # πŸ“ˆ Machine learning lap time predictor
β”‚   β”œβ”€β”€ CSV data loading & preprocessing pipeline
β”‚   β”œβ”€β”€ RandomForest regressor training (150 trees)
β”‚   β”œβ”€β”€ Feature engineering (one-hot encoding, normalization)
β”‚   └── Model evaluation metrics (RMSE, RΒ², feature importance)
β”‚
β”œβ”€β”€ main.py                       # 🎯 Batch Q-Learning agent training
β”‚   β”œβ”€β”€ Multi-agent training loop (15 agents: 5 teams Γ— 3 profiles)
β”‚   β”œβ”€β”€ Progress tracking with tqdm library
β”‚   β”œβ”€β”€ Training visualization (reward curves, Q-value heatmaps)
β”‚   └── Agent persistence using pickle serialization
β”‚
β”œβ”€β”€ ppo_eval.py                   # πŸ“Š PPO agent evaluation script
β”‚   β”œβ”€β”€ Deterministic policy rollouts (no exploration)
β”‚   β”œβ”€β”€ Detailed lap-by-lap performance logging
β”‚   └── Episode statistics aggregation (mean, std, min, max)
β”‚
β”œβ”€β”€ models/                       # πŸ€– Trained ML models (auto-downloaded)
β”‚   β”œβ”€β”€ ppo_pit_stop.zip          # PPO neural network policy (PyTorch)
β”‚   └── lap_time_predictor.pkl    # RandomForest regressor (Scikit-learn)
β”‚
β”œβ”€β”€ saved_agents/                 # πŸ’Ύ Q-Learning agent collection
β”‚   β”œβ”€β”€ Ferrari_Aggressive_q.pkl
β”‚   β”œβ”€β”€ Mercedes_Balanced_q.pkl
β”‚   β”œβ”€β”€ Red_Bull_Conservative_q.pkl
β”‚   └── ... (15 total .pkl files for all team/profile combinations)
β”‚
β”œβ”€β”€ logs/                         # πŸ“ Simulation output data (auto-generated)
β”‚   └── gym_race_lap_data.csv     # Lap-by-lap race telemetry logs
β”‚
β”œβ”€β”€ training_figures/             # πŸ“Š Training visualizations (auto-generated)
β”‚   β”œβ”€β”€ rewards_{Team}_{Profile}.png      # Episode reward curves
β”‚   └── heatmap_{Team}_{Profile}.png      # Q-value state-action heatmaps
β”‚
β”œβ”€β”€ requirements.txt              # πŸ“¦ Python package dependencies
β”œβ”€β”€ README.md                     # πŸ“š Complete project documentation
└── LICENSE                       # βš–οΈ  MIT open-source license

Key Directory Descriptions

Core Application Files

streamlit_app.py - Primary user interface (3500+ lines)

  • Web dashboard powered by Streamlit framework
  • Automatic model downloading from Hugging Face Hub
  • Real-time race simulation with Plotly animations
  • PDF export functionality for race reports

env/gym_race_env.py - Reinforcement learning environment

  • OpenAI Gymnasium API compliance
  • F1-specific state space (tire wear, fuel, weather, traffic)
  • Reward shaping for optimal pit stop timing
  • Physics-based tire degradation and fuel consumption

rl/q_learning_agent.py - Q-Learning agent implementation

  • Tabular RL algorithm with discrete state space
  • Epsilon-greedy exploration-exploitation balance
  • Bellman equation for Q-value updates

Training Scripts

train_ppo.py - Deep reinforcement learning training

  • Proximal Policy Optimization (Stable-Baselines3)
  • 300,000 training timesteps (~2-3 hours CPU)
  • Automatic checkpoint saving and model evaluation
  • TensorBoard integration for training visualization

train_lap_model.py - Supervised learning for lap time prediction

  • RandomForest regression with 150 decision trees
  • 14-dimensional feature engineering pipeline
  • Model evaluation: RMSE < 2.0s, RΒ² > 0.85

main.py - Batch Q-Learning training orchestrator

  • Trains 15 specialized agents (5 teams Γ— 3 driver profiles)
  • Progress tracking with tqdm progress bars
  • Generates reward curves and Q-value heatmaps
  • Saves trained agents to saved_agents/ directory

ppo_eval.py - Agent performance evaluation

  • Tests PPO agent on validation episodes
  • Deterministic policy (no random exploration)
  • Exports detailed statistics for analysis

Data & Model Directories

models/ - Machine learning model storage

  • Auto-downloaded from Hugging Face Hub on first run
  • ppo_pit_stop.zip: PPO neural network (PyTorch SavedModel)
  • lap_time_predictor.pkl: RandomForest regressor (Scikit-learn pickle)

saved_agents/ - Q-Learning agent repository

  • 15 pre-trained agents covering all team/profile combinations
  • Each .pkl file contains Q-table and hyperparameters
  • Examples: Ferrari_Aggressive_q.pkl, McLaren_Balanced_q.pkl

logs/ - Simulation telemetry data

  • gym_race_lap_data.csv: Training data for ML lap time predictor
  • Generated automatically during race simulations
  • Contains: lap number, tire wear, fuel weight, traffic, lap time, etc.

training_figures/ - Training visualization outputs

  • Reward curve plots for each Q-Learning agent
  • Q-value heatmaps showing optimal state-action pairs
  • Auto-generated during main.py execution

File Organization Benefits

🎯 Modularity:

  • Clear separation between environment, agents, and training
  • Easy to modify individual components without affecting others

πŸ”§ Maintainability:

  • Self-documenting file names and directory structure
  • Comprehensive inline comments and docstrings

πŸ“¦ Scalability:

  • Easy to add new agents or training scripts
  • Extensible architecture for future features

πŸš€ Developer Experience:

  • Quick navigation to specific functionality
  • Standard Python project layout conventions

⬆ Back to Table of Contents


βš™οΈ Configuration

Configuration Settings - F1 Pit Stop Simulator Environment Parameters, Driver Profiles, and Weather System Configuration for Racing Strategy Optimization

Environment Parameters - Race Simulation Settings

Customize the F1 racing environment using PitStopEnv initialization parameters:

Track Configuration Example

env = PitStopEnv(
    total_laps=58,                  # Race distance
    base_lap_time_seconds=90.0,     # Ideal dry lap time
    pit_time=28,                    # Pit stop time loss
    tire_wear_rate_config=1.1,      # Track abrasiveness multiplier
    traffic_penalty_config=3.0      # Time lost per traffic unit
)

Parameter Descriptions

total_laps - Race Distance

  • Range: 20-80 laps
  • Default: 58 laps (standard Grand Prix)
  • Short races: 20-30 laps for testing
  • Full distance: 50-80 laps for realistic strategy

base_lap_time_seconds - Ideal Lap Time

  • Represents optimal lap time in dry conditions with fresh tires
  • Varies by track: Monaco (~71s), Spa (~105s), Monza (~80s)
  • Used as baseline for tire degradation and fuel effects

pit_time - Pit Stop Duration

  • Total time lost during pit stop (including pit lane transit)
  • Typical F1 range: 20-35 seconds
  • Monaco: ~25s (short pit lane) | Spa: ~32s (long pit lane)

tire_wear_rate_config - Track Abrasiveness

  • Multiplier for tire degradation rate
  • Low wear: 0.7-0.9 (smooth tarmac)
  • Medium wear: 1.0-1.3 (standard circuits)
  • High wear: 1.5-2.0 (abrasive surfaces like Bahrain)

traffic_penalty_config - Overtaking Difficulty

  • Time penalty per traffic unit when following other cars
  • Low traffic: 1-3s (fast circuits like Monza)
  • High traffic: 5-10s (street circuits like Monaco)

Driver Profile Effects - Racing Style Customization

Driver profiles modify AI agent behavior and tire physics:

Profile Comparison Table

Profile Pit Threshold Tire Wear Multiplier Overtake Bonus Strategy Style
Aggressive 75% wear 1.15Γ— (faster degradation) -0.2s (time gain) Early pits, push hard
Balanced 65% wear 1.0Γ— (baseline) 0.0s (neutral) Standard strategy
Conservative 55% wear 0.85Γ— (slower degradation) +0.1s (time loss) Extended stints, preserve tires

Detailed Profile Mechanics

Aggressive Profile - Maximum Attack

  • Pit Threshold: 75% tire wear (pushes tires to limit)
  • Tire Degradation: 1.15Γ— faster wear (aggressive driving style)
  • Overtaking: -0.2s bonus (risk-taking maneuvers)
  • Best For: Qualifying-style pace, short stints, undercut strategies

Balanced Profile - Standard Approach

  • Pit Threshold: 65% tire wear (optimal trade-off)
  • Tire Degradation: 1.0Γ— baseline wear (neutral driving)
  • Overtaking: 0.0s neutral (standard racecraft)
  • Best For: Versatile strategy, adaptable to race conditions

Conservative Profile - Tire Management

  • Pit Threshold: 55% tire wear (preserves tire life)
  • Tire Degradation: 0.85Γ— slower wear (gentle driving style)
  • Overtaking: +0.1s penalty (cautious approach)
  • Best For: One-stop strategies, high-degradation tracks, overcut tactics

Weather System - Dynamic Race Conditions

Rain Forecast Configuration

Define probabilistic weather windows for realistic race scenarios:

rain_forecast_ranges = [
    {
        "start": 15,           # Rain window start lap
        "end": 25,             # Rain window end lap
        "probability": 0.7,    # 70% chance of precipitation
        "intensity": 0.6       # Rain strength: 0.0 (light) to 1.0 (heavy)
    }
]

Multiple Weather Windows Example:

rain_forecast_ranges = [
    {"start": 10, "end": 20, "probability": 0.5, "intensity": 0.3},  # Light rain
    {"start": 35, "end": 50, "probability": 0.8, "intensity": 0.8}   # Heavy rain
]

Rain Effects on Race Performance

Lap Time Penalties:

  • Light rain (0.1-0.3): +5-8 seconds per lap
  • Medium rain (0.4-0.6): +8-12 seconds per lap
  • Heavy rain (0.7-1.0): +12-15 seconds per lap

Grip Reduction:

  • Dry conditions: 100% grip (baseline)
  • Light rain: 70-80% grip (-20-30% reduction)
  • Heavy rain: 40-60% grip (-40-60% reduction)

Tire Compound Requirements:

  • Intensity < 0.4: Intermediate tires recommended
  • Intensity β‰₯ 0.4: Full wet tires mandatory
  • FIA Rule: Must use at least one wet/intermediate compound in wet races

Strategic Implications:

  • Early pit advantage: Teams can switch to wet tires before rain intensity increases
  • Track position gamble: Stay out on slicks or pit early for inters
  • Safety Car potential: Heavy rain often triggers Safety Car deployments

⬆ Back to Table of Contents


πŸ“Š Performance Metrics

Performance Metrics - AI Agent Evaluation for F1 Pit Stop Strategy with Total Reward, Pit Efficiency, and Machine Learning Accuracy Benchmarks

Agent Evaluation Metrics - Measuring AI Performance

Total Reward - Cumulative Race Score

The primary metric for evaluating reinforcement learning agent performance:

Total Reward = Ξ£(lap_rewards) - penalties
where lap_reward = -(lap_time) + bonuses

Formula Breakdown:

  • Lap Reward: Negative lap time (faster = better reward)
  • Bonuses: Overtake bonuses, strategic timing rewards
  • Penalties: FIA regulation violations, unsafe maneuvers
  • Objective: Maximize total reward (minimize race time)

Interpretation:

  • Higher reward = Better strategy (closer to 0)
  • Lower reward = Slower race time
  • Typical range: -6000 to -4700 units

Pit Efficiency Rating - Strategy Optimization Score

Measures how effectively pit stops are executed relative to total race time:

Pit Efficiency = 100 Γ— (1 - (pit_stops Γ— pit_time) / total_race_time)

Formula Components:

  • pit_stops: Number of pit stops executed
  • pit_time: Time lost per pit stop (~20-35s)
  • total_race_time: Complete race duration
  • Result: Percentage efficiency (0-100%)

Example Calculation:

2 pit stops Γ— 28s = 56s lost
Total race time: 5400s (90 min race)
Pit Efficiency = 100 Γ— (1 - 56/5400) = 98.96%

Performance Targets:

  • Excellent: >95% (minimal pit time impact)
  • Good: 90-95% (balanced strategy)
  • Poor: <85% (excessive pit stops)

FIA Penalties - Regulation Compliance

Penalties applied for violating Formula 1 technical regulations:

Current Penalties:

  • Missing 2-compound rule: -20 reward units
    • Applies to dry races only
    • Must use at least 2 different tire compounds
    • Ensures strategic variety

Future Penalties (Roadmap):

  • Unsafe pit release: -10 units (v2.0 feature)
  • Speeding in pit lane: -5 units (planned)
  • Track limits violations: -3 units per incident (planned)

Typical Performance Ranges - Agent Comparison

Benchmarks from 100+ race simulations across varied conditions:

Metric Q-Learning PPO Custom Strategy
Total Reward -5500 to -4800 -5300 to -4700 -6000 to -5000
Avg Pit Stops 1-3 1-2 User-defined
Pit Efficiency 85-92% 88-95% 70-95%
Win Rate 35-40% 45-55% 10-30%
Strategy Consistency Medium High Variable

Performance Analysis

Q-Learning Agent:

  • Strengths: Fast inference, interpretable decisions
  • Weaknesses: Limited generalization to novel scenarios
  • Best Use Cases: Well-defined tracks, stable conditions

PPO Agent:

  • Strengths: Superior performance, excellent adaptability
  • Weaknesses: Slower training (2-3 hours)
  • Best Use Cases: Complex weather, mixed conditions, competitive racing

Custom Strategy:

  • Strengths: Full user control, educational value
  • Weaknesses: Requires F1 knowledge, inconsistent results
  • Best Use Cases: Testing specific strategies, learning pit timing

ML Model Accuracy - Lap Time Predictor Performance

RandomForest regressor metrics for lap time prediction:

Model Performance Benchmarks

Root Mean Squared Error (RMSE):

  • Range: 1.5-2.5 seconds
  • Interpretation: Average prediction error
  • Target: <2.0s for production use
  • Typical: 1.8s on test set

RΒ² Score (Coefficient of Determination):

  • Range: 0.85-0.92
  • Interpretation: Variance explained by model
  • Target: >0.85 for reliable predictions
  • Typical: 0.89 on test set

Mean Absolute Error (MAE):

  • Typical: 1.2-1.8 seconds
  • Best Case: <1.5s with abundant training data

Feature Importance Rankings

Top predictors of lap time performance (from trained model):

Rank Feature Importance Impact
1 tire_wear 35% Most critical - exponential lap time increase
2 lap_number 18% Fuel load reduction improves pace
3 fuel_weight 14% ~0.03s per kg impact
4 traffic 12% Overtaking difficulty affects lap time
5 tire_compound 10% Soft vs. Hard performance differential
6 track_temp 6% Grip variation with temperature
7 rain_intensity 5% Wet conditions slow pace

Key Insights:

  • Tire wear is 2Γ— more important than any other feature
  • Combined tire metrics (wear + compound) = 45% importance
  • Weather factors (rain, temp) = 11% importance

Performance Optimization Tips

🎯 Improving Agent Performance:

  • Train on diverse conditions (rain, SC, traffic variations)
  • Increase training episodes: 2000 β†’ 5000 for Q-Learning
  • Use GPU acceleration for PPO training (10Γ— faster)

πŸ“ˆ Improving ML Predictor Accuracy:

  • Generate 500+ laps of training data
  • Include edge cases (full wet races, SC periods)
  • Retrain model after major configuration changes

⚑ Benchmarking Best Practices:

  • Run statistical comparisons (50+ races per strategy)
  • Test across multiple tracks (different wear rates)
  • Include random events (rain, SC) for robustness testing

Performance Visualization

The simulator provides real-time performance tracking:

During Race:

  • Live reward accumulation
  • Pit efficiency percentage
  • Lap time delta chart

Post-Race:

  • Comparative bar charts (Q-Learning vs PPO)
  • Distribution plots (box plots, histograms)
  • Statistical summary tables

⬆ Back to Table of Contents


πŸ›£οΈ Roadmap

Roadmap - Future Development Plans for F1 Pit Stop Simulator including Multi-Agent Racing, Advanced AI Strategy, Deep Learning Models, and Championship Mode

Phase 1: Multi-Agent Racing (Q3 2025)

  • Implement 20-car grid simulation
  • Inter-agent competition with overtaking logic
  • Position-based reward shaping
  • Qualifying-based grid positions

Phase 2: Advanced Strategy (Q4 2025)

  • Dynamic pit strategy updates (mid-race replanning)
  • "What-If" scenario analysis tool
  • Tire compound optimizer (pre-race selection)
  • Undercut/overcut detection system

Phase 3: Enhanced Realism (Q1 2026)

  • Red flag handling and race restarts
  • Driver skill variation (consistency, errors)
  • Team radio communication simulation
  • Pit crew performance modeling

Phase 4: Deep Learning (Q2 2026)

  • Transformer-based strategy predictor
  • LSTM for lap time forecasting
  • Convolutional networks for track layout analysis
  • Multi-agent reinforcement learning (MARL)

Phase 5: Championship Mode (Q3 2026)

  • Full season simulation (23 races)
  • Points system and standings
  • Car development progression
  • Strategic resource allocation (tire allocation)

⬆ Back to Table of Contents


πŸ”— External Resources

External Resources - Hugging Face Model Repository, Stable-Baselines3 Documentation, Gymnasium API Reference, and Streamlit Component Library for F1 Pit Stop Simulator

Model Repository

All trained models hosted on Hugging Face Hub

Automatic Download: Models are fetched on first app launch via download_models_from_hf() function.

Manual Download (if needed):

huggingface-cli download Richard1224/pit-stop-simulator-models --local-dir ./

Documentation

⬆ Back to Table of Contents


πŸ™Œ Contributing

Contributing to F1 Pit Stop Simulator - Open Source Project Accepting Contributions for Multi-Agent Racing, Deep Learning, and F1 Strategy Features

Contributions welcome! Priority areas:

High-Impact Improvements

  1. Real F1 Data Integration: Parse actual race telemetry (F1 API, FastF1 library)
  2. Additional RL Algorithms: A3C, SAC, TD3 implementations
  3. Performance Optimization: Numba JIT compilation, parallel simulation
  4. Track Expansion: Add 2024 F1 calendar circuits with accurate characteristics

Development Setup

# Fork and clone
git clone https://github.com/YOUR_USERNAME/pit-stop-simulator.git
cd pit-stop-simulator

# Create feature branch
git checkout -b feature/your-feature-name

# Install dev dependencies
pip install -r requirements-dev.txt

# Run tests (if implemented)
pytest tests/

# Submit PR with detailed description

Code Standards

  • Follow PEP 8 style guide
  • Add docstrings to new functions
  • Include type hints for function signatures
  • Update README if adding major features

⬆ Back to Table of Contents


πŸ“„ License

License Information - F1 Pit Stop Simulator Released Under MIT Open Source License for Free Commercial and Personal Use

This project is licensed under the MIT License β€” see LICENSE file for details.

MIT License - Copyright (c) 2025

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

⬆ Back to Table of Contents


πŸ† Acknowledgments

Built with passion for Formula 1 and powered by:

Inspiration

This project honors the strategic complexity and split-second decisions that define Formula 1 as the pinnacle of motorsport. Every pit stop, tire choice, and weather call can mean the difference between victory and defeat.


⬆ Back to Table of Contents

Made with ❀️ for F1 fans and ML enthusiasts

Report Bug Β· Request Feature Β· Documentation

Packages

No packages published

Languages