A Matlab user-interface tool for modelling herds

Animal flocks, schools, and herds are iconic examples of agent-based complex systems. Individual animals interact with each other in ways we can model on a computer, with group properties emerging under certain conditions. In this paper we provide a Matlab-based user-interface tool (UI) that allows users to model herds with two prominent algorithms—the Voronoi and Local Crowded Horizon attraction models—in environments with and without predators. This UI also allows the user to include random animal motion (or noise) with the herding algorithms, which can facilitate herd cohesion and lower predation risk. Our UI is well suited for the teaching of complex systems, allowing students and those new to the field to build an intuition on how group behaviour can emerge from individual interactions. Ultimately, relating simulation results to observations of animals in nature helps refine our models and improve our understanding of these biological systems.

that allow group properties to emerge. The conditions under which emergent properties arise may be applicable beyond the particular system being studied, so the larger goal is to discover rules that are as general as possible. Examples of complex systems abound in physics (ferromagnetism), biology (biofilms), and in social interactions (crowd and traffic patterns, stock market gyrations) (Moussaïd et al 2011, Newman 2011, Vicsek and Zafeiris 2012, Holovatch et al 2017. Indeed, the human ability to think is an emergent property that depends on the interaction of many billions of neurons. The focus of our paper is to provide a Matlab userinterface tool (UI) that allows users to build intuition for the behaviour of complex systems using the specific example of animals in a herd.
Many animals group together in herds, which provide several advantages. Most notably, herds provide protection against predators, encapsulated in Selfish Herd theory (Hamilton 1971)-and it is this quality that our UI explores. There are certainly additional advantages for herding animals: social interactions that build bonds (Silk 2007), mating opportunities (Arnold and Owens 1998) and foraging advantages (Barnard 1980). In principle, it would be enlightening to build a simulation tool that explores how all these advantages might arise. In practice, this is too broad: modelling each emergent property typically adds an additional dimension (or even several) to the available parameter space, which quickly becomes computationally problematic. Instead we simplify the system and the interactions as much as possible, consistent with still observing the particular emergent property we wish to study.

Agent-based interaction models
Perhaps the best-known agent interaction model is the Vicsek model (Vicsek et al 1995), whereby the movement of each individual is influenced (equally) by neighbours within a certain fixed radius, as in figure 1.
For living ('self-propelled') systems, the original Vicsek model states that individuals adjust their velocity to align with the average of their neighbours' within this fixed radius. By doing so, momentum is not conserved-which is fine for living systems with internal energy reservoirs. Certain inanimate systems can be made to respond similarly with the application of an external energy source (Vicsek and Zafeiris 2012). In either case, a phase transition from a disordered state to an ordered one can occur under certain conditions involving velocity, density, and noise.
While the Vicsek alignment model is very successful in modelling phase transitions and is the basis for many models of group formation, other types of models may be applicable to certain systems. Progression from disordered (or randomly distributed) states to ordered ones can also occur through an attraction between group members, with pattern formation (Strömbom 2011) and a reduction in predation risk (Ose and Ohmann 2017) emerging from the transition. Some studies may even incorporate both alignment and attraction models (e.g. Couzin et al 2002). Our Matlab UI incorporates two prominent attraction models: the Local Crowded Horizon (LCH) model and the Voronoi (V) model.
The V model is motivated by observations of starlings (Ballerini et al 2008). Whereas social interaction in many grouping models depends on a fixed interaction distance or radius, the V model depends only on the relative positions of the animals in the group, without regard to how far apart these animals are. Formally, we say the Vicsek model is metric, whereas the V model is topological. Okabe et al (1992) gives a helpful introduction to V diagrams with many applications.
The V topology states that individuals are influenced by those in adjacent cells in a V tessellation, as in figure 2 (Ginelli and Chaté 2010, Schubring and Ohmann 2013, Strandburg-Peshkin et al 2013. Each V polygon encompasses the region closer to a particular individual than to any other; its area gives a measure of predation risk (the 'domain of danger' or DOD) from predators within the herding area. An important feature of the V topology is that animals in adjacent V cells are necessarily within line-of-sight. This is not always the case in the Vicsek model, in which some animals may be shielded from view by others within the radius of influence. In this way, the V topology more naturally incorporates certain sensory information important for particular animal groups, like starlings. Our UI assumes overlapping sensory information (e.g. sight, hearing, and smell) so that all V neighbours have the same influence, without blind spots or other missing information that can occur in some animals due to their physiology or under certain environmental conditions (McComb and Kajiura 2008, Barberis and Peruani 2016).
It should be noted that the V topology, rather than the Vicsek fixed radius, can be used with velocity alignment interactions, as with the modelling of Merino sheep by Ginelli et al (2015). However, both our V model as well as the LCH model (Viscido et al 2002) instead use an explicit attraction between herd members. In our V model, individuals are attracted equally to their V neighbours, whereas individuals in the LCH model are attracted to each herdmate with a weighting that decreases with distance. Figure 1. A collection of 50 individual agents (shown as dots) randomly distributed on a 360×360 grid, which we take as the initial positions of our herd members. Each individual in the Vicsek model is influenced by others within a fixed radius, as shown by a red circle around a particular animal.

V and LCH movement models
At the start of each run in a UI simulation, individual animals are placed on a 360×360 flat grid, following Ose and Ohmann (2017). Our simulations actually incorporate slightly larger bounds (400×400) to allow random motion to carry individuals briefly beyond the observed grid space, though interactions in either the LCH or V model will prevent animals from straying too far even with substantial noise. Each simulation involves one or more runs, each with the number of discrete time steps specified by the UI. If open space is available, animals move one body length (BL) per time step. The position  r i of individual i at time step t+1 is based on the position and normalised velocity  v i calculated at the previous time step t (i.e. a 'backward update'): . Though animal speeds are set to one BL per time step, the direction of each animal is determined by the V or LCH model, and may include a random motion or noise component as specified by the UI. This normalised velocity for each individual i can be written Figure 2. An example of a Voronoi tessellation using the same herd of 50 animals as in figure 1. Voronoi polygons representing the domain of danger (DOD) surround each animal, and encompass the area nearer to that individual than to any other. Larger DODs imply a greater vulnerability to predators in the herding area. We highlight one animal in red, with its Voronoi neighbours in green.
Noise ,Noise with A Model and A Noise describing the weighting of the model and noise components,d i,Model andd i,Noise respectively. Since we are primarily interested in the relative effects of noise on each of the movement models, we set = A 1 Model in both the V and LCH models, with A Noise ranging between 0 and 5 as specified in the UI. The noise componentd i,Noise is simply a randomised direction on the unit circle.
Each Random motion can serve different purposes in these animals, from helping locusts restore group alignment that had previously been lost to helping soldier crabs enter an area that they normally avoid. Recently Ose and Ohmann (2017) showed that including a random motion component in overall motion can also facilitate herd compaction and reduce predation risk. This emergent property can readily be seen with this UI by running the appropriate simulations. Our Matlab UI incorporates two distinct ways of measuring predation risk. The first is the calculation of the DOD around each individual (Hamilton 1971). Formally, the DOD describes the area nearer to a particular individual than to any other; mathematically, this corresponds precisely to the V polygons shown in figure 2. The central idea is that more exposed herd animals are at greater risk from predators in the herding area, such as a snake. As a herd forms, the interior members are better protected and have a lower predation risk, which is reflected in the smaller DOD for these individuals. Of course, not all herd members are advantaged: the more exposed exterior animals have a greater predation risk described by their larger DOD. Still, on balance, herd formation protects a greater share of individuals than would be the case if animals were randomly scattered.
Herd animals may also be at risk from predators emerging outside the herding area, such as a lion (Vine 1971). Our Matlab UI has a Mixed Herd setting that models a predator emerging from outside the herd after a specified time. This predator finds the nearest prey (which may change during the pursuit) and eventually 'kills' it. This Mixed Herd setting allows some herd members to follow one movement rule and the others to follow a different one (in each case with or without noise), so that relative predation risk can be determined from predators originating outside the herding area.

Overview
The Matlab UI code is hosted on GitHub 1 and may also be obtained from the corresponding author. The UI is accessible from either the original Matlab source code Predator_Prey_UI.m or the executable file Predator_Prey_UI_Installer.exe. Additional installation details are contained in a Quick Reference Guide included with the code on GitHub. For users with Matlab software, using the original source code version is recommended. The UI was developed for the Windows platform, though Matlab is supported on Mac and Linux as well 2 . The UI formatting is optimised to large (desktop) or small (laptop) screens through buttons on the upper left of the UI, shown in figures 3 and 4 below.
Users select the type of simulation (DOD or Mixed Herd) using either a reference herd with a fixed number of members that start in the same initial positions, or else an initial herd with a variable number of members with randomised initial positions. For DOD simulations, all animals are grouped into one herd with members following the same movement rule: LCH, LCH+noise, V, or V+noise. Mixed Herd simulations allow two separate herds of variable sizes, each following its own movement rule. Slider bars on the UI specify the number of time steps in each run, the number of runs per simulations, and the noise weight A Noise if applicable. Settings can also be directly typed in the appropriate box to the right of each slider bar.
Simulation results are shown by two tabs on the data output window: one displays the cumulative statistics and the other the animal locations in an animation, both as a function of time as each run progresses. The generated data is saved to a text file, the name of which can be input by the user. We describe additional simulation details immediately below, with learning objectives suggested in the subsequent Discussion section.

DOD example
DOD simulations give a measure of the improvement in predation risk as a group of animals coalesces into a herd. These simulations compare the area of the V polygon initially surrounding each animal to the corresponding area after the selected number of time steps. If grouping into a herd provided no net benefit, approximately 50% would improve their DOD. Comparing the histogram results in figure 3(a) to the animations in figure 3(b) makes clear the relationship between herd cohesion and DOD improvement, as well as the value of including a noise component in the overall movement.  chosen. An animal following the first movement rule (LCH + noise) is represented as a blue 'x' and an animal following the second movement rule (V + noise) as a red '+'. The predator is displayed as the symbol ' ', located here on the left side of the animation window.

Mixed herd example
Mixed Herd simulations give a measure of predation risk from a head-to-head competition between two movement rules. After the herd members interact for the number of time steps selected, a predator appears at a random location (generally starting off-screen, 250 BL from the herd centre). This predator chases and eventually 'kills' the nearest animal when it moves within 1 BL of its target during this extended time. We record the 'death' of this animal and the particular movement rule it followed, and then a new run begins. As shown in figure 4(a), the Statistics window shows which type of movement rule makes animals more vulnerable to predators appearing from outside the herd; it is complementary to the DOD histogram used to measure risk from predators emerging from within the herding area itself. Figure 4(b) shows the Animation window for the Mixed Herd simulation, with locations of herd members following each of the two selected movement rules marked by blue x's or red +'s respectively. In each simulation run the interaction between herd members is visually apparent, as is the pursuit by the predator (marked by the symbol ) during extended time.
The idea behind introducing a predator in the UI simulations is that it serves as a mechanism to remove vulnerable prey, thereby giving a measure of relative effectiveness of our motion models with and without noise. The predator itself is simplistic: after it appears, the predator simply moves towards the nearest prey animal at the identical speed of 1 BL per time step. During its pursuit, the predator will switch targets if a different prey animal becomes nearer the predator than the original animal. In addition, the prey are not aware of the presence of the predator and do not change their motion model during the simulation run. Certainly more sophisticated predator-prey dynamics could be incorporated, and we make our code freely available to those who are interested.

Discussion
Our Matlab UI presents several learning opportunities. The first is the science itself-an understanding of how herding animals might move and interact, given the inherent predation risk to which these animals are normally exposed. Related to this is an appreciation for the useful role that random motion might play in animal behaviour, facilitating herd compaction and lowering predation risk when combined with a movement model based on herdmate interactions. While two prominent attraction models-LCH and V-are incorporated in this UI, the open distribution of this software allows others to be included as desired, such as the Vicsek velocity alignment model (Vicsek et al 1995) or the various models described in Morton et al (1994) and Viscido et al (2002).
Beyond the science, this UI has useful pedagogical benefits. Most importantly, the UI shows the power of computer simulation as a method to address problems that are too difficult to solve analytically. Imagine trying to write (let alone solve) an analytical equation describing the interaction between 100 animals! Yet this UI as well as the computational algorithms on which it is based (Ose and Ohmann 2017) were both written by physics undergraduate students, with faculty guidance.
Finally, this UI illustrates the importance of statistics in drawing conclusions from simulations. Since an arbitrary group of animals in a homogeneous environment can have any particular set of initial positions, it seems reasonable to distribute them randomly in some confined grid space. However, interactions between particular animals as well as individual predation risk will have some dependence on this initial configuration (Morrell et al 2011). We can therefore only draw conclusions regarding herding behaviour and predation risk after multiple runs with different initial starting positions, so that idiosyncratic behaviours in any particular run will be washed out by the statistical analysis of a large amount of data. This UI shows animations of each run individually as well as the cumulative statistics, allowing students to gain an understanding of how the results of each run fit into the larger whole.