The Structure of a NetLogo Program

NetLogo is an IDE for developing agent-based models. The two principle types of agents in NetLogo are mobile "turtle" agents and stationary "patch" agents. In the screen shot below the patches are the shaded green tiles and the turtles are the shaded red chevrons.

Program Structure

A NetLogo program has three tabs:

Interface
Information
Procedures

Here's a screenshot of the interface tab:

The Procedures tab is usually divided into two sections:

Declarations of Attributes and Globals
Declarations of Procedures and Reporters

Turtle, Patch, and Environment Attributes

Attributes are the attributes of patches and turtles. Globals can be viewed as attributes of the environment. For example:

patches-own [
   altitude
   temperature
]

turtles-own [
   energy
   metabolism
]

globals [
   cycle
]

Turtles and Patches are Objects

In fact, NetLogo is a Java program! Here is the API.

Turtle and patch attributes are analogous to fields in Java and C++. For example, here's how the attribute declarations above might be translated into Java and C++:

class MyPatch extends Patch {
   public double altitude;
   public double temperature;
   // ...
}

class MyTurtle: public Turtle {
public:
   double energy;
   double metabolism;
   // ...
};

Where Patch and Turtle are library classes that contain fields that all custom patches and turtles must inherit:

class Patch implements Agent {
   public int pxcor, pycor; // location in patches[][]
   public int pcolor; // color of this patch
   // ...
}

class Turtle: public Agent {
public:
   int xcor, ycor; // current location of this turtle
   int who;        // index of this turtle in turtles[]
   int color;      // color of this turtle
   bool penUp;     // this turtle will draw if false
   // ...
};

Notice that the fields are declared public. NetLogo doesn't have private and protected scopes.

Notice that the color of a patch is not color, but pcolor. NetLogo doesn't allow any form of name sharing.

We may assume Agent is an empty abstract class or interface:

interface Agent { }

The World

The World, is the environment where the patches and turtles live.

NetLogo globals are like C++ globals:

int cycle; // = number of times the model is updated

In Jave we might imagine that globals are static fields of some environment class:

class MyWorld extends World {
   public static int cycle;
   // ...
}

A UML Model

Here's a UML model of the above:

Procedures and Reporters

NetLogo procedures are analogous to Java and C++ methods that return void. NetLogo reporters are analogous to methods that have non-void return types.

For example, here are two definitions of turtle methods:

to init-turtle
   set energy 100
   set metabolism 0.5
end

to-report energy-consumed [num-steps]
   report energy * metabolism * num-steps
end

Here's how these might be translated into C++:

class MyTurtle: public Turtle {
public:
   double energy;
   double metabolism;
   void init_turtle();
   double energy_consumed(double num_steps);
   // ...
};

Where:

void MyTurtle::init_turtle() {
   energy = 100;
   metabolism = 0.5;
}

double MyTurtle::energy_consumed(double num_steps) {
   return energy * metabolism * num_steps;
}

Of course MyTurtle and MyPatch inherit many methods from Turtle and Patch, respectively.

Invoking Methods

The World contains patches and turtles:

MyTurtle[] turtles;
MyPatches[][] patchs;

In Java/C++ we might ask turtle 12 to initiate himself then take ten steps as follows:

turtles[12].init_turtle();
turtles[12].fd(10);
turtles[12].energy =
   turtles[12].energy – turtles[12].energy_consumed(10);

Here's the equivalent NetLogo syntax:

ask turtle 12 [
   init-turtle
   fd 10
   set energy energy – energy-consumed 10
]