A genetic algorithm-based neural network car training simulation. Cars learn to navigate a bounded arena using evolution, mutation, and natural selection. The system prevents fitness plateau and demonstrates continuous multi-generation improvement.
40 Generations, 25 cars per generation:
# Clone and install dependencies
git clone <repo>
cd SelfLearningML
# Create virtual environment
python -m venv .venv
.\.venv\Scripts\Activate.ps1
# Install requirements
pip install pygame numpy
python -m src.demo_train
Output:
Generation 1 | Best: 2078.85 | Avg: 578.83
Generation 5 | Best: 2478.47 | Avg: 1346.87
Generation 10 | Best: 3694.94 | Avg: 2097.25
...
Generation 40 | Best: 3696.37 | Avg: 3464.81
python -m src.train
python -m src.main
Controls:
src/
├── __init__.py # Package marker
├── brain.py # Neural network (7→8→2)
├── car.py # Car physics & AI
├── environment.py # Simulation world
├── population.py # Genetic algorithm
├── train.py # Full training loop
├── storage.py # Database & weights persistence
├── main.py # GUI simulator
└── demo_train.py # Headless 40-generation demo
tests/
├── test_car.py # Unit tests
requirements.txt # Dependencies
Input (7): [5 sensors, velocity, angle]
↓
Hidden (8): tanh activation
↓
Output (2): [acceleration, steering] ∈ [-1, 1]
fitness = distance_traveled + (age × 0.3) + sensor_reward - (collisions × 100)
Edit src/demo_train.py to customize:
run_demo(
pop_size=25, # Population size
num_gens=40, # Number of generations
steps_per_gen=400 # Steps per episode
)
Edit src/car.py physics constants:
self.max_speed = 10
self.acceleration_rate = 0.3
self.friction = 0.03
self.steering_rate = 4
self.max_age = 2000
# Run unit tests
pytest tests/test_car.py -v
# Quick import check
python -c "from src.population import Population; print('OK')"
✅ Fixed package imports (relative imports + src/__init__.py)
✅ Distance-based fitness (unbounded, unlike velocity)
✅ Adaptive mutation rate (exploration boost during stagnation)
✅ Tournament selection + crossover (population diversity)
✅ Sensor engagement reward (encourages smart navigation)
✅ Headless training support (no pygame required)
MIT
Self-Learning ML Project (Feb 2026)