liveBook · Manning

Genetic algorithm

As explained in Optimization Algorithms: AI techniques for design, planning, and control problems

A genetic algorithm is a stochastic evolutionary optimization algorithm that generates candidate solutions through selection and variation of a population.

A genetic algorithm (GA) searches for a good solution by evolving a population of candidate solutions. Instead of following one fixed path through the search space, it evaluates candidates, selects parents, and uses crossover and mutation to produce new candidates. Random choices help it explore, but also mean that improvement is not guaranteed on every run and an optimum may be missed. section 7.3 section 2.2

A basic GA repeats this cycle:

  • Initialize a population. Encode each candidate in a form suited to the problem, such as a binary, real-valued, or permutation vector. section 7.2.3 section 7.3
  • Evaluate each candidate with a fitness function, then select parents. Selection may be random or weighted by fitness. section 7.3 section 7.4
  • Apply crossover to combine parents and mutation to introduce random changes. Check and repair offspring when the problem has constraints. section 7.2.3 section 7.3
  • Evaluate offspring, select survivors, and repeat until a stopping condition is met, such as a generation limit, adequate fitness, or stalled improvement. section 7.3
Algorithm 7.1 Genetic algorithm
Initialization: Randomly generate an initial population M(0)
  
Evaluate all individuals: Compute and save the fitness f(m) for each individual in the current population M(t)
  
While termination criteria are not met
    Select parents: Define selection probabilities p(m)for each individual p in M(t)
    Apply crossover: Generate M(t+1) by probabilistically selecting individuals from M(t) to produce offspring via genetic operators
    Apply mutation: Introduce random changes to individuals
    Evaluate: evaluate the fitness of the new individuals
    Select survivors: select individuals to form the next generation

The pseudocode is a compact guide to the order of evaluation, parent selection, variation, and survivor selection. section 7.3

Choosing a ticket price

Suppose a ticket price must be chosen from $75 to $235 to maximize profit. One Python implementation encodes prices as eight-bit chromosomes, evaluates each decoded price with profit(x) = −20x² + 6200x − 350000, and evolves the population. Its reported result is a $155 price with $130,500 profit. A second implementation defines the pricing problem in pymoo and configures its GA with sampling, crossover, mutation, and a 100-generation run; it reports the same price and profit. section 7.4

Match the representation and operators to the decision. A traveling-salesman route is a permutation, not a binary price: a pymoo example evaluates route length across 20 US cities and uses permutation sampling, order crossover, inversion mutation, and repair to fix the starting city. This changes the candidates and their variation operators without changing the evaluate–select–vary cycle. section 8.6 section 7.2.3

Tune population size, crossover, mutation, and stopping rules experimentally. Crossover shares traits between candidates; mutation maintains diversity and can help prevent premature convergence. A high crossover rate with a lower mutation rate is recommended as a starting balance between exploring new areas and refining existing solutions. Do not treat a found solution as proof of global optimality. section 7.5 section 2.2

How Optimization Algorithms: AI techniques for design, planning, and control problems covers it

How deep it goes
Works through representations, fitness, selection, variation, survival, termination, and Python implementations; does not establish a guarantee of optimality.
Examples
Uses a binary ticket-pricing implementation and a permutation-based, 20-city traveling-salesman solver.
What it assumes
Introduces genetic-algorithm building blocks before implementation, while using Python functions and lists without teaching Python syntax from zero.
What you can do after
Build it from scratch for binary ticket pricing, or apply it via pymoo to pricing and route optimization.
How it connects
Connects evolutionary computation and stochastic search with fitness functions, constrained optimization, exploration and exploitation, and the traveling salesman problem.

Also explained in

Compare how 7 books explain genetic algorithm →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Optimization Algorithms: AI techniques for design, planning, and control problems. The section links point to the parts of the book it draws on.