This project simulates autonomous cars learning to navigate a track using neural networks and a genetic algorithm. The cars learn to avoid walls and dynamic obstacles through evolutionary training.
The simulation features:
- Cars with neural network "brains" that control their movement
- A procedurally generated racetrack with inner and outer boundaries
- Dynamic obstacles that move along the track
- Genetic algorithm for evolving better-performing cars over generations
- Real-time visualization of car sensors (LiDAR-like rays)
- 3D-like visualization of the car's perspective
- New: A dedicated "About" panel providing detailed information about the simulation, its features, and controls.
- New: A clear information bar above the simulation canvas displaying real-time generation, speed, and distance from obstacles.
- New: Comprehensive training analytics dashboard with real-time charts and performance metrics.
index.html- Main HTML filejs/- Directory containing all JavaScript filesnn.js- Neural network implementation using TensorFlow.jsga.js- Genetic algorithm functionsparticle.js- Car/Agent class with neural network brainray.js- Raycasting for sensorsboundary.js- Track boundary and obstacle classessketch.js- Main p5.js sketch with setup and draw functionsloadbinary.js- Binary file loading utilitiesp5.collide2d.js- Collision detection library
css/- Directory containing stylesheet filesstyle.css- Main stylesheet for the application
ml-data/- Pre-trained modelsmodel.json- Base model structuregit-commands.sh- Git helper script
- A modern web browser (Chrome, Firefox, Edge, Safari)
- Internet connection (for loading external libraries)
- Open
index.htmlin a web browser - The simulation will start automatically
- Watch as cars evolve over generations to better navigate the track
- Dynamic/Static Toggle: Switch obstacle movement on/off
- Obstacle Count: Set number of obstacles (enter number and click "Done")
- Speed Slider: Control simulation speed
- Save Best: Save the best performing car's neural network
- Load Model: Load a pre-trained model
- Settings: Toggle visibility of the simulation settings panel.
- About: Toggle visibility of the about information panel.
- Analytics: Open the comprehensive training dashboard with real-time charts and performance metrics.
- Toggle Theme: Switch between light and dark UI themes.
This project uses Git for version control. To set up version control (already done):
git init
git add .
git commit -m "Initial commit: Autonomous car simulation with genetic algorithm and neural networks"For future development:
# Check status
git status
# Add changes
git add .
# Commit changes
git commit -m "Description of changes"
# View commit history
git logA helper script git-commands.sh is included to make common Git operations easier:
- Run
./git-commands.shand follow the prompts - Options include checking status, committing changes, viewing logs, and switching branches
mainbranch: Stable, working versions of the codedevelopmentbranch: Active development branch for new features
When working on new features:
- Switch to the development branch:
git checkout development - Make your changes
- Commit your changes
- When ready to release, merge to main:
git checkout main && git merge development
- Input layer: 13 neurons (sensor readings)
- Hidden layer: 26 neurons with sigmoid activation
- Output layer: 2 neurons with sigmoid activation (steering angle and speed)
- Population size: 100 cars
- Mutation rate: 20%
- Lifespan: 30 frames
- Sensor range: 80 pixels
Cars have 13 sensors (rays) spread across a 130-degree field of view to detect:
- Distance to track boundaries
- Distance to dynamic obstacles
The code has been refactored to improve:
- Modularity: Each functionality is separated into its own class or function
- Readability: Added comprehensive comments and consistent formatting
- Documentation: Detailed JSDoc comments for all functions and classes
- Maintainability: Improved code organization and structure
The simulation now includes a comprehensive analytics dashboard that provides:
- Fitness Evolution: Tracks best and average fitness scores across generations
- Lap Completion: Monitors progress in track completion over time
- Population Survival: Shows percentage of agents surviving each generation
- Speed Performance: Displays average speed metrics throughout training
- Current Generation: Live generation counter
- Best Fitness: Highest fitness score achieved
- Best Laps: Maximum laps completed by any agent
- Average Survival: Population survival percentage
- Training Time: Total time spent in training
- Improvement Rate: Measures fitness improvement over recent generations
- Convergence Score: Indicates how close the population is to optimal performance
- Diversity Index: Measures genetic diversity within the population
- Success Rate: Percentage of agents completing at least one lap
- Export training data as JSON for analysis
- Reset statistics and start fresh training sessions
- Historical data tracking with configurable retention
-
nn.js: Neural network implementation with detailed documentation
-
ga.js: Genetic algorithm functions with clear explanations
-
particle.js: Particle (car) class with comprehensive sensor and control logic
-
ray.js: Ray casting for obstacle detection with improved comments
-
boundary.js: Track boundary and obstacle classes with better structure
-
sketch.js: Main simulation logic with organized setup and draw functions
-
dashboard.js: New comprehensive training analytics system with Chart.js integration
For detailed development tasks, feature requests, and implementation priorities, see TODO.md.
The roadmap includes:
- Performance optimizations and memory management
- Enhanced track generation and curriculum learning
- Advanced neural network features and training methods
- Comprehensive testing and validation framework
- UI/UX improvements and accessibility features
- p5.js - Creative coding framework
- TensorFlow.js - Machine learning library
- p5.collide2d - Collision detection library
This project is open source and available under the MIT License.