Optimum Path with Genetic Algorithm

Optimum Path with Genetic Algorithm

January 2, 2026

TL;DR

Time to level up the modelling!

Intro

Recently I was exploring the GoPro GPS telemetry to get flying laps information and creating a simple point mass simulator on that same circuit.

The Problem: Position β‰  Actions

Gradient solver (v1) optimizes: Positions (x, y) - where to be on track

Implicitly: “drive along this line”

Now, the GA (v2) needs: Actions (steering, pedal) - what to do

What’s Happening in Gen 0: The ga_converter.py converts gradient results to steering/pedal:

Goes off-track β†’ fitness = 100s penalty + remaining distance β‰ˆ 1000s+

What WILL Happen: the GA will learn from scratch:

Gen 1-10: Random mutations stumble upon “stay on track” strategies Gen 10-30: Evolution finds basic control patterns that complete laps Gen 30-100: Refinement toward optimal ~80s The gradient seeds aren’t helping much due to the physics mismatch, but that’s OK - the GA will evolve its own solution! 🧬

Let it run - you should see improvement soon!

The initial genes

From…the gradient seeds!

python3 apexsim_v1.py

βœ… All 20 complete the lap (gradient solver guarantees) βœ… Diverse lap times (85.84s β†’ 80.36s) βœ… Different strategies (early vs late optimization) βœ… Only 20 files (manageable) βœ… Actually useful for GA!

Dont overcomplicate it and get the guarantee that at least those are finishing the lap, not crashing and not doing very slow laps.

Running the Genetic Path Optimizer

#cd 2-pointmass-ga/
#ga_converter.py executed via the apexsim_v1.py script will generate the ga_seed_variations folder
chmod +x test_phase3.py && python3 test_phase3.py

chmod +x apexsim_ga.py
python3 apexsim_ga.py
βœ“ Evolution stopped: Max generations (100) reached

Step 5: Generating visualizations...
  βœ“ Saved: results/ga_input_profile.png
  ⚠ Skipping G-G diagram (simulation didn't finish)
  βœ“ Saved: results/ga_convergence.png
  βœ“ Saved: results/ga_racing_line.png

Step 6: Exporting results...
  βœ“ Saved: results/ga_best_chromosome.csv
  ⚠ Skipping detailed export (simulation didn't finish)
  βœ“ Saved: results/ga_report.txt

============================================================
  EVOLUTION SUMMARY
============================================================
============================================================
  ApexSim GA-Edition - Evolution Report
============================================================

TRACK INFORMATION
  Length: 1781.88 m
  Points: 2651

VEHICLE PARAMETERS
  Top Speed: 85.0 km/h
  Max Lateral G: 1.0g
  Max Braking G: 0.6g
  Max Accel G: 0.3g

GA CONFIGURATION
  Population Size: 200
  Elitism: 10
  Crossover Prob: 70.0%
  Mutation Prob: 20.0%
  Control Nodes: 392

EVOLUTION RESULTS
  Generations: 100
  
  Initial Best Fitness: 1706.90 s
  Final Best Fitness:   1643.77 s
  
  Improvement: 63.13 s (3.7%)

BEST LAP DETAILS
  Finished: False
  Lap Time: 1643.76 s

============================================================

Total evolution time: 3.1 minutes
Time per generation: 1.8 seconds

alt text

alt text

Genetic Algorithm - Racing Line fail

alt text