Making Your First Diffusion of Information Agent-Based Model with NetLogo

Chathika Gunaratne
6 min readApr 9, 2022

Agent-based models are a great tool for studying the dynamics of complex adaptive systems. Briefly, complex adaptive systems are those where collections of autonomous individuals interact with one another to generate macroscale outcomes that are difficult to predict through analysis of the individuals in isolation. Hence the notion, the whole is more than the sum of the parts. Similarly, agent-based models work by letting you program agent rules that dictate the actions and reactions an agent might take during an encounter with other agents. These interactions in turn produce interesting patterns of simulated collective behavior that may provide insights into social, economic, or ecological phenomena observed in the real world.

NetLogo [1] is probably the easiest agent-based modeling toolkit to get started with. It features an intuitive GUI with easy-to-use drag and drop buttons, charts, output monitors, and environment window. You do need to do some basic coding to create and manipulate your agents, but we’ll discuss how to in this blog post. You’ll have to download and install NetLogo from https://ccl.northwestern.edu/netlogo/ to follow along.

We’ll create a model of information diffusion, where the goal is to simulate the spread of awareness throughout a population of agents. If any uninformed agent comes in proximity to an informed agent then the first agent will also become informed. In this example, let’s assume an initial state where only one agent is informed in the beginning.

Initialization

To get your first agent-based model up and running, let’s create some agents. Switch over to the code tab. NetLogo refers to agents as “turtles” and here we need to use the turtles-own construct to declare any variables that are local to agents.

Next, let’s create a NetLogo procedure to initialize our simulation, which we’ll call setup. NetLogo functions may either be procedures that do not return any value, indicated by toor reporters that return a value, indicated by to-report , both ending with the endcommand. setupdoes not need to return a value so we can use toand end. The first thing to do in setup is to clear the environment from any previous simulation runs using the clear-all command. Next, we’ll create 100 agents using create-turtuleswith the boolean variable informedset to false using a custom procedure update-state(which we’ll get to in a bit). We’ll also position these agents at random coordinates within the environment. To make sure at least one of our agents starts informed, we’ll randomly instruct one of our agents to update-state to true using the ask and one-ofcommands. Finally, we’ll get our simulation ready by setting the simulation tick counter to 0 using reset-ticks .

Note how both the random placement of the agents and update-statecall were done in the create-turtles procedure context. You can use this to set any initial states the agents should have upon creation.

The update-state procedure, is “turtle-level”, and will take a boolean argument state and set the informed variable and color of the agent accordingly.

The Go Procedure

Next, we have to tell the agents what to do as the simulation progresses. We typically do this in a procedure labeled go (the name being mere convention). Let’s make our go procedure tell agents to call a move procedure, followed by a call to a determine-new-statereporter, which will determine and return the new informed state based on the agent’s neighbors. Finally, go must increment the simulation step counter by calling the tick command.

Lets program the agents to perform random walks, where they move forward a single patch with forwardand then randomly shift their heading every 10 ticks. The number of simulation ticks passed can be queried using ticks .

Finally, let’s create the determine-new-state reporter such that it returns true if any informed agents appear within a variable patch radius of itself, else it returns false. Let’s call this radius vision-radius .

GUI Components…

Ok! Now your agents know what they are supposed to do. Let’s switch over to the interface tab, where we’ll set up the GUI components needed to run and monitor the simulation. To add GUI components hit the ‘add’ button on the toolbar, or right click the interface and select the appropriate component. Create two buttons. You will be prompted with a menu where you can specify the commands executed by the buttons. Type in setup for one button and go for the other. For the go button, also tick the “Forever” checkbox, which will make the simulation run indefinitely.

Also, add a ‘slider’ and name it vision-radius . NetLogo will map the name of this slider to the global variable vision-radius you used in the determine-new-state reporter, giving the agents an adjustable radius of perception. Here we set the default vision-raduis to 1.

Finally, add a ‘plot’ component. You will be prompted for the plot name, axis labels, optional set up commands, and plot pen information. Name the plot Fraction Informed Over Time. Label the x axis Time and y axis Fraction Informed . X axis limits will be automatically managed, so you can leave the defaults for this. But set the y axis limits to 0 and 1. Finally, rename the default pen to Fraction Informed , and in the ‘pen update commands’ enter the command to measure the fraction of informed individuals. We will use the plot command which takes a reporter as an argument and plots the value over time. Enter plot (count turtles with [informed = true]) / (count turtles) to plot the fraction of informed turtles over time.

You can select and resize the GUI components for an aesthetically pleasing layout. The final interface should look something like this.

Simulate!

Congratulations! You have built your first agent-based model of information diffusion! To initialize, just hit setup and press go to run. (Hint: use the slider at the middle of the toolbar to slow your simulation down so you can better observe the dynamics)

Next Steps…

There are endless possiblities with NetLogo and a good next step is to explore the various commands and helper functions provided in the NetLogo dictionary https://ccl.northwestern.edu/netlogo/docs/dictionary.html . The NetLogo model library (File -> Models Library) also has a great collection of models to experiment with, many which have research papers associated with them under the Info tab. Finally, [2] is a great reference too learn more about working with NetLogo

References

[1] Wilensky, U. 1999. NetLogo. http://ccl.northwestern.edu/netlogo/. Center for Connected Learning and Computer-Based Modeling, Northwestern University. Evanston, IL.

[2] Wilensky, U., & Rand, W. (2015). An introduction to agent-based modeling: modeling natural, social, and engineered complex systems with NetLogo. Mit Press.

--

--

Chathika Gunaratne

PhD Modeling and Simulation | Postdoctoral Research Associate at Oak Ridge National Laboratory