Genetic and Evolutionary Algorithms Tutorial
Learn Genetic and Evolutionary Algorithms step by step: 36 lessons in 9 chapters with examples, quizzes, diagrams and projects.
Start learning →
of lessons completed
Getting Started
- What Are Genetic Algorithms? Learn what a genetic algorithm is, why evolution makes a good search method, and when to use one, with runnable Python that compares random search to evolution.
- Evolutionary Computation and Its Vocabulary Learn the vocabulary of genetic algorithms (gene, chromosome, population, fitness, generation) and meet the evolutionary computation family: GA, ES, EP, GP and DE.
- Your First Genetic Algorithm in Python Build a complete genetic algorithm from scratch in Python: initialization, fitness, selection, crossover, mutation and elitism, tested on OneMax and a target string.
- The GA Loop, Parameters and Fitness Landscapes Learn how a genetic algorithm is structured, how to choose population size, mutation and crossover rates, when to stop, and why fitness landscapes trap searches.
Representation and Fitness
- Binary and Gray Encoding Learn how to encode numbers and choices as bit strings for a genetic algorithm, decode them, and why Gray code fixes the Hamming cliff of plain binary encoding.
- Real-Valued Encoding for Genetic Algorithms Encode continuous parameters directly as floating-point genes, handle bounds and mixed gene types, and optimize classic test functions with a real-coded GA in Python.
- Permutation Encoding for Ordering Problems Represent tours, schedules and orderings as permutations in a genetic algorithm, evaluate them on a traveling salesman problem, and see why bit-string operators break them.
- Designing Fitness Functions Learn how to design fitness functions that guide a genetic algorithm: minimization versus maximization, scaling, noise, shaping partial credit, and avoiding loopholes.
- Population Initialization and Diversity Learn how to create the first generation of a genetic algorithm, measure population diversity with Hamming and Euclidean distance, and detect premature convergence.
Selection
- Roulette Wheel and Stochastic Universal Sampling Implement fitness-proportionate (roulette wheel) selection and stochastic universal sampling in Python, see how it behaves, and learn its weaknesses with negative or skewed fitness.
- Tournament Selection Learn tournament selection, the most popular selection method in genetic algorithms: how tournament size sets selection pressure, with probabilistic and deterministic variants.
- Rank, Truncation and Boltzmann Selection Learn rank-based, truncation and Boltzmann selection, plus fitness scaling (windowing, sigma scaling), to control selection pressure independent of raw fitness values.
- Selection Pressure, Elitism and Takeover Time Quantify selection pressure with takeover time, compare generational and steady-state replacement, and use elitism, (mu+lambda) and crowding to balance speed against diversity.
Crossover and Mutation
- Crossover for Binary Strings Learn one-point, two-point, k-point and uniform crossover for bit-string genetic algorithms, with Python code, positional bias, and when to prefer each operator.
- Crossover for Real Values: Blend, Arithmetic and SBX Learn crossover operators for real-valued chromosomes: discrete, arithmetic, BLX-alpha blend and simulated binary crossover (SBX), with Python code and their effect on diversity.
- Crossover for Permutations: OX, PMX, Cycle and Edge Learn permutation crossover operators that always produce valid tours: order crossover (OX), partially mapped (PMX), cycle (CX) and edge recombination, with tested Python code.
- Mutation Operators: Bit-Flip, Gaussian and Polynomial Learn mutation for every encoding: bit-flip, uniform, Gaussian, polynomial and boundary mutation for numbers, plus swap, insert and inversion for permutations, with mutation-rate guidance.
- Parameter Control and Self-Adaptation Stop hand-tuning mutation and crossover rates: learn deterministic schedules, the 1/5 success rule, self-adaptive step sizes and feedback control, with Python examples.
Constraints and Multi-objective
- Handling Constraints with Penalty Functions Learn how to keep a genetic algorithm inside the rules. Build static, dynamic and adaptive penalty functions and see how penalty strength changes the search.
- Repair Operators and Decoders Keep every individual feasible by construction. Learn repair functions, random-key decoders and greedy decoders with runnable knapsack and scheduling examples.
- Multi-objective Optimization and Pareto Dominance Optimize several goals at once. Learn Pareto dominance, non-dominated sorting and the Pareto front, plus the weighted-sum trap, with runnable Python examples.
- NSGA-II from Scratch Build NSGA-II, the classic multi-objective genetic algorithm, in under 80 lines of Python. Covers ranking, crowding tournaments, elitism and the ZDT1 benchmark.
Swarm and Other Metaheuristics
- Particle Swarm Optimization Learn how particle swarm optimization works: velocity, inertia, personal and global best. Build PSO in NumPy, tune its parameters and compare it with a genetic algorithm.
- Differential Evolution Learn differential evolution, a powerful evolutionary optimizer for real-valued problems. Build DE/rand/1/bin from scratch, tune F and CR, and use scipy.optimize.
- Simulated Annealing and Hill Climbing Compare single-solution metaheuristics with genetic algorithms. Code hill climbing, random restarts and simulated annealing in Python, and learn when each wins.
- Ant Colony Optimization Understand ant colony optimization (ACO): pheromone trails, heuristic information and evaporation. Build an Ant System for the traveling salesman problem in Python.
Genetic Programming
- Genetic Programming: Evolving Programs Learn genetic programming (GP): evolve programs as expression trees. Build trees, evaluate them, apply subtree crossover and mutation, and run a tiny GP in Python.
- Symbolic Regression with Genetic Programming Discover a formula from data with genetic programming. Build a full symbolic regression GP with tournament selection, depth limits and parsimony pressure in Python.
Applications and Tuning
- Tuning GA Parameters Learn to tune genetic algorithm parameters with evidence: population size, mutation and crossover rates, tournament size and elitism, using repeated runs and diversity metrics.
- Fitness Caching and Parallel Evaluation Make genetic algorithms faster. Cache repeated fitness evaluations, vectorize with NumPy, evaluate in parallel with multiprocessing, and use cheap surrogate models.
- Feature Selection with a Genetic Algorithm Use a genetic algorithm to choose the best subset of features for a machine learning model. Bit-mask chromosomes, cross-validated fitness and a size penalty in Python.
- A Genetic Algorithm for the Traveling Salesman Problem Solve the traveling salesman problem with a genetic algorithm in Python. Permutation encoding, order crossover, swap and inversion mutation, and a 2-opt memetic upgrade.
Projects and Practice
- Project: Knapsack Optimizer Build a complete 0/1 knapsack optimizer with a genetic algorithm, check it against the exact dynamic programming answer, and report gap, speed and reliability.
- Project: Evolve a Neural Network Train a small neural network without backpropagation. Evolve its weights with a genetic algorithm on XOR and a two-class dataset, using only NumPy.
- Practice: Benchmark GA, DE and PSO Compare a real-coded genetic algorithm, differential evolution and particle swarm optimization fairly on standard benchmark functions, with equal budgets and many seeds.
- Practice Exercises and Where to Go Next Test your skills with hands-on genetic algorithm exercises (starter code included), a design checklist for new problems, and a roadmap of libraries and topics to explore next.