0

Self-Driving Car with Genetic Algorithms

An AI-powered autonomous driving simulation using a genetic algorithm for evolving control strategies.

Self-Driving Car Simulation

πŸš— The Road to Autonomous Driving

What if a self-driving car could teach itself to drive, just like evolution shapes life? Instead of training with labeled data, this project evolves driving skills using Genetic Algorithms (GA).

Through natural selection, our cars learn to avoid obstacles, optimize speed, and navigate efficientlyβ€”all without explicit programming.

πŸ› οΈ The Evolution of an AI Driver

Traditional reinforcement learning often requires reward functions, but our genetic algorithm-based AI finds optimal driving behaviors without predefined rewards. Instead, we use evolutionary selection, crossover, and mutation to improve driving strategies.

Key Features:

  • 🧬 Genetic Algorithm-based Learning: No explicit rulesβ€”only evolution.
  • 🏎️ Real-time Driving Simulation: Watch cars learn and improve dynamically.
  • 🏁 Elitism & Crossover Techniques: Ensuring top-performing drivers evolve.

πŸ“Œ How It Works

The system starts with randomly initialized driving strategies. Over multiple generations, the genetic algorithm selects the best-performing cars, allowing them to pass their "genes" (weights of the neural network) to the next generation.

πŸ”₯ Step 1: Creating the Population

Each car in the simulation has its own neural network controller, initialized with random weights.

class Car {
    constructor() {
        this.brain = new NeuralNetwork([5, 8, 4]); // Sensor inputs β†’ Hidden layer β†’ Controls
    }
}

πŸ”„ Step 2: Evolution through Natural Selection

The best drivers are chosen based on distance traveled and collision avoidance.

function selectBestCars(cars) {
    return cars.sort((a, b) => b.fitness - a.fitness).slice(0, topPerformers);
}

Top drivers are selected, then "breeding" (crossover) occurs.

function crossover(parent1, parent2) {
    let child = new Car();
    child.brain.weights = mixWeights(parent1.brain.weights, parent2.brain.weights);
    return child;
}

🏎️ Step 3: Mutation for Exploration

To avoid premature convergence, we introduce random mutations in the neural network.

function mutate(car) {
    for (let i = 0; i < car.brain.weights.length; i++) {
        if (Math.random() < mutationRate) {
            car.brain.weights[i] += (Math.random() - 0.5) * mutationStrength;
        }
    }
}

🎯 Future Improvements

  • 🌍 Dynamic Environments: Training cars in variable road conditions.
  • πŸ€– Reinforcement Learning Hybrid: Combining GA with deep RL.
  • πŸš€ Hardware Acceleration: Optimizing for GPU-based physics simulations.

πŸ“₯ Get Started

Clone the repository and start evolving self-driving cars today!

git clone https://github.com/OneOfCosmosMostWanted/Auto_Driving_Simulation_Genetic-Algorithm.git
cd Auto_Driving_Simulation_Genetic-Algorithm
npm install

Run the simulation:

npm start

πŸ’‘ Want to contribute? Experiment with different genetic operators or hybrid learning techniques!

πŸ”— GitHub Repository