NetLogo is one of my favourite simulation tools, mainly because it allows quite sophisticated simulations to be built very quickly. This has made it one of the most popular agent-based modelling tools for social applications.
Uses of NetLogo in environmental science and ecology tend to have agents or “turtles” (representing people or wildlife) interacting with “patches” of territory, which often involve some physical process (such as hydrology, growth of crops, etc.). The NetLogo version of “Artificial Anasazi” is a good example.
Modelling the physical process on the “patches” may stretch NetLogo’s computational limits, however, and a good solution is to use the NetLogo “extensions” feature to write the “patch” dynamics in Java.
This blog post (part 1 of 3) describes a simple example, in which “turtles” interact with a grid undergoing a simple diffusion process. To use the example below (which is also illustrated above), place the diffusion.jar (Java) file in a folder called “diffusion” in the extensions folder inside the NetLogo installation directory.
For this example, the use of Java reduces the time taken (in “fast” mode) from 41.8 milliseconds per iteration step to 5.8 milliseconds per iteration step – that is, our example runs about seven times faster than a pure NetLogo solution. For more complicated patch dynamics, the speedup is likely to be even greater.
The accompanying Java for this example (to be discussed later) defines a
DiffusionGrid datatype, an instance of which is held in the global
grid variable. The Java also defines two reporters, shown in red below, to initialise a
DiffusionGrid object and to query entries within the grid. Two commands, also shown in red below, set entries within the
DiffusionGrid object and implement the diffusion dynamics. This makes the NetLogo code very simple:
extensions [diffusion] globals [ grid ;; Java object with a type defined by the diffusion extension ] patches-own [ value ;; NetLogo copy of the grid data ] ;; code for SETUP button to setup clear-all ;; use extension reporter to set up a "clone" of the patch grid set grid (diffusion:init-grid world-width world-height min-pxcor min-pycor) ;; set up initial values in both patches and grid ask patches [ set value (random-float 9.9) diffusion:set-grid-cell grid pxcor pycor value set-patch-color ] ;; some turtles crt 10 [ setxy random-xcor random-ycor set shape "person" set color sky set size 4 ] reset-ticks end ;; code for GO button to go ;; perform several computation steps within the grid repeat 10 [ diffusion:diffusion-step grid 0.01 ] ;; copy the modified grid data to the patches ask patches [ set value (diffusion:get-grid-cell grid pxcor pycor) set-patch-color ] ;; have the turtles interact with the modified patches ask turtles [ uphill value ] tick end ;; utility method (called twice) to set a patch color from a value to set-patch-color set pcolor (30 + value) end
As a side issue, integration between NetLogo and R is also possible – see here.