Skip to main content

Chapter 7 NetLogo

In the previous programming unit, we explored a number of programming concepts using Racket as our language. Now we will turn to NetLogo, where we will revisit some of the same topics, as well as explore new areas of programming.
NetLogo’s raison d’être 1  is to be able to create complex simulations and models that allow for line user-provided input. Here are some examples of the kinds of programs we can write in NetLogo:

Section 7.1 Getting to Know the NetLogo IDE

The starting state of NetLogo
When we start up NetLogo we see the Interface tab. This is where we can test out commands and see our programs run. It’s a good place to start. You might also notice the Info and Code tabs, which we’ll get into later.
The bulk of the interface tab is taken up by a large black square with some white space surrounding it. The black square is the NetLogo world. This is where all the action happens. Since visual representations are a key component of NetLogo programs, we will see lots of interesting things happen in the NetLogo world. The white space around the world is where we will be able to add interface elements like Buttons, Sliders, Monitors, and many more. Interface elements allow us to interact with a program without having to modify the code.
Below the world you will find the Command Center. This is very similar to the Interactions Panel in DrRacket. We can enter NetLogo commands (including procedures we write ourselves) in this area and NetLogo will immediately run those commands. The Command Center is a great place to learn about new commands or test your own quickly.
You might notice on the bottom-left of the command center, observer>. This is where you set the context for the commands that you enter. What do you mean, context?. I’m so glad you asked...

Section 7.2 Not-so-secret Agents

You may recall that when we introduced Racket (way back here) it was called a functional programming language because writing and calling functions was central to the experience of programming in Racket. NetLogo has many of the same features of Racket , we can perform arithmetic, write procedures (akin to functions), use boolean values and operators, make decisions, even work with lists. But the core experience of programming in NetLogo is writing code that will be run by agents. The context referred to at the end of the previous section determines which agents are being controlled.
A NetLogo agent is a "being" (for lack of a better phrase) that you can program to perform different actions. As a programmer, you control how these agents behave in the NetLogo "world". There are four kinds of agents:
  • Patches: Agents that make up the background of the world.
  • Turtles: Agents that can move.
  • Links: Agents that connect two turtles.
  • Observer: An agent that provides instructions to other agents.
As a NetLogo programmer, it is our job to understand what the different agents do, and how we can use them to achieve our goals. We will largely focus on Turtles and Patches, leaving Links for later investigations.

Subsection 7.2.1 Patches

Remember the NetLogo world, that big black square in the main area of the interface tab? What if I told you it wasn’t just a big square, but instead was itself made up of 1,089 smaller squares? Don’t believe me, here, take a look:
NetLogo world where each patch has a randomly chosen color.
Each one of those squares is called a patch. Patches are agents that make up the "background" of the NetLogo world. When you start NetLogo, the world is broken up into a 33x33 grid, giving us those 1,089 patches. The patches are arranged in a grid, and similar to the coordinate plane you may recall from one or more math classes, we can identify each patch by their pxcor (horizontal location) and pycor (vertical location). The origin is in the center of the world, so the coordinates range from -16 to 16 in both pxcor and pycor.
Patches cannot move, so each patch is can be uniquely identified by its coordinates. We call pxcor and pycor patch properties. A property is a special value that every agent has their own copy of. Often, agent properties have an impact on how the agent appears. Here is a list of the properties that patches have (this is called a property sheet):
The property sheet for a patch
You can access the property sheet for any agent by right clicking on the agent and selecting inspect, which will be the last option in the menu.
In addition to pxcor and pycor you can see three other properties. Each of these held describe the visual appearance of a patch. pcolor is, as you can probably guess, a number representing the color of the patch. Patches can be given a plabel which will display on top of the patch. The plabel and plabel-color properties determine what that looks like. Here is a section of the NetLogo world where every patch’s plabel shows its pxcor.
A 6x6 grid of patches with labels

Subsection 7.2.2 Turtles

As stated above, turtles are agents that can move. Unlike patches, there are no turtles in the world when we start a new NetLogo program, instead we must create them (there are a number of commands to do this). Since we already know about properties, let’s dive right into the property sheet of a turtle:
A turtle’s property sheet
You’ll probably notice a few properties similar to patches, like xcor, ycor, and color. In general, if turtles and patches have similar properties, the patch version will start with a p in order to avoid confusing the two.
Because turtles can move, there are a few key distinctions that show up in their properties. Turtles’ coordinates are not locked to the integers, since they can be anywhere in the world. Also, since turtles move, we cannot use their position as a unique identifier. Instead we have the who property, which is a unique positive integer assigned to each turtle when created. Lastly, you’ll notice a heading property, which tells us what direction the turtle is facing. heading is measured in degrees, with 0 pointing straight up and then increasing clockwise.

Subsection 7.2.3 Observer

Unlike patches and turtles, the observer does not have any visual appearance or properties. Instead, think of the observer as NetLogo’s representation of us, the programmers. The observer has full control over the NetLogo world, and can send instructions to all the other agents.

Section 7.3 Context Matters

Whenever a command is executed in NetLogo, it must have a context. The context determines which agent(s) are meant to run the command, so a context is always one of the agent types. Some commands are agent specific, while others can be run by multiple agents, but may have different results. When running commands in the interface tab, you can set the context using the text right before the prompt area either by selecting the desired context with your mouse or using the TAB key to cycle through the different contexts.
The context part o f the command center
Let’s look at a quick example of how context can change how a program runs. All agents can use the basic arithmetic operators (same as Racket: + - * /). Here’s a very simple calculation in observer context:
observer> 5 * 10
observer> show 5 * 10
observer: 50
And now in patch context:
patches> 5 * 10
patches> show 5 * 10
(patch -16 13): 50
(patch 12 12): 50
(patch 1 -6): 50
(patch -7 10): 50
(patch -9 -1): 50
(patch 6 -4): 50
...
In an actual NetLogo environment, instead of ... you would see many many more lines like the ones above. One of the things context determines is how many agents run the command. There is only one observer, so our input of 5 * 10 gets us the value 50, once. Patch context means every patch will run the command, in a randomly chosen order. As mentioned above, by default there are 1089 patches, so we will see 50 1089 times, preceded each time by the patch that ran the calculation.
It may seem strange that NetLogo behaves this way, but we’ll see that the ability to have potentially thousands of agents run the same command at once is one of the features that makes NetLogo such a powerful modeling and simulation language.