Post navigation

Integrating NetLogo and Java: #1

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: