Agent-Based Modeling in NetLogo
Last Updated on May 13, 2020 by Editorial Team
Author(s): Prabod Dunuwila
Agent-based modeling is a simulation technique that focuses on building a model of a system with a collection of autonomous decision-making entities called agents. Each individual agent can make decisions based on a set of rules provided with regard to their situation or properties. Using ABM techniques, people have identified that more complex patterns emerge as a result of simple interactions between individuals. Some of the examples are traffic jams, a flock of birds flying in V shape, housing patterns, and soΒ on.
You have probably seen a flock of birds flying in βV formed shape.β Most of you have probably thought that the leader bird is in the front, and other birds follow it. This is not the case. What happens is, rather than the same bird staying at their position, different birds occupy different spots while flying based on the independent movement based on the flying direction of the flock, avoiding other birds and not getting far away from the neighborΒ birds.
Likewise, ABM computational methodology enables us to model complex systems. ABM models are composed of agents, and agents have properties. In NetLogo, each agent has a graphical representation on the computer screen. Letβs try to build a simple scenario of a virus spread in society using NetLogo. In society, there are people(agents) who could be identified as infected and non-infected. So when agents interact with each other, infected agents may spread the virus to other agents. So letβs build a simple model to see how a virus is spread through a society based on a simple set ofΒ rules.
Properties of agentsΒ :
- βstatusβ to store whether infected or non-infected
RulesΒ :
- each individual moveΒ randomly
- a virus is infected if the non-infected individual is within the radius of the infected individual based on a spreadΒ rate
First, letβs build the interface of the model by using the βinterfaceβ in NetLogo. Here I have used two buttons named βsetupβ procedure to set up the environment and βgoβ procedure to run the modelΒ forever.
Then letβs create inputs named βinfected-countβ and βpeople-count.β
A slider is used to change the global variable valueβ spread-rate.β
Move to the βcodeβ tab in NetLogo. Since we are using a global variable βcounterβ and creating a new breed type, we have to define it at the top of the code. Here the breed type is βpeople,β and we can use it to call all βpersonβ agent set. And also, we have to define its properties.
globals[counter]
breed[people person]
people-own[infected?]
Letβs look at what we have to do in the βsetupβ procedure. We have to clear the environment first, then set up the environment with agents and reset the ticks. So the code will look likeΒ this.
to setup
clear-all
initialize-people
reset-ticks
end
You will get an error if you βcheckβ the code using the βCheckβ button because you have not yet written βinitialize peopleβ procedure. We have to write that separately below setup. Write the βinitialize-peopleβ procedure to create the agents of people type based on the people-count provided. Agents will be of shape βpersonβ and random x, y coordinates. And people will have βinfected?β property true or false based on the initial βinfected-count.β
to initialize-people
set counter infected-count
create-people people-count[
set shape "person"
setxy random-xcor random-ycor
(ifelse (counter > 0)
[set color red
set infected? true]
[set color white
set infected? false])
set counter counter - 1
]
end
Then in the βgoβ procedure, we have to write what we expect to do when the model is running. So I will simply make people move randomly and spread the disease. I will not focus on the recovery of the infected people agents, and the model will stop when all the people get infected with theΒ disease.
to go
(ifelse any? people with [color = white] [
move-people
spread-disease
]
[stop])
tick
end
We have to write two separate procedures named βmove-peopleβ and βspread-diseaseβ to run the model. First, letβs look at the move-people method. It will ask people to pick a random whole number between 0 and 50. Then the agent turns right this number of degrees. And then ask to choose a random number between 0 and 50 and turns left that number of degrees. Then move forward with the specified stepΒ value.
to move-people
ask people [
rt random 50
lt random 50
forward 0.1
]
end
In the βspread-diseaseβ procedure, when a non-infected person meets an infected person, he/she will get infected with the disease based on the βspread-rateβ provided.
to spread-disease
ask people [
if any? people with [infected? = true] in-radius 0.5 [
if random 100 < spread-rate * 100 [
set color red
set infected? true
]
]
]
end
Move back to the βinterfaceβ tab, and letβs create a plot to show the count of infected and non-infected people at each tick when the model isΒ running.
So the final interface will look like below after making all theΒ changes.
Before you run the model, donβt forget to fill input values for the inputΒ boxes.
I hope all of you have got an understanding of Agent-Based Modelling using NetLogo. If you want to read and learn more about ABM simulation using NetLogo, check out the references providedΒ below.
References
[1] NetLogo HomeΒ Page
[2] An Introduction to Agent-Based Modeling Modeling Natural, Social, and Engineered Complex Systems with NetLogo by Uri Wilensky, WilliamΒ Rand
Agent-Based Modeling in NetLogo was originally published in Towards AIβββMultidisciplinary Science Journal on Medium, where people are continuing the conversation by highlighting and responding to this story.
Published via Towards AI