This repository contains a custom Gymnasium environment that simulates an F1 race strategy problem, allowing an agent to learn pit timing, tire selection, and pace management to minimize total race time.
Reference: we followed Gymnasium's official guide for creating custom environments. See: Gymnasium environment creation tutorial.
Create and activate a Python 3.9+ environment, then install this package in editable mode:
pip install -e .If you plan to contribute:
pip install -e .[dev]The environment registers on import as:
f1_gymnasium/F1Strategy-v0
Quick smoke test to run a short race with random actions:
python examples/run_f1_env.pyOr from Python:
import gymnasium as gym
import f1_gymnasium # ensures env is registered on import
env = gym.make(
"f1_gymnasium/F1Strategy-v0",
render_mode="ansi",
total_laps=10,
seed=42,
)
obs, info = env.reset()
done = False
while True:
action = env.action_space.sample()
obs, reward, terminated, truncated, info = env.step(action)
if env.render():
print(env.render())
if terminated or truncated:
break- Action space (Discrete 6): stay-normal, stay-push, stay-save, pit-soft, pit-medium, pit-hard
- Observation: lap, stint_laps, tire_compound, tire_wear, ers, safety_car
- Reward: negative lap time in seconds (minimize total race time)
- Stochasticity: safety car / VSC probabilities and lap time noise
- Tune tire wear and performance parameters
- Add multi-car interactions and traffic loss models
- Add weather dynamics and mandatory tire rules
- Provide vectorized env wrapper for faster training