Lesson 6 Agent-based models

In analogy to system dynamics and to cellular automata models, we can view agent-based models through the lens of systems thinking and decompose these models into the three core parts of a system: purpose, elements and interrelations.

Upon completion of this module, you will be able to…

  • conceptualise agent-based models,
  • implement simple ABM models with GAMA, and finally
  • name a few typical problems that can be studied with the ABM approach.
A conceptual overview of the agent-based modelling approach.

Figure 6.1: A conceptual overview of the agent-based modelling approach.

Agent-based modelling is rooted in complexity theory. At its heart it aims to model system-level patterns that emerge from interactions of individual agents with their neighbouring agents and the local environment. The interactions are governed by rules (not by equations like in systems dynamics). The implementation of agent-based models therefore needs programming skills to code the rules (rather than an understanding of calculus. In ABM we are interested in causal relationships between individual behaviour and system-level processes, i.e. why a specific system level processes is observed. This bottom-up modelling approach was developed in parallel within three different communities: in biology it is called ‘individual-based’ modelling, in forestry ‘forest gap’ modelling and in the social sciences ‘agent-based’ modelling. For the rest of this module we will stay with the term ‘agent-based’ modelling, irrespective of the application domain.

6.1 Purpose

The purpose of agent-based models is to explain system-level properties by the behaviour of interrelated individuals. The behaviour of a population thus ‘emerges’ in a self-organised manner from the behaviour of individual animals during their life cycles and the interplay of these individuals with other individuals. System-level properties like population size, birth- and death rates are the result rather than the input.

Especially in the Life Sciences, agent-based modelling has gained momentum for its close resemblance of living systems, where the core units of an ecosystem are individual animals and plants. An animal for instance is not aware of any birth-rates at population-level. Actually, it will successfully give birth to offspring, if it finds a mating partner at due time, and if there is enough food around the nest for feeding.

The uniform distribution of bird nesting sites can be understood by its generation: birds try to maximise the space around their nests.

Figure 6.2: The uniform distribution of bird nesting sites can be understood by its generation: birds try to maximise the space around their nests.

Agent-based modelling is thus an adequate tool, if we know a lot about an individual and wonder about the collective behaviour. For example, to better understand the outbreak and distribution of a virus in human health research or the emergence of congestions at rush hours for better traffic management.

As ‘emergence’ is one of the key properties of agent-based modelling, we will have a closer look at this concept. Grimm and Railsback (2013) identify three criteria of emergent behaviour:

  1. Emergent properties are not simply the sum of the properties of the individuals
  2. Emergent properties are of a different type than the properties of the individuals (e.g. the spatial distribution of individuals is a system property of a type that none of the system’s individuals has)
  3. Emergent properties are often counterintuitive and cannot easily be predicted by looking only at the individuals.

The last point is an important one: ‘not easily predictable’ does not mean ‘impossible to understand’. In contrary, it is the primary goal of an agent-based model to unveil the mechanism of how system-level properties emerge from individual behaviour. If individual behaviour directly determines system-level behaviour, this would be an imposed behaviour – and we would not be able to learn anything from this model.

Consider a model that is designed to better understand the migration speed of a population. If each individual in this population walks at the same pace, the migration speed is imposed. However, if the walking pace of a specific animal depends on interactions with other animals nearby, or on decisions an individual takes in response to the local environment, then we will witness a not directly predictable – emergent – migration pattern of the population.

6.2 Elements: agents

Just like in the movies, an agent in an agent-based simulation model is a discrete and autonomous entity that acts independently in reaction to its living and non-living environment. Often agents are even more ‘intelligent’ and actively pursue a goal, have a memory and are able to learn and to adapt. In ecological ABMs, agents may be the trees in a forest. Tree agents are not considered intelligent, although they may be “purposeful” in the sense of optimizing access to a resource such as water.

James Bond is an agent: he is a unique person that pursues the goal to save the world, he always takes the right decision in difficult situation and he shoots the bad guys and saves the women. Agents in an agent-based model have comparable properties.

Figure 6.3: James Bond is an agent: he is a unique person that pursues the goal to save the world, he always takes the right decision in difficult situation and he shoots the bad guys and saves the women. Agents in an agent-based model have comparable properties.

Although, all agents follow the same set of rules during their life-cycles, their individual actions differ with respect to their specific spatial and temporal context. The spatial context in which agents are located typically is represented as a grid or a network. To a great extent, the individuality of agents is rooted in this explicit consideration of space.

From a computational perspective, agents are the basic building blocks of agent-based models. Their location, attributes and rule sets for their responsive behaviour is encoded in algorithmic form in computer programmes.

6.3 Relations: interaction

Smart agents behave in a way to react and adapt to a situation. Thus, agents need to take situation-dependent decisions. Such adaptation involves a set of rules, and also rules to change rules. Behavioural rules can either be derived from theory or from empirical data and they are basically expressed as “if-then” decisions. For example, a wolf-agent might follow a rule “if I am hungry and a sheep-agent is nearby, then I kill and eat the sheep”. Accordingly, a car-agent will follow a rule “if the traffic light turns green, then I continue driving towards destination”.

To be more realistic, behavioural rules can be more sophisticated. For example, a wolf-agent that is nearby a sheep might trade off its level of hunger against its fear of being spotted by the shepherd dog. So the presence of the shepherd dog can change the behaviour of the wolf.

Agents might also be able to learn from experience. A wolf that has been bitten by a shepherd dog once, tries to avoid dogs in the future. Agents’ learning abilities involve ‘memories’ of individuals.

Sometimes, interaction rules are not deterministic, but they mimic a stochastic process. For example, the wolf that wants to kill and eat the sheep might only succeed with a probability of 80%. The variability at system-level emerges from such stochastic behaviour as well as the spatial distribution of agents that interact locally, adapt to the local environment and have individual learning experiences.

Exercise: Defining interaction

Modelling interaction between agents is a bit trickier than defining the behaviour of individual agents. The definition of interaction between two agents (or agents and the CA) requires to define a subset of agents (or cells) and trigger an action only on this subset. Usually, interaction happens between agents that are close to each other and thus this subset is defined through spatial neighbourhood.

Consider the example of wolves eating sheep. You should be able to code the basic model yourself: create two species (100 sheep, 5 wolves), both with a moving skill that wander around randomly. Sheep are black, wolves are grey.

Now for the new bit: the hunting interaction involves that each wolf 1) creates a subset of sheep that are in a hunting distance of itself and that it 2) asks this subset of sheep to do something (here: to die). With help of a spatial operator we can create a subset of sheep in the local neighbourhood of the wolf. The use of the correct spatial operator is crucial. Go to the GAMA platform and search for the term “spatial operators” to check, which spatial operator fits our needs. Look for the terms “overlaps” in contrast to “overlapping” and “agents_overlapping”. What’s the difference?

