Natural Selection R1

No preview image

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

1 collaborator

Rg_400x400 Ricardo Gonzalez (Author)



Tagged by Ricardo Gonzalez almost 4 years ago

natural selection 

Tagged by Ricardo Gonzalez almost 4 years ago

population dynamics 

Tagged by Ricardo Gonzalez almost 4 years ago

Child of model Natural Selection preview imageNatural Selection
Visible to everyone | Changeable by everyone
Model was written in NetLogo 6.1.1 • Viewed 235 times • Downloaded 27 times • Run 0 times
Download the 'Natural Selection R1' modelDownload this modelEmbed this model

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


[This is a child version of the original model. Go below for a description of the changes: "Description of Version R1". Other than this, the info tab is as per the original].

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.



In the original model, the bugs can see within a radius (vision) from theirselves, at full 360 deg. In version R1 the bugs can see within a circular sector in front of them (in-cone instead of in-radius). The view sector is defined by an angle and a radius whose original values are introduced with a slider. The radius is subject to genetic evolution (as in the original model), but the area of the view sector (PIradius^2angle/360) is constant. Therefore, an increase of the view radius by genetic evolution implies a decrease of the view angle.


In the original model the prey is the nearest bug with at most half one's size; if there is no such bug, then the attention falls to the nearest food. In version R1 the hunter bug selects the nearest target among three prey types: bugsalive (of appropriate size), bugsdead (of all sizes), or food. When a bug-eats-bug, the energy that the eater obtains is proportional to the size of the prey. The ratio between prey and hunter sizes, which is defined as 2 in the original model, is now a global variable through a slider.


