Natural Selection

Natural Selection preview image

This model is seeking new collaborators — would you please help?

1 collaborator



Tagged by Jesper Lindmarker about 4 years ago

natural selection 

Tagged by Jesper Lindmarker about 4 years ago

population dynamics 

Tagged by Jesper Lindmarker about 4 years ago

Parent of 1 model: Natural Selection R1
Visible to everyone | Changeable by everyone
Model was written in NetLogo 6.1.1 • Viewed 438 times • Downloaded 33 times • Run 0 times
Download the 'Natural Selection' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


A model of natural/artificial selection with bugs who compete for food. By controlling different aspects of the environment and gene mutation one can observe how the population of bugs adapt and the selection of genes differ.

The model shows adaption of a population through natural selection. There is no adaption on the agent level, rather it is probability that drives mutation of the genes which manifests as a battle of who can get more food and reproduce.


Bugs wander around the area looking for food. Moving consumes energy and if energy reaches zero the bug dies. Eating refills energy. If energy reaches 100 the bug will reproduce and lower it's energy to 50.

When reproducing, an exact copy of the bug will hatch. However, there is a chance set by sliders that each "gene" of the bug will mutate. Speed, size and vision can mutate higher or lower.

  • Higher speed can help a bug reach more food in shorter time but comes at a higher cost of energy.

  • Larger size allows bugs to eat smaller sized bugs (possible when double the size of another bug) which effectively supplies the environment with more possible food. Larger size also comes with a higher cost of energy.

  • Greater vision allows bugs to see food at a further distance and move towards it and has no higher cost of energy.



FOOD-VALUE and PREY-VALUE controls how much energy a bug receives from eating a piece of food and another bug (prey) respectively.

COST-OF-SPEED and COST-OF-SIZE controls how the cost of energy co-varies with speed and size. Meaning, how much more energy should be consumed by increasing size/speed.

MUTATION-RATE controls how likely it is that a gene will mutate between two generations.

MUTATION-AMOUNT controls how much a gene can change between two generations.

FOOD-GROWTH controls the regrowth of food. This could be said to indicate if the bugs live in an environment of abundance or scarcity.

Plots and monitors

The POPULATION plot on the left side shows population of bugs and food.

The SPEED, SIZE and VISION histograms show the distribution of values of the three genes.

The MEAN plots beneath them plots the mean value in the population of each gene. It also has a line for food-growth to visualize where changes were made during a run.

The BUGS PLOTTED BY SPEED AND SIZE plot each bug as a dot on a xy-plane with speed and size on the axes.


Remember, evolution takes a very long time. Thus, there is no end to the model. It is exploratory. For each run of the model, let it run for at least a couple thousands, or even tens of thousands of ticks to see the long term effect on the population.

There is a tradeoff between size and speed which makes it energy expensive to be both.

Predators appear as the difference in size between bugs reaches a ratio of 2 when the larger bugs can eat the smaller. When this happens, the large "family" of bugs tends to reproduce quickly and take over parts or the whole environment.

Notice how vision, without having a cost of energy, is still subject to natural selection and is not always optimal to increase. Why?

When reaching extreme states the population takes longer to adapt to a change in FOOD-GROWTH. Why?


  • Investigate how the population evolves at different levels of food-growth.

  • See if you can produce two different species co-existing e.g. one big slow and one small fast type of bug.

  • Let the population adapt to one level of food growth then set it to a new level and see how the population adapts. Note that this can take many thousands of ticks.

  • Move between the extreme values of food-growth.

  • Change the cost-of-speed/size and prey/food-value to see how this affects the propensity of adaptation and predation.

  • Explore how mutation rate/amount affects evolution.


The bugs have no flight behaviour in this model. This could be added to make the predator/prey relationship more authentic.

A cone of vision could be used instead of 360 which would be more realistic and decrease the risk of bugs changing direction too often.

Further sliders for starting energy and reproduction thresholds could be added.


  • Vision evolution
  • Bug Hunt Coevolution
  • Wolf sheep predation


This model was created as a part of the Introduction to Agent-Based Modeling course on Complexity Explorer:

Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

globals [ age ]

;; Set two breeds of agents, one for the bugs and one for food

breed [ food a-food ]
breed [ bugs bug ]