To make wolves eat all the sheep within their hunting distance of 5 units, you need to add a reflex procedure to your wolves species that 1) defines the subset of sheep within a 5 unit buffer and then 2) ask these sheep to die. The subset is defined as the overlay of a buffer polygon around the wolf with the sheep locations. Note the elegant way of creating a buffer by taking the location of the agent and adding a circle with a radius of 5 by just using the + sign:

species wolves skills: [moving] {
  list<sheep> my_sheep;  
  
  // wolf interaction with sheep agents
  reflex eatSheep {
    my_sheep <- sheep overlapping (self + circle(5));
    ask my_sheep {
      do die;
    }
  }
}
See the full code.
/**
* Name: Ex L6a - ABM Agent-interaction
* Author: WALLENTIN, Gudrun* 
* Description: Exercise of the UNIGIS Salzburg optional module
* The agents of two species interact
*/

model ExL6aABMagentinteraction

global  {
    
    //Create the agents 
    init {
        create wolves number: 5 {       } 
        create sheep number: 100 {}
    }
}

//sheep agents
species sheep skills: [moving]{
    
    //random walk
    reflex movearound {
        do wander;
    }
    //visualisation
    aspect base {
        draw circle (2) color: #black;
    }
}

//wolf agents
species wolves skills: [moving] {
    list<sheep> my_sheep;  

    //random walk
    reflex movearound {
        do wander;
    }   
  
    // wolf kills nearby sheep agents
    reflex eatSheep {
        my_sheep <- sheep overlapping (self + circle(5));
        ask my_sheep {
            do die;
        }
    }
    //visualisation
    aspect base {
        draw circle (2) color: #grey;
    }  
}

//Simulation 
experiment agent_interaction type:gui {
    output {
        display map type: opengl{
            species wolves aspect:base;
            species sheep aspect:base;
        }
    }
}

6.3.1 Adaptive traits

In ecology, a set of properties and behaviour of an animal or plant is called a trait. This is a very useful concept also for agent-based modelling: all the attributes and rules that define an agent’s potential behaviour together are a trait. Everything this agent has at hand for decision making and acting with respect to its actual context – and eventually also to adapt to environmental changes. In the case of more intelligent agents that adapt to their environment, we refer to the set of potential behaviour as ‘adaptive traits’. All animals in a population share the same (adaptive) trait. However, this is just the potential ways to behave, the actual behaviour can differ between individuals.

An (adaptive) trait is the set of all properties and behaviour that together define the potential ways in which an agent can act.

Figure 6.4: An (adaptive) trait is the set of all properties and behaviour that together define the potential ways in which an agent can act.

The representation of an adaptive trait involves:

  • Awareness of its own state of fitness: Fitness seeking is a core driver for the actions an agent takes. Not least, it is the basic idea in evolution theory, e.g. How hungry am I? How fast do I drive?
  • Sensing the environment and other agents: To react in a context-aware way, the agent needs sensing abilities: “what?” (e.g. where are the prey, how steep is the terrain, “how much” (max. distance to sense), and “how accurate” (can various types of prey be distinguished?).
  • Memory: Some information (and related consequences) may stay in the agent’s memory.
  • Decision-making: A set of dichotomous ‘if-then’ rules. The basis of a decision is the own state of fitness, the sensed information and potentially memorised information.
  • Types of (inter)action: A decision is followed by an action (e.g. move forward), an interaction with another agent (kill and eat prey), or the environment (e.g. graze on grassland and thus reduce its local biomass).

6.3.2 Scheduling

In most agent-based models time is represented in the form of discrete, regular time steps. This means, that the same code is repeated each year, day or month – or whatever real-world correspondence a time step has. At each time step the agents evaluate their current state of fitness, sense the environment, take a decision and act. Each time step the model iterates through these processes. The specific way how sensing and action steps are scheduled determines the agent behaviour. The challenge hereby is to find the best way of representing concurrent behaviour. Strictly speaking, concurrent behaviour cannot be modelled, as a computer is not able to perform ‘multi-tasking’.

For now, it is sufficient to know that agent-based models are discrete in time and iterate through the code each time step. We will have a deeper look into the representation of time and space in simulation model in an upcoming lesson.

6.4 Typical application examples

Amongst the first processes that were studied from an agent-based perspective was the regrowth of trees in forest gaps. Until today, forest stand dynamics is a major application field of agent-based models. ‘Agents’ in these models vary between individuals, but the are not ‘intelligent’ in the sense of having memories or learning behaviour. Therefore, the term ‘individual-based models’ is often used synonymously with agent-based models in biology. However, further prominent examples in biology coined the term ‘crowd intelligence’ such as the formation of huge flocks by birds: this is a typical emerging behaviour. It can fully be explained by interactions between individuals, without one bird that would take command. Such intelligent crowd behaviour is sometimes used in heuristic methods, for example the ‘ant colony algorithm’ in finding shortest path solutions in networks.

From a geographical point of view, agent-based models opened the possibility to study systems from their spatial perspective. An influential model towards this end was the repetition of the traditional system-dynamics model on predator-prey systems in the form of a spatial agent-based predator-prey model that triggered an increasing interest in the spatial distribution of animals (e.g. Wilson, Deroos, and McCauley 1993). Heterogeneous spatial configuration of the environment was found to have a stabilising effect on population dynamics (e.g. Crowley 1981). Spatial territories emerge through species interactions (Potts and Lewis 2014) and habitats emerge from species competition for resources (Grimm and Railsback 2013).

Exercise: Agents interact with a CA

In this exercise, we return to our virtual pasture. Up to now, cows moved around randomly. However, real cows would always go to the spot with the grass that has the highest biomass within their reach. So, I suggest to use a “goto” movement, where the target is the grass with the most biomass. This is written as follows:

do goto target: (grass at_distance cow_speed) with_max_of (each.biomass) speed:cow_speed #m/#s ;

What does a cow do, if it has reached a spot with tasty grass? Right: we need to implement grazing! So within the same reflex, right after the movement command in the cow species section, each cow asks the grass (remember: this is the name that we gave to the grid) that is closest to itself to reduce its biomass. Very polite :).

ask grass closest_to (self) {
    biomass <- biomass - 2;
} 

There is just one problem: what, if there is less than 2 units biomass left on that spot? The biomass cannot have a negative value. Thus, we add an if statement:

ask grass closest_to (self)  {
    if biomass > 2 {
        biomass <- biomass - 2;
    }   
    else if biomass > 0 {
        biomass <- 0.0;
    }
} 

That’s it! Enjoy the five cows grazing on the Vierkaser pasture.