The original mode allows eating if the prey and the hunter are on the same patch. This is changed in version R1: they must be within a predefined given distance (which is set as speed (speed*inter-tick time), but could be different. Food preys are identified by yellow color.

Changes in direction

In the original model the bugs with no prey in sight change directions with a chance of 20%, and the new heading can be any (0-360 deg). In version R1 the bugs retain the same probability of changing direction, but they can only move within 2 times of their vision angle, not 360 deg.

Cost of size and speed

In the original model the energy costs of size and speed are directly proportional to size and speed. In version R1 the relationship is not linear, but follows a power expression a bit more realistic: more energy efficient wrt size, less energy efficient wrt speed. The relationship between size and speed energy cost is now not multiplicative but additive.


In the original model bugs that reach an energy of 0 die. In this version a bug that is "feeble" reduces its activity (speed) and then may more easily become a prey, but only "dies" (dissapears from the model) when eaten. Therefore, the original check-death procedure is cancelled. The move and eat procedures are also reorganized. The level of "feebleness" could be related to the size, but it is fixed so far. Two levels of energy wrt activity are defined: one which reduces the speed (and sets color to grey), and another which halts the bug's activity (and sets color to white).


In the original model age is not considered. In version R1 the bugs have age measured from the ticks at which they were created. At a given age, they reduce their mobility, and finally they cease moving. They only "die" (dissapear from the model) when eaten.

Plots and monitors

New buttons, sliders, plots and monitors are introduced, or modify the original ones.


  • 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

;; Global variables.
globals [ vision_area bugs_dead p_food]

;; Set two breeds of agents, one for the bugs and one for food.
breed [ food a-food ]
breed [ bugs bug ]

[ energy ;; The resource bugs will be depleting and refilling by moving and eating.
  speed ;; The "gene" of movement speed for each bug.
  vision_r ;; The "gene" that decides how far a bug can see.
  vision_a ;; The angle of vision that the bug has.
  age ;; The ticks at which they were created.

to setup

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

to setup-bugs
  ;; Initialize global variables:
  set vision_area 1.5 ;; vision_area: area of a circular sector with angle = 60 deg and radius = 3, wo considering PI.
  set bugs_dead 0 ;; Number of dead bugs.
  set p_food -1 ;; Reference for the probability with which the food will grow.

  ;; Creating the bugs and setting their initial states to numbers that, after trials, suits the model.
  create-bugs 100
  [ set color 0
    set speed .5
    set energy (25 + random 50)
    set vision_r 3
    set vision_a 60
    set age (-50 - random 100)]
  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 ]
  ;; Otherwise:
  move-eat ;; Bugs move forward, change direction towards food/prey within their vision, and then eat it when under reach.
  reproduce ;; Bugs check if their energy is at or above the reproduction threshold, if yes they reproduce.
  check-age ;; Bugs do not live forever!
  grow-food ;; Food grows at random places at a rate set in the interface.


to move-eat
  ;; Reduce the activity of feeble bugs:
  ask bugs with [energy <= 5]
  [ set speed min list speed 0.1
    set color 5]

  ask bugs with [energy < 1]
  [ set speed 0
    set color 9.9]

  ;; Move all, in relation to their energy/speed:
  ask bugs with [energy > 0]
  [ ;; Make bugs consider as prey the nearest bug within their size-range:
    let prey_b min-one-of other ((bugs in-cone vision_r vision_a) with [ (size * size_ratio) < [size] of myself ]) [distance myself]
    ;; Let bugs also consider as prey the dead bugs:
    let prey_d min-one-of other ((bugs in-cone vision_r vision_a) with [ energy < 1 ]) [distance myself]
    ;; Finally, bugs also consider the food within their vision:
    let prey_f min-one-of (food in-cone vision_r vision_a)  [distance myself]

    ;; Target the nearest prey (bug, dead or alive, or food):
    let prey min-one-of (turtles with [member? self (list prey_b prey_d prey_f)]) [distance myself]
    ;; Go for the prey:
    ifelse prey != nobody
    [ face prey
      if ([breed] of prey = food) [ask prey [set color 45]] ;; Mark the food.
      ifelse distance prey < speed
      [ ;; Jump to the prey:
        setxy  [xcor] of prey [ycor] of prey
        ;; Eat:
        ifelse ([breed] of prey = food)
        [ set energy energy + food-value]
        [ ifelse [energy] of prey > 1
          [ set energy energy + ([size] of prey) * prey-value]
          [ set energy energy + ([size] of prey) * prey-value * 0.5] ;; Assume dead corpses are worse than live bodies!
          set bugs_dead bugs_dead + 1]
        ask prey [ die ]
      [ fd speed]
    [ ;; If no prey within vision, move forward at respective speed with a 20% chance of changing direction.
      let rnd random 100
      if rnd < 20 [ifelse rnd < 10 [right random vision_a] [right random (-1 * vision_a)]]
      fd speed

    ;; Set energy to decrease relative to current size and speed as well as the specified cost of each.
    set energy energy - ((cost-of-size * size ^ 0.9) + (cost-of-speed * size * speed ^ 1.2))

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
      [ set age ticks
        ;; 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_r vision_r * (1 + mutation-amount / 100) ]
          [ set vision_r vision_r * (1 - mutation-amount / 100) ]
          set vision_a min list (vision_area * 360. / (vision_r ^ 2)) 360]

to check-age
  ask bugs with [age > 0]
  [ if (ticks - age > bug_age - 10)
    [ set speed min list speed 0.5
      set energy 5
      set color 85]
    if (ticks - age > bug_age)
    [ set speed min list speed 0
      set energy 0
      set color 105]

to grow-food
  ;; Create food at the rate of food-growth at random places.
  if random 3 > p_food
  [ create-food food-growth
    [ setxy random-xcor random-ycor
      set shape "circle"
      set color red
      set size 0.5]
  set p_food 1 ;; for the first call p_food = -1, for the rest 1.

There are 4 versions of this model.

Uploaded by When Description Download
Ricardo Gonzalez almost 4 years ago Main changes: age, cone vision, prey extension, energy costs Download this version
Ricardo Gonzalez almost 4 years ago Main changes: age, cone vision, prey extension, energy costs Download this version
Ricardo Gonzalez almost 4 years ago Main changes: age, cone vision, prey extension, energy costs Download this version
Ricardo Gonzalez almost 4 years ago Main changes: age, cone vision, prey extension, energy costs Download this version

Attached files

File Type Description Last updated
Natural_Selection R1 RG view.png png view almost 4 years ago, by Ricardo Gonzalez Download
Natural_Selection R1 RG.png png panel view almost 4 years ago, by Ricardo Gonzalez Download

Parent: Natural Selection

This model does not have any descendants.

Graph of models related to 'Natural Selection R1'