Philipp Altmann

Philipp Altmann

benchmark

Karting Challenge

Unity project for a karting-based ML‑Agents challenge. Drive a kart around a modular track, collect rewards, and train agents to complete laps robustly across different track parts and obstacles.

Goals

[Expand]

Karting Challenge

  • Provide a reproducible Unity scene for training and evaluating kart agents.
  • Make track parts modular so difficulty can be scaled (ramps, steep turns, bumpy segments).
  • Support both manual driving and ML‑Agents training.
  • Environment And Tracks

    [Expand]

    The karting agent observes a compact state vector, drives with continuous control, and is rewarded for fast, clean progress through checkpoints. The default training scene is Assets/Karting/Scenes/KartingChallenge.unity.

    Training Track Karting Agent
    Base Track Karting Agent
    Component Description Dimension / Type Notes
    Action Continuous control: steer, brake, accelerate 3 values (continuous) 3D continuous control.
    Observation (State) 9 lidar distances + speed + direction + distance to next collider 12‑dimensional vector Vector observations only.
    Reward Multi‑objective Scalar Avoid collisions, pass checkpoints, move toward checkpoints, move fast.
    Track layout Single training track provided N/A Meta‑goal: generalize to other tracks.

    Training Variants

    Training Easy Training Medium Training Hard
    Training Easy Training Medium Training Hard

    These variants are available as scenes under Assets/Karting/Scenes/Train/.

    Quick Start

    [Expand]
  • Open the project in Unity (the editor version 2021.3 LTS can be installed through the Unity Hub).
  • Open the main scene Assets/Karting/Scenes/KartingChallenge.unity.
  • Press Play to drive manually or run training if ML‑Agents is configured.
  • Note: For manual driving, insert the keyboard agent from Assets/Karting/Prefabs/KartClassic/.

    Installation

    [Expand]

    You can install the repo as a Python package for the Unity environment wrapper.

  • Base install (environment only): pip install . or pip install -e .
  • Training extras (adds SB3 + tensorboard): pip install .[training]
  • Training

    [Expand]

    This project is designed to work with Unity ML‑Agents. A typical flow:

  • Install/enable ML‑Agents for your Unity version.
  • Build (compile) the Unity environment executable (see below).
  • Configure the training parameters and behavior names.
  • Start training and monitor episode rewards and lap completion rate.
  • If you add new track parts, ensure the reward logic and collision handling still behave correctly across them.

    Build The Training Environment (Unity Executable)

    Training uses a standalone Unity build (not the editor). Build it once and reuse it for runs.

    Platform Build Output
    Windows Training/KartingChallenge.exe
    macOS Training/KartingChallenge.app
    Linux Training/KartingChallenge.x86_64
  • Open the project in Unity.
  • Go to File > Build Settings….
  • Select your target platform (Windows/macOS/Linux) and click Build.
  • Set the output path to Training/KartingChallenge (see table below).
  • Training (Python)

    To train a Karting Agent using Stable Baselines 3 use:

    from stable_baselines3 import PPO
    from KartingGymnasium import *
    
    # Setup karting environment, pass the path to your build executable 
    # Use time_scale to increase the simulation speed, no_graphics for running headless
    env = KartingGymnasium("./KartingChallenge", time_scale=10, no_graphics=True)
    
    # Setup model, log to tensorboard and train
    model = PPO("MlpPolicy", env, verbose=1, tensorboard_log="./tensorboard")
    model.learn(total_timesteps=10e5)
    
    # Save the model for later inference
    model.save('models/KartingModel')
    env.close()
    

    Gymnasium Wrapper

    The KartingGymnasium package exposes a Gymnasium-compatible environment around the Unity build. It connects via ML‑Agents, provides a standard reset()/step() loop, and supports continuous actions only.

    Export Policy (ONNX)

    To export a trained SB3 model to an ONNX "brain" for inference in Unity, use:

    from stable_baselines3 import PPO
    from KartingGymnasium import *
    
    # Load model, create karting brain, and export 
    model = PPO.load('models/KartingModel')
    brain = KartingBrain(model)
    brain.export("models/KartingModel.onnx")
    

    To use the model, move it to Assets/Karting/Prefabs/AI. In Unity, with the Scene open, select the KartingAgent; in the inspector under Behavior Parameters > Model select your model. Pressing play in the editor should start the scene and the agent running in inference using the behavior from the trained model.

  • Module: KartingGymnasium/wrapper.py
  • Class: KartingGymnasium.KartingGymnasium
  • Observations: vector and/or visual observations, exposed as Gymnasium spaces.
  • Actions: continuous action space (steer, brake, accelerate).
  • Tournaments

    [Expand]

    In addition to the training track specified above, there are predefined tournament scenes. Tournament scenes live under Assets/Karting/Scenes/Tournament/. These scenes contain multiple agents preconfigured with different models for head‑to‑head evaluation.

    Tournament

    Tournament Layouts

    Tournament Easy Tournament Medium Tournament Hard Tournament Advanced
    Tournament Easy Tournament Medium Tournament Hard Tournament Advanced

    Keyboard Agent

    A keyboard agent is included for manual driving and camera following. Use the arrow keys to drive and the spacebar to swap between agent cameras and the bird’s‑eye view. The keyboard and karting agents are available as prefabs under Assets/Karting/Prefabs/KartClassic/.

    Adapting Parameters

    [Expand]

    In Unity, select a karting agent and adjust parameters in the Inspector under Karting Agent:

  • Sensors: Position, direction, and count of lidar sensors. Default is 9 lidar sensors (forward, ±30, ±45, ±60, ±90 degrees) plus speed, direction, and distance to the next collider for a 12‑D observation. The observation stacks frames before action selection.
  • Colliders: Add and order checkpoint colliders (rings) to define the driving direction and reward structure.
  • Rewards: Weights for the multi‑objective reward (hit penalty, checkpoint reward, progress to checkpoint, speed reward, acceleration reward).
  • Action Space: Continuous control for steering and acceleration/brake (default, 3‑D action) or discrete actions for keyboard‑style control.
  • Training reset: Number of wall collisions before the agent is reset to prevent getting stuck early in training.
  • New tracks can be assembled from parts in Assets/Karting/ModularTrackKit/Prefabs/.

    External References

    [Expand]
  • Unity ML‑Agents Toolkit
  • Unity Karting Microgame
  • Citation

    [Expand]

    When using this repository you can cite it as:

    @software{kartingchallenge2023,
      title = {Karting Challenge},
      author = {Philipp Altmann and Maximilian Zorn and Jonas Nüßlein and Fabian Ritz and Andreas Sedlmeier and Thomy Phan},
      version = {0.8.0},
      year = {2023},
      url = {https://github.com/philippaltmann/KartingChallenge}
    }
    
    , ,, in , , in vol. no. , pp. , . . [Code] [PDF] [Preprint]