3. Practice - NetLogo Simulations
In our last practice, we covered:
map
, filter
, and reduce
That’s quite a lot! Now, let’s dive into how these concepts are applied in simulations using the Models Library.
The NetLogo Models Library is a comprehensive resource included with NetLogo, containing pre-built simulation models spanning diverse disciplines such as biology, economics, physics, and social sciences.
Your Model Library should look somehow like this under File > Models Library.
File > Models Library > Sample Models > Earth Science > Fire
Interface Tab
The Fire Model is a simple yet illustrative agent-based simulation used to study the spread of fire through a forest, represented on a 2D grid. The environment consists of three primary elements:
The setup
button initializes the forest grid based on a key variable called density
. This global variable determines the proportion of the grid occupied by trees and is controlled by a slider in the interface. The density
variable is only utilized during the setup phase and remains constant throughout the simulation, even if the slider is adjusted during runtime.
The go
button initiates the core simulation loop, which governs the spread of fire:
The model includes a monitor labeled percent burned
, which reports the percentage of the grid area affected by fire. This value is calculated by a reporter function that dynamically tracks the burned areas during the simulation.
Let's run the simulation with the initial density
value of 57%.
setup
command in the Interface tab and press the go
button to start the simulation.
Observe what happens to the forest:
In modeling, this epidemic threshold is more accurately referred to as a phase transition—a point where a small change in an input parameter causes a dramatic shift in the system's behavior, leading to the collapse of a previously stable state.
For this simulation, there is a closed-form solution: with a tree density of 57%, there is nearly a 0% chance of the fire reaching the right side of the grid. However, as density increases to 62%, this probability jumps to nearly 100%.
Reference: Phase Transition Robertson, Duncan & Caldart, Adrián. (2008). Natural Science Models in Management: Opportunities and Challenges. E:CO Emergence: Complexity and Organization. 10.
globals [
initial-trees ;; how many trees (green patches) we started with
burned-trees ;; how many have burned so far
]
breed [fires fire] ;; bright red turtles -- the leading edge of the fire
breed [embers ember] ;; turtles gradually fading from red to near black
to setup
clear-all
set-default-shape turtles "square" ;; turn turtles into square shapes
ask patches with [(random-float 100) < density] ;; make some green trees
[ set pcolor green ]
ask patches with [pxcor = min-pxcor] ;; make a column of burning trees
[ ignite ]
set initial-trees count patches with [pcolor = green] ;; set tree counts
set burned-trees 0
reset-ticks
end
;; creates the fire turtles
to ignite ;; patch procedure
sprout-fires 1
[ set color red ]
set pcolor black
set burned-trees burned-trees + 1
end
;; achieve fading color effect for the fire as it burns
to fade-embers
ask embers
[ set color color - 0.3 ;; make red darker
if color < red - 3.5 ;; are we almost at black?
[ set pcolor color
die ] ] ;; Removes the turtle, only the black patch remains
end
to go
if not any? turtles ;; either fires or embers
[ stop ]
ask fires
[ ask neighbors4 with [pcolor = green]
[ ignite ]
set breed embers ]
fade-embers
tick
end
Run the setup
command in the Interface tab and press go
to start the simulation.
density
for which a phase transition happens. How does it compare to the original density threshold?The Preview Commands Editor in NetLogo is a helpful tool that allows you to test your setup and go commands before running an experiment. It ensures that your commands are valid and that the model behaves as expected, helping to catch errors early and avoid wasting time on misconfigured experiments.
BehaviorSpace in NetLogo is a tool that allows you to run and analyze parameterized experiments by automating the execution of your model with different variable combinations. The Preview Commands Editor is connected to BehaviorSpace because it allows you to validate and fine-tune the setup and go commands used in your experiment before running it.
Pressing the New button brings up the following window:
["density" 30 40 50 60 70]
100
to average the results over 100 runs at each density value.count fires
Each experiment in NetLogo can be exported as an .xml file, allowing it to be reused or shared later to ensure reproducibility.
NetLogo also efficiently bundles all components—interface elements (in XML), the Info tab, and the Code tab—into a single .nlogo file for easy management and distribution.
NetLogo includes a built-in documentation tool that helps users effectively document their models. This tool's structure can also serve as a skeleton for documenting other projects. It typically follows the structure below:
Utilizes Markdown syntax, just like this document.
percent-burned
that calculates and reports the percentage of burned trees in the simulation. Modify the code to use neighbors
instead of neighbors4
, allowing the fire to spread to all 8 neighboring patches.
density
set to 37%.percent-burned
values across all runs.to-report percent-burned
report (burned-trees / initial-trees) * 100
end
Run the simulation and observe how the fire spreads:
density
percentage at which the phase transition occurs when fire spreads from all directions.ask patches with [(pxcor = min-pxcor) or (pxcor = max-pxcor) or (pycor = min-pycor) or (pycor = max-pycor)]
[ ignite ]
This means that fire can spread not only to immediate neighbors but also to the neighbors of those neighbors.
to go
if not any? turtles ;; either fires or embers
[ stop ]
ask fires
[ ;; Spread to all patches within a radius of 2
ask patches in-radius 2 with [pcolor = green]
[ ignite ]
set breed embers
]
fade-embers
tick
end
Modify the code so that trees can regrow with a certain percentage per step, controlled by a slider (e.g., regrowth-rate
).
go
procedure to allow patches (previously burned or empty) to regrow into trees based on the slider value.to go
if not any? turtles ;; either fires or embers
[ stop ]
ask fires
[ ;; Spread to all patches within a radius of 2
ask patches in-radius 2 with [pcolor = green]
[ ignite ]
set breed embers
]
regrow-trees
fade-embers
tick
end
to regrow-trees
ask patches with [pcolor < red - 3.5]
[
if random-float 100 < regrowth
[ set pcolor green ] ;; Regrow a tree
]
end