The web content describes an agent-based model (ABM) using Python to simulate ant foraging behavior, illustrating how complex patterns emerge from simple individual rules without central control.
Abstract
The article "Building An Agent-Based Model with Python for Ant Foraging" explores the intricate foraging behavior of ants through the lens of agent-based modeling (ABM). It draws parallels between the collective intelligence of ant colonies and human economic systems, emphasizing that complex phenomena can be understood through simple rules followed by individual agents. The Python programming language is used to create a simulation model where each ant follows basic rules for pheromone laying and following, which results in the emergence of sophisticated trail patterns. The article also discusses the advantages of ABMs, such as their ability to capture dynamic interactions and bridge the gap between micro-level behaviors and macro-level outcomes. It provides a step-by-step guide on how to conceptualize and code an ABM in Python, referencing Allen B. Downey's textbook "Think Complexity" and offering insights into object-oriented programming. Additionally, the article introduces various tools for ABMs, including Netlogo, Mesa, RePast Simphony, and MASON, and concludes by inviting readers to explore the AI service ZAI.chat for cost-effective access to advanced AI capabilities.
Opinions
The author believes that ABMs, which focus on simplicity, are a powerful tool for understanding complex systems in nature and human society.
It is suggested that ABMs can provide insights into economic systems by modeling individual decision-making processes and their collective outcomes.
The article posits that the emergence of complex patterns, such as ant trail formations, can be explained by the collective behavior of agents following simple rules, without the need for centralized control.
The author expresses that Python is a suitable language for implementing ABMs, particularly due to its ease of use and the availability of libraries and frameworks.
The article conveys enthusiasm for the potential of ABMs to contribute to various fields, including computational economics, behavioral sciences, complex networks, and artificial intelligence.
By referencing the work of Theraulaz et. al. (2003) and Deneubourg et. al. (1989), the author acknowledges the scientific basis and historical contributions to the understanding of ant behavior through ABMs.
The author endorses ZAI.chat as a cost-effective AI service alternative to ChatGPT Plus (GPT-4), suggesting its value for readers interested in AI and computational modeling.
Building An Agent-Based Model with Python for Ant Foraging
When we survey the behaviors of ants, we shall be amazed by their collective wisdom and their massive construction. African ants can build a nest that is 6 or 7 meters tall. Their nests not only can be 100 times to an individual ant, the patterns of the nests are sophisticated too. Do they all follow a grand blueprint for construction? Now let’s observe how they search for food. They start searching everywhere. Once they found food, they form a long trail that can be as long as a mile to carry food back to the nest. Do they listen to a central commander to line up? It seems not. It is more believable that each ant just follows some simple actions, which result in a very complex and orderly system. “Without centralized control, ants are able to work together and collectively tackle tasks far beyond the abilities of any one individual” (Theraulaz et. al. 2003). Indeed there is much for human to learn: “Consider an ant’s ways, and be wise: Which having no guide, overseer, or ruler, provides her meat in the summer, and gathers her food in the harvest“. ~ Proverbs 6:6”.
In nature or our lives there are many similar examples. Think about our sophisticated economic system. Does each worker possess unfathomable knowledge to plan for the economy? No. Everyone just makes his simple and self-interest decisions. How do we create a model to understand the phenomena and predict the outcomes?
(1) Agent-Based Modeling (ABM) — Simplicity Is the Best
We articulate a “model”, like a map, to understand complex phenomena. A model should capture the essential features for reasoning. Complex phenomena may require a complex model. However, we human are not all-knowing, how can we design an all-knowing model to explain the complex interactions in a society? Maybe complexity is not the solution but simplicity is. In an ant colony each ant is like an agent following a set of simple rules; in our society each individual is an agent who just makes his self-interest, simple choices. Why don’t we design an agent-based simulation model that allows agents to interact so we can observe the evolution? That’s the name Agent-Based Model (ABM) for such an endeavor. In the past two decades the Agent-Based Modeling has burgeoned to be a new modeling paradigm for rationalization and computational simulation. It has created many landmark contributions with a large body of literatures in computational economics, behavioral sciences, complex networks, and artificial intelligence.
In an ABM each agent follows a set of simple designed rules. The simulation allows agents to evolve in many iterations for us to observe the interactions. This modeling process is very intuitive. You can design an ABM to tell a story and collect the generated data for analysis. You can experiment a wide range of conditions to observe the consequences. If we say data analytics investigate existing data to find insights, an ABM generates and experiment data to render insights.
There are many unique advantages of ABMs:
All agents’ behavior can be specified by explicit mathematical rules, which allows the researcher to simulate the dynamics of the model herself.
ABMs allow for a large number of possibly heterogeneous agents that interact over an extended period. ABMs can capture the evolution path as well as the solution. It affords researchers to analyze the dynamic history.
Many processes cannot be formulated with tractable mathematical formula. Simulations allow for numerical solutions that are otherwise intractable. ABMs are especially powerful for spatial or network simulations.
ABMs bridges the micro-macro gap because it is a bottom-up process from individuals to the entire system.
The complex ant hills inspire some artists to make ant hill art by casting molten aluminum into an ant hill
(2) Why Do I Write This Article?
This post will walk you through how to think in the ABM way and code accordingly. After reading this article, you will be able to run an agent-based model and even develop your modification for your story. Because many readers use Python, this article is in Python. I will also reference popular ABM tools at the end of the article.
(3) Simple Ants Foraging Rules
Let’s assume each just follows a set of simple trail-laying and trail-following rules. No ant has any visibility to the grand trail patterns. What will emerge as a result of this simple setting? This is the Monte Carlo simulation in a well-cited paper “The blind leading the blind: Modeling chemically mediated army ant raid patterns” by Deneubourg et. al. (1989) in the Journal of Insect Behavior. Their simulation confirmed the hypothesis successfully: simple agent actions indeed produce orderly and sophisticated trail patterns. They delineate the following simple rules:
When an ant finds a piece of food, it carries the food back to the nest, dropping a chemical (pheromone) as it moves.
When other ants “sniff” the chemical, they follow the chemical toward the food.
As more ants carry food to the nest, they reinforce the chemical trail.
None of the above rules inform each ant about the formation of trail patterns, but orderly trail patterns come to exist. Let’s see how to code these rules with Python.
(3.1) Fun Facts About Ants
The colonies of the black garden ants can reach in size up to around 40,000 workers in rare cases, but 4,000–7,000 is around average.
The queen of the black garden ants can live up to 15 years, and it has been claimed that some have lived for 29 years.
The queens in the early stages of founding can have two to three other queens in the nest. They will tolerate each other until the first workers come, then it is most likely they will fight until one queen remains.
(4) How to Think to Code an ABM in Python?
The simulation model will have many agents that interact in the model. So the basic framework of an ABM has two components: the agent and the model. Imagine you set up a square floor area for many simple robot ants to move inside the area. You draw grids on the floor area so you know which robot ant goes to which grid, and which grid has food remaining. Now let’s describe the functions of each one:
The agent (robot ant) knows two things: (1) if it carries food or not, and (2) which grid (the x and y coordinates) it goes to. Other than that, an ant does not know anything about other ant’s location or food information.
The model (room) knows how many robot ants are in the room (initializing robots), and keeps records of all the robot ants at what time and their locations. The model advances the simulation with time steps. All robot ants move one step in each time step.
The above components can be coded effectively as an agent class and a model class in Python. If you are not familiar with classes or object-oriented programming, just remember a class can make many similar objects that can do things. Here the agent class will make many robot ant objects, and the model class just make one model object. You will become proficient with Python classes by reading “Learning Object-Orient Programming with Python in 10 Minutes” and “Learning Inheritance in Object-Oriented Programming with Python”.
(5) Python Code
This classic Ant Foraging simulation has received programming codes of some variations from many researchers. This article uses the Python classes from the exercise in Chapter 12 of Allen B. Downey’s textbook “Think Complexity” available on Amazon, and this website or this website. I have simplified the code slightly for tutorial purposes.
(5.1) The Agent Class
An ant only remembers two things: its (x, y) location, and if it has food or not. An ant follows these simple decisions: (a) moving left-forward or right-forward to search for food with a given probability, (b) checking how much pheromone is in the left or right cell, and (c) dropping pheromone at the current location. The functions are:
next_left() and next_left(): the (x, y) position that the ant would move to if it moved forward left or right.
left_pheromone and right_pheromone: the amount of pheromone in the (x, y) position that the ant would move into if it moved left-forward or right-forward.
will_move: whether or not the ant will move is determined by a tangent function. (Some of you may realize this is a popular treatment called the activation function in deep learning.)
lay_pheromone: The ant lays pheromone in its current Location.
(5.2) The Model Class
A model is an environment that the ants live in. So we shall define many things in the environment. The model will (a) add ants at the nest, (b) move all ants in each time step, and (c) evaporate some pheromone from each Location. There are several small functions to pass information.
add_ants(): add ants at the nest
move_ants(): move all ants in each time step
evaporate_pheromone(): evaporate some pheromones
has_food(), get_location(), and num_ants(): these small functions just to obtain information.
It is easier to write location-related information in a separate class called “location”. The location records (a) if there is food, and (b) the amount of pheromone. In the ant class, an ant can remove food or drop a pheromone at each location. In the location class, the location has to remove food if an ant removes food. The location had to add pheromone if an ant drop a pheromone at a location.
(5.3) The Main Program
I made a for-loop to show the dynamic movements of ants.
The entire Python code is available for download via this Github link. Open a command prompt and type in python AntPlot.py. A new window box pops up. You shall see it like this. The code sets “timesteps” to 600 and time.skeep to 0.005, so it takes a few seconds to show the plot. You can change these parameters to make the plotting time shorter.
In our model, there is no central planner that dictators which ant to move where. No ant knows which ant comes from where and going to where. Yet these ants form an orderly food trail.
(6) More Simulation Tools for ABMs
Several open-source tools are widely used for agent-based modeling. These platforms vary in how they support graphing, speed, programming, and data analysis.
(6.1) Netlogo
Since its beginning in 1999 by Wilensky, Netlogo has become a very popular tool in simulation. Its tutorial website is a must-visit. The Ant foraging model is available here. It is based on the Logo language and is meant to be an easy programming tool. It is commonly used by economists, anthropologists, physicists, and so on. This active community Computational Model-based Science (CoMSES) has a good collection of models built on Netlogo.
(6.2) Mesa
Mesa is an agent-based modeling framework in Python. With the popularity of Python, Mesa gets great traction with its community contributing actively. It has built-in core components like Models, Agent, Schedulers, and Spatial grids. It offers data collection functionalities for analyzing the results in a Python notebook. You want to check this interesting code example for ants looking out for sugar.
(6.3) RePast Simphony
Repast Simphony offers users several salient features. Your model can be developed and integrated into different forms including Logo, point-and-click Charts, or Java. Built-in Java, includes graphing tools and automated connections to external tools such as R Studio. Repast for High-Performance Computing (2018) is designed in C++ to offer high-speed computation on large computing clusters and supercomputers. Repast Py is also very appealing to Python users too.
(6.4) MASON
MASON is built in Java for large computational simulation. It contains both a model library and an optional suite of visualization tools in 2D and 3D. I am intrigued by its support for Quicktime movies, charts and graphs, and output data streams. So definitely check it out.
Conclusion
Thank you for reading. I hope this has given you a better understanding of the topic. If so, be sure to let me know in the comments.