bugs-own [
energy ;; The resource bugs will be depleting and refilling by moving and eating
speed ;; The "gene" of movement speed for each bug
vision ;; The "gene" that decides how far a bug can see

to setup

to setup-world
  ask patches [
    set pcolor green + 2

to setup-bugs

  ;; Creating the bugs and setting their initial states to numbers that, after trials, suits the model
  create-bugs 100 [
    set color 10
    set speed .5
    set energy 50
    set vision 3
  ask bugs [
    set shape "bug"
    setxy random-xcor random-ycor]

to go

  ;; Two criteria for stop: One if no bugs are alive and one if any setting results in uncontrolled reproduction
  if not any? bugs [ stop ]
  if count bugs >= 2000 [ stop ]

  move-bug ;; Bugs move forward, change direction or towards food within their vision.
  eat-food ;; If standing on a patch where there is food, eats it
  eat-prey ;; If standing on a patch where there is prey, eats it
  check-death ;; Bugs check if their energy is zero, if yes they die
  reproduce ;; Bugs check if their energy is at or above the reproduction threshold. If yes they reproduce
            ;; with a 20% chance to mutate each gene and direction.
  regrow-food ;; Food grows at random places at a rate set in the interface


to move-bug
  ask bugs [

    ;; Make bugs consider the nearest bug with half the size of self "prey". If there is no such bug within vision, set prey to the closest piece of food within vision.
    let prey min-one-of other ((bugs in-radius vision) with [ (size * 2) < [size] of myself ]) [distance myself]
    if prey = nobody [
       set prey min-one-of (food in-radius vision)  [distance myself]

    ;; If there is any prey, bug or food, within vision: Move towards it.
    ifelse prey != nobody [
      face prey
      ifelse distance prey < speed [
        move-to prey
        fd speed
      ;; If no prey within vision, move forward at respective speed with a 20% chance of changing direction
      fd speed
      if random 100 < 20 [right random 360]

    ;; Set energy to decrease relative to current size and speed as well as the decided cost of each
    set energy energy - ((size * cost-of-size) * (speed * cost-of-speed))

to eat-food

  ;; If there is any food at current position, eat it and set energy to increase by current food-value
  ask bugs [
  let prey one-of food-here
  if prey != nobody [
      ask prey [ die ]
      set energy energy + food-value ]

to eat-prey
  ask bugs [

    ;; If there is any bugs with half the size of self at current position, eat it and set energy to increase by current prey-value
    let prey one-of other bugs-here with [ (size * 2) < [size] of myself ]
    if prey != nobody [
      ask prey [ die ]
      set energy energy + prey-value

to check-death ;; If energy is at zero, the bug dies
  ask bugs [
    if energy <= 0 [ die ]

to reproduce
  ask bugs [

    ;; If energy is above 100, decrease it by 50 and hatch a bug identical to self
    if energy > 100 [
      set energy energy - 50
        hatch 1 [

        ;; Each "gene" has a set chance of increasing or decreasing a set amount when being passed on to the offspring

        if random 100 < mutation-rate [
          ifelse random 100 < 50 [
            set speed speed * (1 + mutation-amount / 100) ][
            set speed speed * (1 - mutation-amount / 100)

        if random 100 < mutation-rate [
          ifelse random 100 < 50 [
            set size size * (1 + mutation-amount / 100) ][
            set size size * (1 - mutation-amount / 100)

        if random 100 < mutation-rate [
          ifelse random 100 < 50 [
            set vision vision * (1 + mutation-amount / 100) ][
            set vision vision * (1 - mutation-amount / 100)

to regrow-food

  ;; Create food at the rate of food-growth at random places.

  if random 3 > 1 [
      create-food food-growth [

      setxy random-xcor random-ycor
      set shape "circle"
      set color red
      set size 0.5

There are 2 versions of this model.

Uploaded by When Description Download
Jesper Lindmarker about 4 years ago Removed histograms with error Download this version
Jesper Lindmarker about 4 years ago Initial upload Download this version

Attached files

File Type Description Last updated
Natural Selection.png preview Preview for 'Natural Selection' about 4 years ago, by Jesper Lindmarker Download

This model does not have any ancestors.


Graph of models related to 'Natural Selection'