liveBook · Manning

Genetic algorithm

As explained in Grokking AI Algorithms, Second Edition

A genetic algorithm is an evolutionary optimization technique that improves candidate solutions through selection, recombination, and mutation.

A genetic algorithm searches for a good solution by evolving a population of candidate solutions. It starts with varied candidates, scores how well each works, and uses selected candidates to produce the next generation. It is useful when there are too many possibilities to check one by one, but it does not guarantee the best possible answer. section 4.3 section 4.2

The basic cycle is:

  • Create a population of random candidate solutions. Represent each as a chromosome—a sequence of genes describing its choices—and account for the problem’s constraints. section 4.6
  • Measure fitness: give each candidate a score for how well it solves the problem. section 4.7
  • Select parents based on fitness, then combine their genes and introduce random mutations to make offspring. Use offspring to populate the next generation, repeating the cycle to seek better solutions. section 4.3

Packing a knapsack

Suppose a bag can carry 9 kg and each item has a weight and value. A candidate can use a 1 for an item it includes and a 0 for one it leaves out. Pearls (3 kg, value 4), a crown (4 kg, value 5), and a ring (2 kg, value 5) make a valid 9 kg candidate worth 14. A fitness function adds the values of included items, but gives an overweight candidate a score of 0. That penalty keeps an attractive but unusable packing choice from scoring well. section 4.2 section 4.6 section 4.7

Table 4.1 Knapsack weight capacity: 9 kg

Item ID

Item name

Weight (kg)

Value ($)

1

Pearls

3

4

2

Gold

7

7

3

Crown

4

5

4

Coin

1

1

5

Axe

5

4

6

Sword

4

3

7

Ring

2

5

8

Cup

3

1

Use the item table to try other 9 kg combinations and compare their values before considering how an algorithm might find them. section 4.2

Configuration matters. A larger population can offer more diversity but costs more computation per generation; the chromosome encoding, fitness function, and parent-selection method also affect results. Because initialization, selection, and mutation involve randomness, a run may find a good answer without finding the global best. Similar searches can help with delivery routes, truck packing, production order, and feature selection in machine learning. section 4.20 section 4.3 section 4.2 section 4.21

How Grokking AI Algorithms, Second Edition covers it

How deep it goes
Walks through population creation, fitness scoring, and the overall life cycle; defers alternative encoding, crossover, mutation, and selection techniques.
Examples
Uses knapsack item combinations, Python population and fitness snippets, delivery routes, truck packing, and feature selection.
What it assumes
Introduces populations and fitness through a concrete knapsack problem rather than requiring advanced mathematical background.
What you can do after
Build it from scratch using random candidate generation and fitness scoring, then configure the search for solution quality and computational cost.
How it connects
Connects evolutionary algorithms with fitness functions, the Knapsack Problem, brute-force search, and local versus global optima.

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 Grokking AI Algorithms, Second Edition. The section links point to the parts of the book it draws on.