Skip to main content

Python in the browser: Evolving Networks.

1260 words·6 mins

The surge of technologies like WebAssembly and its associated ecosystem ecmscripten , pyscript and panel, allow the development of web content in languages like C, Go, Julia or Python, which can be executed on almost any modern web-browser. This provided me with an excellent justification to revise some interesting models and simulations close to my heart, and which can be used to produce demos I can put on spaces like this website.

One example which is very much of interest today not only to me but to several people in the ecology and evolution communities is that of the formation, co-evolution and resilience of ecological networks.

The advantage of being able to share codes in repositories for anybody to run their own simulations, is now enhanced with the ability to literally execute the same code in the browser! This allows anybody to get a flavour of the dynamics without having to install and download anything.

Ecological Evolution as a complex system.
#

Here I briefly discuss the main features of the Webworld model. This milestone model has been applied and studied in several contexts. Essentially, it provides a dynamical framework to understand how an ecosystem produces the hierarchical predator/prey structures called foodwebs from an evolutionary point of view, and to produce networks of who eats who with properties similar to those observed in nature.



Typical foodweb evolution obtained with webworld. Each node represents a species, the node radius is equal to the logarithm of the population and the width of the links is equal to the strength of the interaction. We start with a single species, and continue to show the network as more species are introduced in the system. The arrows show the direction of the resources. At the very beginning of the evolution, the only species present feeds from the external resources.

Foodwebs
#

A foodweb is constructed by considering all the species present in an ecosystem and quantifying all the predators and prey of each species. If a species does not posses any prey, but the external resources, then it belongs to the basal trophic level. If on the contrary, a species does not have any predators, only prey, then it belongs to the top trophic level. A species which posseses both predator and prey species, is accommodated in a trophic level computed by the shortest path to the external resources. In general, foodwebs only have a few of trophic levels and the population sizes of basal species are often orders of magnitude larger that those is the intermediate and upper levels.

The webworld model - evolution as a complex system.
#

A very important aspect of modelling natural phenomena consists of linking the set of equations and core ideas with some representation of the solutions, like a plot or a movie of showing the dynamical behaviour of the system, which is not an easy task itself. Specially, If the system under study is operates in several time-scales in space and time, and it is composed with many different interacting elements.

Complex systems, are those in which their components interact locally amongst them according to some set of rules and are unaware of any global pattern emerging from those interactions. For instance, in an ecosystem, species interact by predator/prey relationships. Some species prey on other species, and at the same time are eaten by some predator species, alongside those processes, species also produce offspring an die of natural causes with some seasonal rate, thus, modifying the species population. The population update processes, might lead to patterns, such as oscillatory behaviours, stable co-existence and extinctions, events which occur at the ecosystem level, well beyond the level of the events of eating and being eaten.

These seasonal population updates occur in the time scale of months/years and in general we can write a model as follows.

$$\dot{N_i}(t)=\lambda \sum_j g_{ij}(t)N_i(t) - \sum_j g_{ji}(t)N_j(t)-d_iN_i(t) \quad (1)$$

The parameters \( \lambda\) and \(d_i\) correspond to the efficiency at which the resources transfer from a prey to its predator, and the natural death rates.

The only remaining quantities required to complete this model are the specification of the predation rates \(g_{kl}(t) \) between species \( k \) and \( l \). However, the goal here is more ambitious, we want to know, how does and ecosystem is formed in the first place, and how does natural selection drives the system to a mature configuration, in which the system (1) operates. To do so, we might, first consider a starting ecosystem consisting of a single species and establish the criteria for species evolution, and how to determine if two species posses a predator/prey relationship.

Species features and mutations.
#

Species are defined by a set of phenotypical features. This is a set of \(L \) different integers \( f_i=\{n_j\}_{j=1}^L \) taken from a pool of \( K \) possible values. Each of the features is compared against each other by a score encoded in a anti-symmetric \( K\times K \) matrix \(\bf{M}\) which contains a random number. Then if we consider two species \( i,j \). We score them by computing:

$$S_{ij} = \max \lbrace 0,\frac{1}{L}\sum_{a \in i, \thinspace b \in j} M_{a,b}\rbrace \quad(2)$$

Which allow us to determine if there exists a predator prey relationship between \( i \) and \( j\). With this definition of species and species score \( (2) \) we can treat the external resources as species \( “0” \), with a string fixed and a large population value \( R \).

Choice of diet, rates and evolutionarily stable strategies.
#

Once that we have a definition of species and a way to compare them. We can assume that species which are close in terms of features and posses a common prey compete harder for a common prey between them, in comparison with species which are not very similar. In fact, inter-specific competition will be the hardest. To achieve this, the model defines the competition score between species \(i\) and \(j\) as:

$$\alpha_{ij} = c+(1-c)q_{ij}\quad(3)$$

Where \(c \in (0,1)\) is a competition parameter and \(q_{ij}\) is the fraction of common features between species.

For the rates \( g_{ij}(t) \) the model uses the generalised ratio dependent functional response:

$$g_{ij} = \frac{ f_{ij}S_{ij}N_j }{bNj + \sum_{k} \alpha_{ki}f_{kj}S_kjN_k} \quad(4)$$

This choice \( (4) \) introduces the fractions of effort \( f_{ij} \) a species \( i \) puts into foraging for \(j \). In other words:

$$ f_{ij} = \frac{g_{ij}}{\sum_j g_{ij}} \quad(5)$$

Equations \( (4) \) and \( (5) \) are re-adjusted each time the populations change. These, determine the foraging strategy of the each predator. This choice of efforts is an evolutionary stable strategy (ESS), which means that any other strategy can not be successful against it, provided the ESS is already taken by the majority. The dynamics of the efforts takes place in the shorter time scale of the model, which is that of days.

Evolution of the system in between evolutionary steps, showing the population updates and the adjustment of the efforts (links thickness). We can see an extinction eeven, once a population reaches a value below 1.0

For a fixed set of species, the foraging and population dynamics will eventually lead to a stationary state, where nothing will change any further. Once the fixed point is achieved, a new species is added to the system. A new species is introduced, by choosing any of the surviving species and replacing one of the features for a new one. The new species starts with a population on one individual and the short and intermediate time scale dynamics starts again.

Evolution of the number of species (right panel) and the efforts matrix (left panel) corresponding to the foodwebs shown earlier. Notice that the system starts with one species and then it fluctuates by the action of extinction events and evolutionary events.


Code repositories.
#

If you want to play with this model in your own computer, or have an issue, there is a github repository available here

For the live version of the code, the hugginface model space lives here and its embedded below!

Live Version.
#

This is it! Run the model right here, right now!. Press the run button and you will see the number of species updating every few hundred evolutionary events (species introduction). This will run until the system performs 5000 evolutionary time-steps or it reaches 59 species. Once the simulation is completed you can see a diagram of the foodweb in the tab labeled PlotFW. The diagram in the live version is drawn with networkx instead of matplotlib, such as the ones provided in the github repository, and shown in this post. Please be sure you also visit the hugginface repository to run this on a full screen!

Suggested readings for in-depth details and discussion of the model and results.
#

For a thorough discussion on the theory of ecological communities, details in the implementation of webworld and other evolutionary approaches, please check the papers and references in the following list:

  1. Modelling coevolution in multispecies communities J. Theor. Biol.

  2. The influence of predator–prey population dynamics on the long-term evolution of food web structure J. Theor. Biol.

  3. The impact of nonlinear functional responses on the long-term evolution of food web structure. J. Theor. Biol.

  4. Modelling food webs Handbook of Graphs and Networks: From the Genome to the Internet

  5. Topological structure and interaction strengths in model food webs. Ecol. Model.

  6. The robustness of the Webworld model to changes in its structure. Ecological Complexity

  7. The characteristics of species in an evolutionary food web model J. Theor. Biol.