Game of Trust Using ABM


With agent-based modeling (ABM), you can show (among other things) how a complex system behaves—in a simple way. A simple problem with deep consequences that can be modeled is trust. Let’s explore what could happen to a country’s level of trust if we play with a couple of variables using NetLogo. If you want a simple intro on NetLogo, please check [I] (, and the post Agent Based Modeling In NetLogo. You can play online using this link, and the code is available here.

To play, click the Setup button (or press the “S” key), and then click the Go button. In this simple model, 100 people select a partner to negotiate with. In the negotiation, each agent chooses to trust or defect from the other based on two previous experiences (the tit-for-two-tats strategy). If my previous two partners let me down in the first instance, I will defect from my partner, but if my partner trusts me the next time, I will trust my partner. With every round (clock tick) after the negotiation, a new random network of connections between agents is created, and the plot of people willing to trust is updated.

But, as in the real world, there are rotten tomatoes which always defect from other people, and you can configure the probability that a person will behave this way using the first slider. The total number of people that are bad seeds is shown in the monitor, and updated every time that you set up the game.

Additionally, as in real life, there is a probability that something happened—At the end of the negotiation, one or both agents misunderstand the result of the negotiation, or you think wrongly that your partner defected, or vice versa. This affects the way you’ll behave in the future. People in blue start trusting the first partner; people in cyan will not trust initially, but are ready to change their minds based on the interaction.

The code

Logo is a language based on Lisp, so you would miss some of the concepts of the traditional imperative languages like Java or C. But with NetLogo specifically, you get a simple UI builder and some network analysis tools that allow you to create, view and analyze several types of problems.

NetLogo admits global variables to be declared with the global keyword. In addition, the basic concept of agent -the turtle- can be extended with attributes (in our model, boolean flags for one person’s trust and a partner’s trust, the string value of the type of strategy, and a reference to the current partner:

The simulation can run forever, but as an end condition, we count the number of agents that trust. If there are none, we stop the simulation:

The setup of each round is basically the cycle to release the current partner of each agent and the search for a new partner, which is done by the find-partner method:

I find this type of syntax beautiful. It’s like programming in natural language. The method one-of takes a collection as an argument and selects one element randomly; the other method returns a collection which is the same as the input, but excludes the current agent.

The negotiation method takes into account the last two results and introduces a stochastic behaviour (the misunderstanding) before running the strategy of the current agent:

The tit-for-two-tats strategy assigns the current round of trust of the agent based on the previous results, and changes the color of the agent based on the end result:

Finally, to update the monitors, we create to- functions which report the count of agents with specific attributes:

Those methods will be called from the UI each round. The time monitor plots the NetLogo ticks variable which contains the current number of rounds.

Play the game several times with the same setup—It will not always show the same results. Change the country and slider values to explore different base situations. You can also improve the model with new strategies or variables that influence the outcome.


You can model this kind of problem with several tools. (Check this response on Stackoverflow.) But the simplicity of NetLogo lets you create a starter version in less than an hour, and you can publish the model as a webpage using, which is a great way to show your model to the world.

Do you think you can beat this Sweet post?

If so, you may have what it takes to become a Sweetcode contributor... Learn More.

Nicolas Bohorquez is a Developer and Entrepreneur from Bogotá, Colombia, he has been involved with technology in several languages, teams, and projects in a variety of roles in Latin America and United States. Currently he is doing the Master in Data Sciente for Complex Economic Systems in Torino, Italy


Click on a tab to select how you'd like to leave your comment

Leave a Comment

Your email address will not be published. Required fields are marked *