NetLogo: A Hidden Gem in Computational Simulation
I stumbled upon a quirky language for actor-based modeling for school and found something odd and beautiful.
One of the beautiful things about living at this time in human history is that there is no end to the strange and beautiful things you might find. NetLogo is one of those things I’ve discovered recently, and I thought I’d share a little bit about it.
First — I love programming languages. Each programming language has a unique story, history, lineage, and community. I recently stumbled upon a language I’d never heard of before called NetLogo. I needed to learn the basics of this language for a course I was taking this semester, but the more I researched the language, the more intrigued I became.
NetLogo is an important programming language for agent-based modeling simulations. The language is quirky. You learn the basic concepts when you go through the tutorials, but things get weird quickly. There are some important primitives, such as how to build maps, deal with time events known as “ticks,” and create dashboards to change variables and monitor your simulation (things like plotting graphs or tracking the value of a variable). It is all handy stuff. The language comes with its own development environment and tooling, with a UI that appears to be from 2003. It can also run simulations in a web-based tool.
In NetLogo, the world you build is made up of agents. And there are four fundamental agent types: turtles, patches, links, and the observer.
Turtles are a fundamental agent type?
Welcome to NetLogo.
There is a bit of history here. You see, NetLogo has a fascinating lineage when it comes to programming languages. It goes:
Lisp → Logo → NetLogo
For the uninitiated, Lisp is the Archaea Domain of programming language families. Remember learning about Archaea in school? They are those weird single-celled organisms that aren’t Bacteria or Eukaryota. That is what it means to be a Lisp.
Because NetLogo is a descendant of the programming language Logo, it inherited some inside jokes and references to the language. The Turtle was an educational robot used in the 1980s with the Logo programming language.
So, with that bit of history, an important type of actor in NetLogo is known as a turtle. You can assign a breed to your turtle; these actors can move around the map and interact with any of the other actors. For instance, a turtle can target another turtle; this utilizes a link actor, which can bind actors together. Turtles can also interact with patches, which are tiles on the map that can have their own properties.
The observer is a special agent that can interact with all other agents. In my short type using the system, I use the observer to manipulate the behaviors of the other actors in the main event loop, known as the go procedure. This is where we move time forward in a loop, tick by tick.
The big takeaway is that NetLogo’s Actor-based modeling gives a powerful way to describe the behavior of each of the actors in your simulation, and from that, you get emergent behavior from the system. You are encouraged to write simple rules for each of the actors. It allows you to build complex systems, and as the observer, you can see the emergent behavior that comes out of the system.
An early example of Emergence in computer science is Conway’s Game of Life.
In Conway’s Game of Life, each “cell” can either be black or white, and the cell follows a specific set of rules to decide what it will do to change its state based on its eight neighbors. Out of these simple rules, more prominent behaviors emerge on the grid.
In the NetLogo model known as Wolf Sheep Predation, NetLogo models the population fluctuations between predator and prey.
Interestingly, this model was built with only 150 lines of code. If we look at the primary event loop, we see how simple this program is.
if not any? turtles [ stop ]
if not any? wolves and count sheep > max-sheep [
user-message "The sheep have inherited the earth"
ask sheep [
ask wolves [
set energy energy - 1
If we follow the code from top to bottom.
The observer checks to see if all the turtles are dead. All turtles include both wolves and sheep. If there aren’t any left, then the model stops.
The observer checks to see if all the wolves are dead, and if the sheep count is greater than the maximum number of sheep, prints the message “The sheep have inherited the earth,” and the model stops. Otherwise, the model continues.
The observer asks all sheep to move and to run the reproduce-sheep procedure. This is a common pattern in NetLogo: to drop into another frame of reference; you ask an actor or list of actors to do something. The move and reproduce-sheep procedures are executed in the individual sheep context for each sheep in your model.
The observer asks all wolves to do a set of tasks. This includes moving, losing a bit of energy, attempting to eat-sheep (if successful, it gains energy), if energy is <= 0 die, and trying to reproduce.
Finally, we increment the tick by one. This means we have progressed another round in our model.
Repeat until the Stop condition has been met (this could continue forever)
Again, it’s a quirky little language that shines in a specific type of computing for Agent-Based Modeling (ABM). This language is used extensively in academia and simulation research. This semester, we read several academic papers on military simulations that used NetLogo to model battle conditions, and how they used the tool was novel and interesting. There are also additional tools, such as BehaviorSpace, which allows you to run “headless” (no UI) NetLogo models many times so that you can perform experiments in “parameter sweeping” and statistical analysis.
I love finding languages that solve a specific set of problems for their users. If I hadn’t taken this elective in grad school on a whim, I don’t know if I’d ever have encountered this language, and I’d never have had the opportunity to sort out how it worked. I hope you can follow along and now you know about this language too. And I hope that next time you have an opportunity to use a tool that seems alien to you, take some extra time to learn about that tool’s community and history. It will make your experience so much richer.