NetLogo is an open source agent-based modeling tool that is relatively simple to use. It is ideal for modeling complex systems that contain hundreds or thousands of agents interacting simultaneously. It allows users to explore the relationship between micro-level agents and macro-level emergent behaviors that occur. The language was heavily influenced by Logo, and is designed for users from many disciplines. It is commonly used by economists, anthropologists, and physicists, just to name a few. You don’t need to know other programming languages to work with and understand the programs designed within NetLogo. For this reason, it is ideally suited for conveying the results of complex simulations to individuals with relatively little knowledge of how to program. This post will help you get started.
The interface makes it possible for users to interact with the variables within a simulation and visualize the results without having to look at the code itself. The language reads like English, making it relatively easy for a layperson to understand the functionality of each line of code. In addition, NetLogo contains an extensive models library that provides sample programs from all different disciplines, which is very useful for the purpose of teaching and learning. It can be downloaded here (https://ccl.northwestern.edu/netlogo/).
NetLogo is divided into three windows: Interface, Info, and Code. The primary function of the Interface is for visualization and interaction. It contains various display and plotting tools, allows the user to interact with specified variables, and features a command line.
The Info window allows the author to provide detailed context for each simulation, and explanations for each section of code. This is especially helpful for conveying the specifics of the code to an untrained audience.
The Code window is self-explanatory; it contains the heart of the simulation—the code itself. Supplementary to the Info window, the author can also comment on each line of code for a more comprehensive understanding of the interactions occurring.
I will use an example to elucidate the basic features typically present in a NetLogo simulation. (The example can be found under File > Models Library > Biology > Ants)
It is easiest to see what is happening in the simulation by simply running it. To do this, click the Setup button in the Interface window, and then the Go button.
When the Setup button is clicked, the Interface will change. The three blue circles represent piles of food, and the purple circle represents the ant nest. When the Go button is clicked, the simulation will start, and red ants will begin moving about the world (a 2D Cartesian grid in this case).
In this example, a colony of ants searches for food. If an ant finds food, it carries it back to the nest. As it does so, it drops a chemical (represented by the green coloring) that affects the other ants in the colony. You can change the diffusion and evaporation rates of the chemical by adjusting the DIFFUSION-RATE and EVAPORATION-RATE sliders, as well as the population. By adjusting these variables, you can observe the resulting changes in consumption rate.
Setting up the simulation
NetLogo is a 2D world that is based on three different types of prototypical agents: patches, turtles, and the observer. In our example, the ants represent the turtles, and patches are the individual squares that make up the 2D grid. The observer oversees the interactions that occur between the agents and the patches. Variables within the simulation can be attributed to either the agents, patches, or both. The typical structure of the code contains to setup procedures (corresponding to the Setup button), and to go procedures (corresponding to the Go button).
Before establishing setup and go procedures, we first must define variables that are attributed to the patches. Patches-own allows us to do this.
patches-own [ chemical ;; amount of chemical on this patch food ;; amount of food on this patch (0, 1, or 2) nest? ;; true on nest patches, false elsewhere nest-scent ;; number that is higher closer to the nest food-source-number ;; number (1, 2, or 3) to identify the food sources ] Every time the Setup button is clicked, to setup is executed. For the turtles, this establishes the shape, size and color, as well as the population variable (which is tied to the slider within the Interface).;;;;;;;;;;;;;;;;;;;;;;;; ;;; Setup procedures ;;; ;;;;;;;;;;;;;;;;;;;;;;;; to setup clear-all set-default-shape turtles "bug" create-turtles population [ set size 2 ;; easier to see set color red ] ;; red = not carrying food setup-patches reset-ticks end to setup-patches ask patches [ setup-nest setup-food recolor-patch ] end
To setup calls to setup-patches. This establishes the nest and food sources, as well as the coloring characteristics of the chemical that is dropped by the turtles. (It is possible to do this directly under to setup; however, structuring the code in this way improves readability drastically.)to setup-nest ;; patch procedure ;; set nest? variable to true inside the nest, false elsewhere set nest? (distancexy 0 0) < 5 ;; spread a nest-scent over the whole world -- stronger near the nest set nest-scent 200 - distancexy 0 0 end to setup-food ;; patch procedure ;; setup food source one on the right if (distancexy (0.6 * max-pxcor) 0) < 5 [ set food-source-number 1 ] ;; setup food source two on the lower-left if (distancexy (-0.6 * max-pxcor) (-0.6 * max-pycor)) < 5 [ set food-source-number 2 ] ;; setup food source three on the upper-left if (distancexy (-0.8 * max-pxcor) (0.8 * max-pycor)) < 5 [ set food-source-number 3 ] ;; set "food" at sources to either 1 or 2, randomly if food-source-number > 0 [ set food one-of [1 2] ] end to recolor-patch ;; patch procedure ;; give color to nest and food sources ifelse nest? [ set pcolor violet ] [ ifelse food > 0 [ if food-source-number = 1 [ set pcolor cyan ] if food-source-number = 2 [ set pcolor sky ] if food-source-number = 3 [ set pcolor blue ] ] ;; scale color to show chemical concentration [ set pcolor scale-color green chemical 0.1 5 ] ] end
Running the simulation
When the Go button is clicked, to go is executed at every tick. It defines how the ants move from patch to patch based on whether or not they are carrying food, as well as the diffusion and evaporation of the chemical dropped on the patches.;;;;;;;;;;;;;;;;;;;;; ;;; Go procedures ;;; ;;;;;;;;;;;;;;;;;;;;; to go ;; forever button ask turtles [ if who >= ticks [ stop ] ;; delay initial departure ifelse color = red [ look-for-food ] ;; not carrying food? look for it [ return-to-nest ] ;; carrying food? take it back to nest wiggle fd 1 ] diffuse chemical (diffusion-rate / 100) ask patches [ set chemical chemical * (100 - evaporation-rate) / 100 ;; slowly evaporate chemical recolor-patch ] tick end
If an ant is carrying food, it drops the chemical onto the patch it is on, and heads toward the nest. If it is not carrying food and is on a patch that contains the chemical, it will head in the direction where the chemical is strongest. If there is no chemical around, it will continue to move randomly (to wiggle).to return-to-nest ;; turtle procedure ifelse nest? [ ;; drop food and head out again set color red rt 180 ] [ set chemical chemical + 60 ;; drop some chemical uphill-nest-scent ] ;; head toward the greatest value of nest-scent end to look-for-food ;; turtle procedure if food > 0 [ set color orange + 1 ;; pick up food set food food - 1 ;; and reduce the food source rt 180 ;; and turn around stop ] ;; go in the direction where the chemical smell is strongest if (chemical >= 0.05) and (chemical < 2) [ uphill-chemical ] end ;; sniff left and right, and go where the strongest smell is to uphill-chemical ;; turtle procedure let scent-ahead chemical-scent-at-angle 0 let scent-right chemical-scent-at-angle 45 let scent-left chemical-scent-at-angle -45 if (scent-right > scent-ahead) or (scent-left > scent-ahead) [ ifelse scent-right > scent-left [ rt 45 ] [ lt 45 ] ] end ;; sniff left and right, and go where the strongest smell is to uphill-nest-scent ;; turtle procedure let scent-ahead nest-scent-at-angle 0 let scent-right nest-scent-at-angle 45 let scent-left nest-scent-at-angle -45 if (scent-right > scent-ahead) or (scent-left > scent-ahead) [ ifelse scent-right > scent-left [ rt 45 ] [ lt 45 ] ] end to wiggle ;; turtle procedure rt random 40 lt random 40 if not can-move? 1 [ rt 180 ] end to-report nest-scent-at-angle [angle] let p patch-right-and-ahead angle 1 if p = nobody [ report 0 ] report [nest-scent] of p end to-report chemical-scent-at-angle [angle] let p patch-right-and-ahead angle 1 if p = nobody [ report 0 ] report [chemical] of p end ; Copyright 1997 Uri Wilensky. ; See Info tab for full copyright and license.
More information about the Ants example can be found here.