liveBook · Manning

Population

As explained in Grokking AI Algorithms, Second Edition

A population is the collection of candidate solutions maintained in an evolutionary algorithm at a particular generation.

A population is a collection of candidate solutions in one generation of an evolutionary algorithm. Think of it as several attempts at a puzzle being tested at once. Each candidate, or individual, is a chromosome made of genes. Keeping varied candidates lets the search explore different parts of a large solution space instead of relying on one attempt. section 5.22 section 4.2 section 4.4

A population of knapsacks

Suppose a knapsack has a 9 kg weight limit and eight available items. Represent each candidate with eight binary digits: 1 means include an item; 0 means leave it out. One candidate includes items 1, 3, and 7, weighing 9 kg and worth $14. Other strings represent other packing choices. Together, those strings form the population. section 4.2 section 4.4

A population changes as the algorithm runs:

  • Create an initial set of varied, random candidates. Check the problem’s constraints: an invalid candidate can be repaired, discarded, or given a poor fitness score. section 4.3 section 4.6
  • Score each individual with a fitness function. For a knapsack, higher total value is better only if the weight limit is respected; an overweight candidate can receive a score of zero. section 4.7
  • Select parents, produce offspring, then choose who enters the next generation. Population size is usually fixed, so adding offspring means removing some candidates. section 4.3 section 4.17

How many individuals are replaced is a design choice. A steady-state model replaces a portion each generation; a generational model replaces everyone. Keeping only the highest-scoring individuals, called elitism, can reduce diversity if those survivors are too similar. A varied population matters especially when a search might otherwise get stuck at a local best, though evaluating many candidates takes computation. section 4.9 section 4.17 section 4.2

def generate_initial_population(population_size):
    population = []
    for individual in range(0, population_size):
        individual = ''.join([random.choice('01')
        ➥for n in range(26)]) #A
        population.append([individual, 0, 0]) #B
    return population

This Python example builds an initial population by generating random 26-digit binary strings and storing each as an individual. It shows population creation, but does not itself check whether a candidate satisfies knapsack constraints. section 4.6

How Grokking AI Algorithms, Second Edition covers it

How deep it goes
Walks through encoding, random initialization, fitness scoring, and generation replacement, with a learning-oriented Python initialization example.
Examples
Uses binary-encoded knapsack candidates, overweight-solution penalties, and a Python function that generates random individuals.
What it assumes
Defines chromosomes and genes before using binary strings and Python arrays to represent candidate solutions.
What you can do after
Build it from scratch in Python by generating an initial population of binary-encoded candidates.
How it connects
Links population to chromosomes, genes, fitness functions, natural selection, and steady-state and generational models.

Also explained in

Compare how 9 books explain population →

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.