My Profile Photo

Siying Chen


GIS, sustainable transportation, and more livable cities please.


Agent-based Spatial Pattern Modeling Using NetLogo Part 1

I developed this model to simulate the interaction between two groups of agents (or breeds), turtles and grass. This model was inspired by the “Wolf Sheep Predation Model”.

Here are some basic rules.

  • A turtle has energy.
  • When a turtle eats a unit of grass, it gains energy from grass.
  • When a turtle reproduces a new turtle, it loses energy.
  • When a turtle moves, it loses energy too.
  • If a turtle has no energy, it dies. Otherwise, it survives.
  • Last but not least, the grass might be able to regrow.

Demo:

energy keeps track of when the turtle is ready to reproduce and when it dies.

turtles-own [energy]

The initial number of turtles is defined by number. number-of-ticks decides how many ticks the model runs before it stops.

to setup
  clear-all
  setup-patches
  setup-turtles
  reset-ticks
end


to setup-patches
  ask patches [ set pcolor green ]
end


to setup-turtles
  create-turtles number [ setxy random-xcor random-ycor ]
end


to go
  if ticks >= number-of-ticks [ stop ]
  move-turtles
  eat-grass
  reproduce
  check-death
  regrow-grass
  tick
end

energy-to-move defines how much energy a turtle loses when it moves each time.

to move-turtles
  ask turtles [
    right random 360
    forward 1
    set energy energy - energy-to-move
  ]
end

Users can define how much energy a turtle can get from eating a patch of grass using the energy-from-grass slider. If the show-energy switch is on, a label will be placed on each alive turtle to show its energy level. If the show-energy switch is off, the label is set to be an empty text.

to eat-grass
  ask turtles [
    if pcolor = green [
      set pcolor black
      set energy (energy + energy-from-grass)
    ]
  ifelse show-energy?
    [ set label energy ]
    [ set label "" ]
  ]
end

If a turtle has enough to reproduce, which means its current energy is higher than the birth-energy defined, an offspring will be generated by attaining the birth-energy.

to reproduce
  ask turtles [
    if energy > birth-energy [
      set energy energy - birth-energy
      hatch 1 [ set energy birth-energy]
    ]
  ]
end

A turtle dies if it has no energy left.

to check-death
  ask turtles [
    if energy <= 0 [ die ]
  ]
end

The grass grows at a rate of grass-regrowth out of 100. This is visualized by changing the patch color to green.

to regrow-grass
  ask patches [
    if random 100 < grass-regrowth [ set pcolor green ]
  ]
end