Optimum Path with Genetic Algorithm
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


