← Back to Dev LogsAyushDeep.space
October 31, 2026 · 3 min read

Teaching Neural Networks to Race: Building an F1-Style AI Simulator

A development log on building a 2D racing simulator where neural-network drivers learn track control through evolutionary training.

PythonNeural NetworksGenetic AlgorithmsPygame

The experiment

I wanted to build something that makes machine learning visible. Instead of looking at a training score or a notebook full of metrics, I wanted to watch an agent learn to drive.

The result is a 2D, F1-inspired racing simulator built with Python and Pygame. Each car observes the track through sensors and uses a small neural network to decide how to steer and accelerate.

Turning a track into inputs

A driver needs enough information to stay on the circuit without being handed the correct action. The current controller receives seven directional track-sensor readings and a normalized speed value.

The network produces two controls:

The important design choice is that the network does not receive a precomputed racing line. It has to learn useful behavior from the observations available to it.

Learning through evolution

I used a genetic algorithm to train a population of drivers. Each generation is evaluated in the simulator, the stronger candidates are retained, and their parameters are combined and mutated to create the next population.

The fitness function rewards progress around the track and completed laps. Crashes receive a penalty so that simply moving quickly without staying on the circuit is not a good strategy.

This is not reinforcement learning with a value function or policy-gradient update. It is neuroevolution: the network weights are the candidates being evolved.

What changed during development

The first versions were useful for proving the idea, but scaling the population made rendering expensive. I reduced visual overhead by simplifying car rendering and separating the training objective from purely cosmetic detail.

I also built a replay path for inspecting the saved best driver. That matters because a high fitness score is not enough. Watching a replay exposes behaviors that a single number can hide, including unstable steering and late crashes.

What I learned

Next steps

The next useful improvements are more robust crash handling, repeatable evaluation across multiple tracks or starting conditions, and clearer generation-level metrics. I also want to move from isolated training runs toward races with multiple cars using the same learned policy.

The goal is not to claim that the cars have mastered racing. It is to build a simulator where each improvement can be observed, measured, and reproduced.

© 2026 Ayush Deep · Built with React & Diamonds.