Optimal Raceline is a NumPy and PyGame project that explores how evolutionary computation and neural control can discover high-performing racing trajectories on a track. The project models a population of virtual cars that sense the track ahead, choose steering and throttle actions, and improve over generations through mutation, elitism, and crossover-inspired policy evolution.
The simulation combines several ideas from robotics, control, and evolutionary computation:
- A geometric track model with curvature estimation and spatial lookup.
- A lightweight physics-inspired car model with adaptive sensing.
- A recurrent neural policy that maps sensory inputs to steering, acceleration, and speed targets.
- A neuroevolution loop that evolves better policies over generations.
- A visual interface for observing the evolving raceline and the live training process.
The core motivation is to study how an agent can learn to drive efficiently around a closed circuit by optimizing a scalar fitness signal derived from progress along the track and finishing behavior.
- main.py: The interactive training loop and visualization frontend.
- config.py: Global constants, sensor parameters, and Monaco-inspired track data.
- track_numpy.py: Track geometry, curvature estimation, and track lookup utilities.
- car_numpy.py: Vehicle state, sensor simulation, motion update, and rendering.
- nn_numpy.py: Neural policy implementation with recurrent memory and mutation support.
- evolution_numpy.py: Evolutionary generation update logic.
- line_tracker.py: Utility helpers for tracking and simplifying the best path.
- test_curves.py: Small diagnostic scripts for curvature and speed-limit analysis.
The track is represented as a discretized centerline with derived features such as:
- tangent and normal directions,
- curvature estimates at each sample,
- coarse corner sectors,
- a spatial grid for fast on-track queries.
This representation allows the agent to reason about upcoming corners and to estimate progress along the circuit.
Each car maintains a state vector consisting of position, heading, speed, progress, and sensor history. The car uses a simple ray-casting approach to determine how far it can see before leaving the track. These measurements are then fed into a neural network that outputs control actions.
The design deliberately mixes classical racing heuristics with learning-based control, including:
- adaptive sensor range based on curvature and speed,
- asymmetric sensor angles that bias sensing toward the inside of a corner,
- curvature-aware feature augmentation,
- a speed target head alongside steering and throttle outputs.
The evolutionary loop evolves a population of neural controllers by:
- ranking agents by fitness,
- preserving elite policies,
- mutating promising individuals,
- blending top-performing parents for offspring creation.
This approach is a lightweight form of evolutionary reinforcement learning and is well suited to a simulation where reward signals are sparse and delayed.
The PyGame interface provides a live view of:
- the race track,
- active cars and their sensors,
- the best discovered raceline,
- generation-level summary statistics.
This makes the project useful both as an educational demo and as a research-oriented environment for studying policy evolution under constrained sensing.
From a software engineering and research perspective, the repository illustrates several relevant ideas:
- Neuroevolution as an alternative to gradient-based reinforcement learning.
- Hybrid control architectures that combine geometric priors with learned behavior.
- Curriculum-like adaptation through mutation and elite preservation in a closed-loop simulation.
- The use of lightweight numerical methods and vectorized operations in a research prototype.
Although it is intentionally simple compared to modern end-to-end self-driving systems, it provides a compact and readable implementation of the core concepts behind evolutionary autonomous driving.
-
Install the dependencies:
- Python 3.10+
- NumPy
- PyGame
-
Launch the simulation:
python main.py
-
Use the interface to observe the training process:
- Space: advance the next generation
- Escape: quit
- Mouse wheel: zoom
- Left drag: pan the view
This repository is intended as a clear educational and experimental implementation rather than a production-grade autonomous driving stack. Its value lies in the combination of simulation, visualization, and evolutionary learning in a compact codebase.