Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 37 ยท Projects and Practice

Project: Natural Selection Simulation

Build a simulation of a population of individuals with a heritable trait, apply selection and mutation each generation, and plot how the population changes.

In this project you will build a small program that models natural selection from first principles. Each individual has a number that stands for a heritable trait (think of the shade of a beetle, from 0 for pale to 1 for dark). The program repeats four steps each generation: selection, reproduction, inheritance and mutation. You will then change the settings and explain what happens, which is the best way to understand each part of the argument.

This is a model, which means it is a simplified imitation of nature. Its results come from the rules that we write, not from an experiment on real animals.

Step 1: The rules

  • The population has N = 200 individuals.
  • Each has a trait t between 0 and 1. At the start, all traits are random.
  • The environment favors dark individuals: survival probability is 0.2 + 0.6 x t (so from 0.2 to 0.8).
  • Survivors are chosen at random from the population with those probabilities, and parents are picked among survivors to refill the population to N.
  • Each child's trait equals its parent's trait plus a small random mutation (standard deviation 0.02), kept between 0 and 1.
Python Version 1: one run, text output

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The mean trait rises toward 1 because darker individuals have a higher survival chance and pass their trait on.

Step 2: Plot and compare

Now run the model several times with different random seeds, and with different strengths of selection. We make the strength a parameter: survival = base + strength x t.

Python Version 2: plot selection of different strengths

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With no selection (strength 0) the lines wander because of chance (drift) and do not trend. Weak selection gives a slow upward trend, and strong selection a fast one.

Step 3: Experiments to try

Change one variable at a time and write down your prediction before running.

  1. Population size. Set N = 20 and then N = 2000. Which gives more variation between runs? (Think about drift.)
  2. Mutation rate. Set mutation = 0. After many generations, can the mean keep rising? Why or why not? Selection can use only the variation that exists.
  3. Changing environment. After generation 30, reverse the favored direction (0.8 - 0.6 x t). How long does the population take to turn around?
  4. Antibiotics. Replace the trait with "resistance" and make survival 0.05 + 0.9 x t. Compare with the antibiotic lesson.
Python Version 3: environment reverses at generation 30

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The population turns around because the variation is still there to select from. If variation had been lost earlier (low mutation, small N), the turnaround would be slower or impossible.

Reflect

Write a short paragraph answering: Which parts of Darwin's argument (overproduction, variation, inheritance, differential survival) were represented in the code? Which were not? A strong answer notes that the model keeps the population constant and ignores sex, which real populations do not.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. In the model, what happens if the mutation size is 0 and all individuals begin with similar traits?

    Selection can only choose among existing variation.
  2. 2. With no selection (strength 0), why does the mean still wander?

    Random sampling of parents changes the average from generation to generation.
  3. 3. Which population size gives the largest random swings in the mean trait?

    Drift is strongest in small populations.

Summary

  • A simple model needs only variation, inheritance, differential survival and reproduction to show adaptation.
  • Selection strength sets the speed of change; with no selection, drift gives random wandering.
  • Variation (from mutation) is the fuel for future change.
  • Models are simplified: say what they leave out.

Next: Project: Hardy-Weinberg Calculator