See the full code to double-check.
/***
* Name: Ex L6b ABM-CA interaction
* Author: WALLENTIN, Gudrun
* Description: Exercise of the UNIGIS Salzburg optional module
* pasture model: interaction of mobile agents with a dynamic environment
***/

model ExL6b_ABM_CA_interaction

global  {
    //Load geospatial data
    file study_area_file <- file("../includes/study_area.geojson");
    //fenced area includes shrubs and grassland
    file fenced_area_file <- file("../includes/vierkaser.geojson"); 
    //high-quality pasture area
    file lower_pasture_file <- file("../includes/lower_pasture.geojson"); 
    //low-quality pasture area
    file hirschanger_file <- file("../includes/hirschanger.geojson"); 
    
    //grass regrowh rate
    float grass_growth_rate <- 0.01;
    
    //the action range of a cow in one time step (=potential speed)
    float range <- 7.0;

    //Define the extent of the study area as the envelope (=bounding box) of the study area 
    geometry shape <- envelope (study_area_file);
    
    //extract the geometry from the vector data
    geometry fence_geom <- geometry(fenced_area_file);
    geometry lower_pasture_geom <- geometry(lower_pasture_file);
    geometry hirschanger_geom <- geometry(hirschanger_file);
    geometry pasture_geom <- lower_pasture_geom + hirschanger_geom;
    
    //declare a (initially empty) subset of grass cells 
    list<grass> pasture_cells ;
    
    //Create the agents 
    init {
        //create 5 cow agents that are located within the pasture
        create cows number: 5 {
            location <- any_location_in (pasture_geom);
        }
        //colour the grass already at setup
        ask grass {
            color <- rgb([0, biomass * 15, 0]);
        }   
    }
}       
        

// cow agents
species cows skills: [moving] {
    
    //behaviour: move to grass
    reflex graze {
        do goto target: (grass at_distance range) with_max_of (each.biomass) speed:range #m/#s ;
        
        ask grass closest_to (self)  {
            if biomass > 2 {
                biomass <- biomass - 2;
            }   
            else if biomass > 0 {
                biomass <- 0.0;
            }
        } 
    }
    
    //visualisation
    aspect base {
        draw circle (5) color: #red;
    }
}

//the pasture CA is a hexgrid with cells 5m x 5m
grid grass cell_width:5 cell_height:5 neighbors:6 {
    float biomass <- 0.0;   
    float max_biomass <- 0.0;
    rgb color <- rgb([0, biomass * 15, 0]) update:rgb([0, biomass * 15, 0]) ;
    
    init {  
        //assign the value of 1 as the max. biomass within the fenced area 
        if self overlaps(fence_geom){
            //set the maximum biomass of a grazeland cell to 1
            max_biomass <- 1.0;
            biomass <- 1.0;
        }
        //if the cell overlaps the lower pasture: assign the value of 10 to the biomass (this overwrites the biomass=1 within the fenced area) 
        if self overlaps(lower_pasture_geom){
            max_biomass <- 10.0;
            biomass <- 2.0;
        }
        //if it is not in lower pasture, but it overlaps Hirschanger: assign the value of 7 to the biomass ("else if" overwrites the fenced area, but not the lower pasture) 
        else if self overlaps(hirschanger_geom){
            max_biomass <- 7.0;
            biomass <- 2.0;
        }
    }
        
    // let grass grow until its maximum potential
    reflex grow_grass when: max_biomass > 0 {
        //logistic growth: N + r*N*(1-N/K)
        biomass <- biomass + grass_growth_rate * biomass * (1 - biomass / max_biomass);
        color <- rgb([0, biomass * 15, 0]);
    }           
}

//Simulation 
experiment virtual_pasture type:gui {
    output {
        display population_chart type: 2d {
            chart "mean biomass" {
                data "number of cows" value: length(cows);
            }
        }
        display map type: opengl{

            grid grass;
            species cows aspect:base;
        }
    }
}

References

Crowley, Philip H. 1981. “Dispersal and the Stability of Predator-Prey Interactions.” The American Naturalist 118 (5): 673–701.
Grimm, Volker, and Steven F Railsback. 2013. Individual-Based Modeling and Ecology. Princeton university press.
Potts, Jonathan R, and Mark A Lewis. 2014. “How Do Animal Territories Form and Change? Lessons from 20 Years of Mechanistic Modelling.” Proceedings of the Royal Society of London B: Biological Sciences 281 (1784): 20140231.
Wilson, WG, AM Deroos, and E McCauley. 1993. “Spatial Instabilities Within the Diffusive Lotka-Volterra System: Individual-Based Simulation Results.” Theoretical Population Biology 43 (1): 91–127.