Project Overview
An advanced self-driving car simulation that uses artificial neural networks to learn autonomous driving behavior. The AI cars navigate through traffic, avoid collisions, and evolve their driving strategies through genetic algorithms and machine learning techniques.
This project demonstrates deep understanding of neural networks, genetic algorithms, collision detection, and real-time AI decision making. The cars learn to drive by processing sensor data and making split-second decisions to navigate safely through complex traffic scenarios.
Key Features
Neural Network AI
Multi-layer neural network with configurable inputs, hidden layers, and outputs for intelligent decision making.
Genetic Algorithm Evolution
Population-based learning where successful cars pass their "genes" to the next generation with mutations.
Real-Time Sensor System
Ray-casting sensors that detect road boundaries and traffic obstacles for environmental awareness.
Collision Detection
Precise polygon-based collision system for realistic interactions between cars and environment.
Traffic Generation
Dynamic traffic patterns with dummy cars creating realistic driving scenarios and challenges.
Network Visualization
Real-time visualization of neural network activity showing decision-making processes.
Technical Implementation
Built entirely in vanilla JavaScript using HTML5 Canvas for real-time graphics rendering and custom neural network implementation from scratch.
Core Systems:
- Neural Network - Custom implementation with feedforward propagation and bias systems
- Genetic Algorithm - Mutation and selection mechanisms for evolutionary learning
- Physics Engine - Realistic car movement with acceleration, friction, and steering
- Sensor System - Ray-casting for environment detection and obstacle avoidance
- Canvas Rendering - High-performance 2D graphics with smooth animations
// Neural Network Implementation
class NeuralNetwork {
constructor(neuronCounts) {
this.levels = [];
for (let i = 0; i < neuronCounts.length - 1; i++) {
this.levels.push(new Level(
neuronCounts[i], neuronCounts[i + 1]
));
}
}
static feedForward(givenInputs, network) {
let outputs = Level.feedForward(
givenInputs, network.levels[0]);
for (let i = 1; i < network.levels.length; i++) {
outputs = Level.feedForward(
outputs, network.levels[i]);
}
return outputs;
}
static mutate(network, amount = 1) {
network.levels.forEach(level => {
for (let i = 0; i < level.biases.length; i++) {
level.biases[i] = lerp(
level.biases[i],
Math.random() * 2 - 1,
amount
)
}
});
}
}
AI Learning Process
The self-driving cars use a sophisticated learning approach combining neural networks with evolutionary algorithms:
Sensor Input Processing:
- Ray Casting - 10 sensors detect distances to obstacles and road boundaries
- Normalization - Sensor data converted to 0-1 range for neural network input
- Real-time Processing - Continuous sensor updates for dynamic decision making
Neural Network Architecture:
- Input Layer - 10 neurons (one for each sensor)
- Hidden Layers - Configurable layers for complex pattern recognition
- Output Layer - 4 neurons (forward, left, right, reverse controls)
- Activation Function - Binary threshold activation for clear decisions
Evolutionary Learning:
- Population - 300 AI cars learning simultaneously
- Fitness Function - Distance traveled without collision
- Selection - Best performing car becomes the "parent" for next generation
- Mutation - Random variations in neural network weights and biases
Challenges & Solutions
Neural Network Optimization:
Challenge: Finding the right network architecture and learning parameters.
Solution: Implemented configurable network layers and mutation rates, allowing experimentation with different configurations to find optimal performance.
Real-Time Performance:
Challenge: Running 300+ AI cars simultaneously without frame rate drops.
Solution: Optimized collision detection algorithms and efficient canvas rendering techniques to maintain smooth 60fps performance.
Learning Convergence:
Challenge: Preventing AI from getting stuck in local optima.
Solution: Implemented mutation mechanisms and diverse traffic patterns to encourage exploration of different driving strategies.
Interactive Features
The simulation includes several interactive elements for experimentation and analysis:
- Save/Load System - Preserve best performing neural networks for future sessions
- Real-time Visualization - Watch neural network decisions being made in real-time
- Population Management - Control the number of AI cars learning simultaneously
- Traffic Patterns - Configurable traffic density and behavior for varied challenges
- Network Inspection - Visual representation of active neurons and connections