NetLogo: Agents, Sets, and Lists

We have already seen that the procedures tab of a NetLogo program has two sections:

attribute and global definitions
procedure and reporter definitions

We also saw that a typical NetLogo agent-based model has two important top level procedures:

to init-model
   ; ...
end

to update-model
   ; ...
end

The first one is invoked by pressing the INIT button, while the second one is iterated by pressing the UPDATE button.

We now look at some more complicated definitions from the Sugarscape model.

Turtle and Patch Attributes

patches-own [
   sugar
   grow-back
   rfp-queue
]

turtles-own [ 
   energy
   metabolism
   proposal-queue
   broadcast-range
   hasProposal
   target
]

Here's how we can display the value of the energy attribute of the turtle with id number 12:

print [energy] of turtle 12

Turtle and Patch Procedures (Methods)

Some procedures can be viewed as turtle methods while other procedures can be viewed as patch methods. For example, here's the turtle procedure consume-sugar:

1. to consume-sugar [a-patch]
2.    let old-energy energy
3.    set energy max (list ([sugar] of a-patch) 100)
4.    ask a-patch [
5.       set sugar sugar - (([energy] of myself) - old-energy)
6.    ]
7.    set hasProposal false
8.    move-toward one-of patches 2 * broadcast-range
9.    type self
10.   print " has consumed sugar"
11. end

Here is how we ask turtle 12 to execute the consume-sugar procedure with input the patch located at the coordinates: x = 3, y = 4:

ask turtle 12 [consume-sugar patch 3 4]

Here are some things to note about this procedure. Readers should consult the latest NetLogo Manual for details:

Line 1: a-patch is a parameter

Line 2: let declares a local variable called old-energy

Line 9: self = this = the turtle currently executing this procedure

Lines 9 & 10: type prints in the command center (shell) window without a newline, print prints with a newline. For example, this line might print:

   (turtle 12) has consumed sugar     

Printing to the command center is usually only done for diagnostic purposes.

Line 3: set assigns a new value to the energy attribute of self

Line 3: the new value is the maximum value in a list consisting of 100 and the amount of sugar owned by a-patch. In other words, the energy of a turtle should never exceed 100.

Lines 4, 5, & 6: We ask a-patch to decrement its sugar supply by the amount consumed by self. Here's the general format of ask:

ask agent-set [command command ... command]

We are asking each agent in agent-set to execute each command in the command block. Inside the command block the value of self becomes the agent executing the block. In our example self is a-patch inside the command block. We must use myself to refer to the turtle asking a-patch to decrement its sugar attribute.

Line 8: We ask self (now the turtle) to execute the move-toward procedure:

This procedure requires two inputs: which patch to move toward and how far to move.

We use the one-of procedure to select a random patch from patches, the set of all patches.
Let's look at the move-toward procedure:

1.  to move-toward [goal steps]
2.     face goal
3.     fd steps
4.     set energy (energy - energy * metabolism * steps)
5.     set color scale-color red energy 140 0
6.     if (energy <= 0)
7.     [
8.        type self
9.        print " is dying"
10.       die
11.    ]
12.  end

Line 2: NetLogo provides a variety of command for setting a turtle's heading.

Line 3: Self moves forward the desired number of steps.

Line 4: Self's energy is reduced by an amount determined by its metabolism and the number of steps moved.

Line 5: Self's color fades in proportion to its energy.

Line 6: NetLogo provides two types of conditional commands:

if condition [commands]

ifelse condition [commands] [commands]

We can also create a simple multi-way conditional using the stop command:

if condition1 [command1 stop]
if condition2 [command2 stop]
if condition3 [command3 stop]
etc.

The stop command is similar to the void return command in C.

Line 10: This is how we kill turtles. Self will disappear from the world after executing this line.

Agent Sets

NetLogo makes it easy to gather together all agents with some property. The command is:

agents with [property]

For example:

turtles with [energy > 50]
patches with [sugar = 0 and not grow-back-rate = 0]

For example, here's how a turtle broadcasts a request for a proposal:

to make-rfp
   let my-neighbors patches with [distance myself <= [broadcast-range] of myself]
   ask my-neighbors [set rfp-queue lput myself rfp-queue]
end

NetLogo has a built-in command that simplifies this:

to make-rfp
   let my-neighbors patches in-radius broadcast-range
   ask my-neighbors [set rfp-queue lput myself rfp-queue]
end

Lists

NetLogo has a rich collection of procedures for manipulating lists. Here's a quick example:

set exam1-scores [20 30 40]
set exam2-scores [40 50 60]
set scores list exam1-scores exam2-scores ; = [[20 30 40] [40 50 60]]
set means map [mean ?] scores ; = [30 50]

Unfortunately, procedures for manipulating lists can't be used on sets of agents. Agent sets must be converted into lists using the of command. For example:

set energy-list [energy] of turtles
set patch-list [self] of patches with [sugar > 0]

We can implement make-rfp as follows:

to make-rfp
   let my-neighbors patches in-radius broadcast-range
   foreach [self] of neighbors [
      ask ? [set rfp-queue lput myself rfp-queue]
   ]
end

Note the for-each command, which iterates over a list. The current item in the list is referenced by the implicit ? symbol.