Amphibians vs. Preys - A Linear Programming Analysis

Florian Roscheck, 2021-02-13

In this notebook, we use Google's OR-Tools to solve an optimization task with linear programming. Linear programming is a way of optimizing the outcome of a mathematical model with linear dependencies under constraints.

Task

We have the following task:

A zoo biologist will place three species of amphibians (a toad, a salamander, and a caecilian) in an aquarium where they will feed on three small different preys: worms, crickets, and flies. Each day 1500 worms, 3000 crickets, and 5000 flies will be placed in the aquarium. Each amphibian consumes a certain number of preys per day, as shown in the table below.

Food Toad Salamander Caecilian Available
Worms 2 1 1 1500
Crickets 1 3 2 3000
Flies 1 2 3 5000

How many amphibians, up to 1000 of each species, can exist in the aquarium, assuming food is the only relevant constraint?

Summary

First, let's load the OR tools library!

Defining the Problem for OR-Tools

The first step for defining the problem for OR-Tools is to select an appropriate solver. It is important to realize that we are dealing with integer numbers. We know that there are no preys that are only consumed half and that there are no amphibians that can only exist in part. Amphibians are either alive and eat or dead and do not eat.

For OR-Tools we characterize this problem as Mixed Integer Program. According to the documentation, a third-party MIP solver is most appropriate for solving this problem, as none of the variables in this problem are booleans.

The third-party SCIP (Solving Constraint Integer Programs) solver ships with OR-Tools and is an MIP solver. So, let's use it to solve this integer problem!

Next, we need to define the variables we would like to solve for. These are the amphibians whose total count we would like to maximize. Given our task definition, we want to allow up to 1000 individuals of each species.

As just stated, we want to maximize the total number of amphibians in the aquarium. This is our objective function!

Now, let's deal with the constraints on the food supply. We will add a constraints for each type of prey.

Letting OR-Tools Solve the Problem

Now that we have our problem defined, let OR-Tools solve it.

A status of 0 means that OR-Tools was able to find an optimum solution for the problem. Great!

Exploring the Solution

Now let's analyze OR-Tools' solution. We would like to understand what the total number of amphibians in the aquarium is. Also, we are curious to find out how amphibians (demand) and preys (supply) match up.

To get a better insight into the model's optimal solution, let's write out some code.

The total number of amphibians in the aquarium is 1400. Of those 1400, 1000 are caecilians, 300 are salamanders, and 100 are toads. This means that there is not enough prey to feed all 1000 potentially available toads and salamanders. Of course, we could feed more toads and more salamanders – but the cost would be that we could feed less caecilians and the overall number of total amphibians in the aquarium would decline. Such is the nature of the optimum!

While the amphibians are able to consume all works and crickets we supply, about 1300 flies are remaining unconsumed per per day. If the supply costs us, we could save money by buying only 3700 flies per day, instead of the 5000 currently available.

The 1400 residents of the aquarium consume 8200 preys per day – an impressive number, resulting in an approximate 5.9:1 prey-to-amphibian ratio.

Thinking Outside of the Box: A Generalized Ecosystem Model

In a broader sense, the specific amphibian-prey example at hand resembles an isolated market of supply and demand. We are trying to find out the maximum achievable market penetration given a specific, limited set of supplies.

In this spirit of generalization, the author has adapted and extended the analysis above into a Python Ecosystem Class. This class is explained in detail in the ecosystem_example notebook.

Let's use the Ecosystem class to re-build the ecosystem we defined above and get an even better picture of the feeding situation inside the aquarium.

Not surprising (but maybe reassuring), the results from the Ecosystem class are the same as for the manual model process above.

A Detailed Look Into the Food Supply

A question that we did not answer previously is "To which amphibians do the different food sources go?". The Ecosystem class helps us get a better insight into this.

We see that most supplies go to feeding caecilians (of which we feed 1000) and just 4.2% go to feeding toads (of which we feed just 100).

It would also be interesting to know how many preys exactly go to which species. The Ecosystem class also provides this information.

It is impressive how many flies get consumed by caecilians vs. toads (30x as many). Caecilians consume just 5x as many worms as toads, although there are 10x as many caecilians in the aquarium as toads.

Next Steps

Although the problem at hand may seem small and complicated, there are many more ways to look at it and extract valuable insights:

First, it would be interesting to know more about the business context of the problem. If we were working for a client, what is the client trying to achieve? Reduce supply cost? Increase number of amphibians per day? Estimate a necessary aquarium size? Depending on the business context, we could use the OT-Tools model to run experiments and give specific recommendations.

Second, we may want to look further into non-linear effects in the ecosystem. For example, depending on the number and composition, and age of amphibians and preys in the aquarium, the temperature in the aquarium may rise, potentially endangering the survival of either prey or amphibians. The oversupply of flies may also have detrimental effects on prey and amphibian survival. If no flies are removed, the number of flies in the aquarium trends towards infinity. Finally, amphibians or prey may reproduce, leading to further non-linearities.

Third, while OR-Tools provides an optimal solution, it may be still worth exploring the solution space by accounting for uncertainties of estimates. It seems hard to count 1000 flies or caecilians exactly. What happens if any of the species in the species is under- or overcounted? We should account for that and explore how solutions vary. Here, too, it is important to recognize the business context of the problem. Ideally, uncertainties are tied to costs. Knowing which uncertainties matter most helps us to focus our counting efforts (and reducing related uncertainty) to the tasks most effective for the business outcome.