A JavaScript-based self-driving car simulation using neural networks and genetic algorithms. The project implements a complete AI system with no external libraries, featuring real-time visualization of both the car's behavior and its neural network decision-making process.
- Neural Network AI: Cars learn to navigate traffic using a custom-built neural network
- Genetic Algorithm: Population of 1000 cars with mutation-based learning
- Real-time Visualization: Live display of neural network activity
- Sensor System: Ray-casting sensors detect road borders and traffic
- Collision Detection: Polygon-based collision detection system
- Persistent Training: Save and load the best-performing neural network
- Traffic System: Dynamic traffic obstacles with random colors
- Input Layer: Sensor rays detecting obstacles
- Hidden Layer: 6 neurons processing sensor data
- Output Layer: 4 neurons controlling movement (↑, ←, →, ↓)
- 1000 AI-controlled cars start simultaneously
- Each car uses a neural network to make driving decisions
- The best-performing car (travels farthest) is tracked
- Save the best brain to continue training from that point
- New generations mutate from the saved brain with 10% variation
- A modern web browser (Chrome, Firefox, Safari, Edge)
- Local web server (optional, but recommended)
- Clone or download the repository
- Open
index.htmlin your browser, or - Use a local server:
# Using Python 3 python -m http.server 8000 # Using Node.js (with http-server) npx http-server
- Navigate to
http://localhost:8000
- 💾 Save Button: Save the best-performing neural network to browser storage
- 🗑️ Discard Button: Delete saved neural network and start fresh
- Let the simulation run until most cars crash
- Save the best brain using the save button
- Refresh the page - the saved brain will load with mutations
- Repeat the process to improve performance over time
self_driver/
├── index.html # Main HTML file
├── style.css # Styling for canvas and UI
├── main.js # Entry point, animation loop
├── car.js # Car class with physics and rendering
├── controls.js # Input handling (keyboard/AI)
├── sensor.js # Ray-casting sensor system
├── network.js # Neural network implementation
├── visualiser.js # Neural network visualization
├── road.js # Road rendering and boundaries
├── utils.js # Utility functions (lerp, intersection, etc.)
└── car.png # Car sprite image
- Acceleration: 0.2 units/frame
- Max speed: 3 units/frame (AI cars), 2 units/frame (traffic)
- Friction: 0.05 units/frame
- Steering: 0.03 radians per frame
- Type: Feedforward neural network
- Activation: Custom threshold-based activation
- Mutation Rate: 10% linear interpolation
- Training Method: Parallel genetic algorithm
- Ray count: Configurable (default in network architecture)
- Ray length: Detects road borders and traffic
- Ray spread: Full frontal arc coverage
In main.js, change the value of N:
const N = 1000; // Change to desired numberIn main.js, adjust the mutation parameter:
NeuralNetwork.mutate(cars[i].brain, 0.1); // Change 0.1 to desired rateEdit the traffic array in main.js to add/remove obstacles:
const traffic = [
new Car(road.getLaneCenter(1), -100, 30, 50, "DUMMY", 2, getRandomColor()),
// Add more traffic cars here
];- Input: Sensor readings from ray-casting (distances to obstacles)
- Processing:
- Inputs are multiplied by weights
- Biases are added
- Activation function determines output
- Output: Four control signals (forward, left, right, backward)
- Learning: Genetic algorithm selects best performer and mutates it
- Runs at 60 FPS on modern hardware
- Handles 1000+ simultaneous car instances
- Real-time neural network visualization
- Efficient collision detection using polygon intersection
This project is open source and available for educational purposes.