-
Notifications
You must be signed in to change notification settings - Fork 17
Ant AI Stack
This is the formal document describing, as close to the real code structure as one can get in natural language, how ant behavior should work in Formicidae.
Ants are fundamentally simple animals, but their behaviors are anything but simple. To simulate ants properly in a computer program, it must be understood that a relatively simple AI stack must result in sophisticated emergent behaviors. Not all of the behaviors we want to see from our ants will be specifically coded into the AI stack; much should ideally only become evident in the interactions of many, many ants running this stack simultaneously, interacting with each other. Crucially, ants, though often thought of as a superorganism, are fundamentally disconnected from each other! They do not "know" what other ants are thinking at any point in time during the entirety of their little lives; as near as science can tell, there is nothing that it is "like" to be an ant; they have zero internal experience. They are, for all intents and purposes, little machines.
Like all animals, adult ants respond to stimuli in their environment. The purpose of their central nervous systems is to process the stimuli they receive from their various sensors and translate that reception into appropriate actions. To that end, it is productive to enumerate the types of sensors available to real ants, and their uses, even if we are only going to approximate these sensors within the game.
-
Olfactory cells: Ants have a good sense of smell, allowing them to sense the presence of nearby food and moisture. They are especially attuned to pheromones - chemicals secreted by ants in their own colonies, which have profound effects on the behaviors of ants which smell them.
-
Antennae: Almost all ant species make use of a pair of limbs growing from their heads. These fine-tuned limbs are used to feel the environment around the ant, especially in front of them, and are used to stridulate (rub and tap) the antennae of other ants, to communicate between the two ants. Commonly this is used to determine friend from foe, request trophallaxis (ants can regurgitate the contents of their crops for other ants to consume), or find a body part to latch onto (if in combat).
-
Eyes: Most (but not all) species of ants have eyes - two sets, actually. The first set is the compound eye, which is usually attuned to the visible spectrum of light. Like most other insect compound eyes, it is not capable of strong focus, but rather provides a 360 degree view of objects relatively near the ant. Ants generally do not use these eyes very much; if scent or touch indicates otherwise, they will readily ignore what they see. The primary purpose of their compound eyes is to warn them of imminent danger, although there is some research that indicates they may be used for astrogation and navigation using the position of the sun! They also posses a smaller set of eyes that detect light and dark in a variety of spectra. These are used to help the ant understand whether it is above or underground, whether it is day or night, and to help regulate their circadian rhythm.
-
Hairs: Ants possess a thin covering of hairs on their carapaces, which are used to communicate a sense of touch on their bodies. These usually serve the purpose of helping an ant navigate in tight areas and evading predators and parasites.
-
Orientation: Ants do seem to have a sense of gravitational pull; they know if they are on a ceiling vs. a floor, for example. Ants will cling tightly and move more carefully on surfaces where gravity and/or wind, etc. would pull them off, if they have a particular reason to be there (following a scent, for example). Since their terminal velocity is so far below their fatal velocity, however, sometimes ants will willingly let themselves be stripped from their surface by gravity or wind, in order to distribute themselves more widely in foraging efforts.
-
Pedometer: Some ant species keep track of the distance they have traveled (they count their steps away from the colony entrance), which can help them regulate their foraging behaviors.
-
Magnetism: Some ant species orientate themselves using the magnetic field of the earth, like homing pigeons.
-
Temperature: Some ant species forage less on hot days to avoid dessication (dying by drying up; ants cannot regulate their body's moisture very well due to the fact that they breathe through a stomata that is always open). It is not clear whether this is emergent behavior or individual. Pheromone trails may evaporate more quickly on hot days, leading to decreases in foraging, but as many foragers do not use pheromones except when returning from a successful expedition, this does not entirely explain the behavior.
On the other end of the equation, ants also have biological needs (internal stimuli). They require food and water (although this is usually obtained by metabolizing moisture from food), rest, and air. All of these may affect an individual ant's reaction to an external stimulus - for example, if an ant finds food and is hungry, it will consume it, but if an ant finds food and is not hungry, it will try to carry it back to the nest externally.
The colony itself doesn't have needs because it is not a thing - it is a unitary system we humans use to conceptualize ant behaviors, but in the context of a game we can think of the colony as being a real thing with real needs, for the purposes of simulation. To survive a colony must maintain its own homeostasis, which consists of the following:
-
Having at least one reproducing queen: Many ant species' non-reproductive castes restrict their allegiance to only one queen (the one which birthed them), but others will sometimes allow multiple queens to coexist in a single nest. The queen creates all other castes through a combination of selective genetic expression and epigenetic factors (how the larvae are treated helps determine the morphology of their genes - their caste, if you will).
-
Nest: Except for army ants, all ants are sedentary and require a nest. This is usually constructed by worker castes excavating tunnels and chambers into various strata, such as wood, dirt, sand, agar gel, etc. This construct varies in size according to the number of ants comprising the colony that inhabits it - more ants digging means the nest gets bigger faster. Ants use their nests to protect their queen and larvae, shelter during periods of rest, store food, and in some species, to grow and/or harvest food (carpenter ants, aphid-farming ants, leafcutter ants, etc).
-
Larder: like many examples of megafauna, superorganisms such as ant colonies eat more than they need when they can get it, to offset times of scarcity when they cannot. In megafauna this is accomplished through expandable stomachs and crops, but ants take it a step further, harvesting and storing food in their nests or bivouacs for use in the future. Ants even secrete antibiotics which help sterilize storage chambers in their nests and preserve stored food items for later use.
-
Other reproductive morphologies: winged young queens and males (sometimes referred to as gametes) are also needed by a colony in order to pass on its genes during mating season. These castes are expelled from the colony at various times of year in order to find members of the opposite sex, mate with one or more of them, and then either start a colony (in the case of the young queens) or die (in the case of the gametes).
-
Young queens which stay in their original colonies may (depending on the species of ant in question) be eaten by the competing mother's workers.
With that brief overview of the sensors ants use to receive stimuli and non-sensed stimuli, it is easier to conceptualize what the AI stack of a simulated ant would look like. The first thing to note is that while many parts of the stack will be the same for all ants, many other parts will need to be different to account for the different behaviors observed in differing ant morphologies and species; a queen does not behave the same way as a brood ant, nor does a brood ant behave the same way as a forager, a soldier, or a gamete - and all of these will also vary to some degree among species. Army ant brood workers behave VERY differently to the brood workers of Harvester Ants, for example. In the average ant species, a variety of morphologies exist, all formed from the same larvae, born from the same queen, but altered by genetics and/or the food and hormones they are given by the brood workers:
- Queen
- Brood Worker
- Worker
- Major Worker ("Soldier")
- Young Queen
- Gamete ("Winged Male")
There are also two basic environments each of these castes might find themselves in: the overworld and the nest. Behaviors are quite different between these two worlds for some castes, while others behave identically or rather simplistically in one of the two environments.
For these reasons, the ant AI stack needs to be divided up into several different modules which control ant objects in the game world differently depending on the type of ant object (caste) and which constantly test for the type of world (overworld or nest) the ant object is operating in.
The final basic truth which lends philosophical structure to the way the ant AI stack is constructed in Formicidae is that some behaviors take priority over others: while an ant is ALWAYS doing something, there are always stimuli that could cause it to stop doing what it is doing in order to do something else - ideally, something more pertinent to the survival of its colony, but functionally something more pertinent to its own immediate survival. Thus ant AI can be conceptualized as stateful, in that it essentially should consist of an ant which holds one state at a time to be "true," and that state dictates its behaviors. It also means that the ant is capable of changing its state at any time if it senses proper (more "important") stimuli.
One of the most important stimuli an ant can experience is the presence of its own colonies pheromones, which are functionally feed-back loop systems for organizing colonial behavior. These chemical trails, while not precisely commensurate with the ant's state, are extremely important to any ant that encounters them, and hold an immense amount of sway the decision-making process of an ant - each is essentially the colony's code for performing a specific behavior in or near the location the pheromone was smelled. Real ant colonies probably have dozens of subtly different pheromones; for the purposes of Formicidae, we will restrict ourselves to just a few well-defined examples:
Forage Dig Brood Nest Alarm
Queen Object AI Stack
if in the Overworld, then dig a nest
else while Z axis position < 20
if Nest = true
then travel toward Nest gradient
else travel down
while pheromones = Alarm
flee from Alarm gradient
#does the following create a situation where a starved queen (<10 food) is broken?
while food > 50
then lay one egg, then wait a random number of seconds between 1 and 10
else Forage
while food < 10
do nothing