An autonomous racing algorithm that combines the Follow the Gap (FTG) with Genetic Algorithm optimization to evolve optimal driving parameters for F1TENTH race cars in simulation.
Note: This Algorithm requires using the f1tenth_gym_ros simulator, you can find it here
This comes from my f1tenth repository, can be found here
f1tenth_gym_rossimulator installed and running- ROS2 Foxy
- Python 3.8+
- Docker
# First clone into your local workspace
# Build the package
colcon build --packages-select genetic_driver
source install/local_setup.bash
# + source any other local files requiredros2 launch f1tenth_gym_ros gym_bridge_launch.pyFor each new terminal:
docker exec -it f1tenth_gym_ros-sim-1 /bin/bash
source /opt/ros/foxy/setup.bash
source install/local_setup.bashTerminal 1 - Follow the Gap Driver:
ros2 run genetic_driver ftgTerminal 2 - Genetic Supervisor:
ros2 run genetic_driver supervisorThe supervisor will automatically start evolving after a couple seconds.
While the algorithm is running, you can interact via the supervisor terminal:
- Press
f+ Enter: View current champion genome stats (fastest lap) - Press
r+ Enter: Manually reset the current simulation run - Press
t+ Enter: Print recent valid lap times to console - Press
h+ Enter: Write detailed valid-lap history tolog/full_history.txt(overwrites each time)- Log file will be located in the container directory:
/sim_ws/install/genetic_driver/lib/python3.8/site-packages/log/full_history.txt
- Log file will be located in the container directory:
Example output:
🏆 CURRENT CHAMPION (As of Generation 5)
GenomeID : 12
Time : 24.37s
Score : 31.55
Genes : [1.234, 5.67, 2.1, ...]
GeneticSupervisor (ROS2 Node)
├── Manages simulation lifecycle
├── Tracks lap times & crashes
├── Publishes evolved parameters
└── Handles user input
GeneticML (Core Algorithm)
├── Maintains population of genomes
├── Calculates fitness scores
├── Selects & breeds best performers
└── Mutates genes for diversity
FollowTheGap (FTG Driver)
├── Processes LIDAR scans
├── Implements gap detection
├── Publishes steering/speed commands
└── Reports crash detection
Edit parameters in genetic_ml.py:
self.population_size = 20 # Number of genomes per generation
self.max_generations = 50 # Total generations to evolve
self.elitism_percent = 0.2 # Top 20% always surviveEach genome contains these evolving parameters (in GeneticParameters):
MAX_LIDAR_DIST- Maximum LIDAR range for gap detectionSTRAIGHT_SPEED- Target velocity on straightsTURN_SPEED- Target velocity during turnsSTEERING_SCALE- Steering sensitivity multiplier- ...and more (defined in
parameters.py)
fitness_score = (1000 / lap_time) - (crash_count * 500)- Positive side: Rewards faster lap times
- Negative side: Heavy penalty (500 points) for each crash
- Result: Evolves safe, fast driving strategies
- Elitism: Top 20% of genomes automatically advance
- Crossover: Random parents breed offspring
- Mutation: Offspring genes randomly mutate for diversity
- Replacement: New generation replaces worst performers
genetic_driver/
├── genetic_ml.py # Core genetic algorithm
├── parameters.py # GeneticParameters class
├── supervisor.py # ROS2 supervisor node
├── ftg_driver.py # Follow the Gap implementation
└── README.md