Lotka-Volterra Simulation Using C#

When I was a math student in college, I remember taking a class that was about math models. The Lotka-Volterra (LV) predator-prey model made a big impression on me. Recently, I was looking at models of biological neurons with a view towards using the ideas for advanced spiking neural networks. I realized that bio-neuro models were similar in some respects to Lotka-Volterra, so I decided to warm up by coding a simulation of Lotka-Volterra.

Before I go any further, let me mention that the vast majority of resources for LV simulation I found on the Internet were misleading in some way, or even had significant errors. For example, the Wikipedia entry on LV gives an example with baboons and cheetahs which is quite misleading because it leaves out a few critical details. One of the problems is that classical LV uses calculus to solve a pair of related differential equations, but a computer simulation uses a very different approach.

The two key LV equations are:

dx/dt = (a * x) - (b * x * y);
dy/dt = (c * x * y) - (d * y);

x = x + dx/dt
y = y + dy/dt
t = t + dt

where x = number of prey (rabbits) at time t,
y = number of predators (foxes) at time t,
a = prey growth rate, b = prey death rate,
c = pred. growth rate, d = pred. death rate

The specific values of a, b, c, d determine how the numbers of prey and predators vary over time. Using a = 0.09, b = 0.01, c = 0.01, d = 0.04, I graphed the results of my simulation for 1000 time steps. Notice that my choices for a, b, c, d don’t give a realistic result because the graph shows there are usually more predators than prey, but in most real bio systems, the number of prey usually greatly outnumber the predators.


I captured the output from my demo program then dropped the values into Excel and made this graph.

Even though the simulation program only has a few lines of code, it’s extremely tricky. For example, the dt value I used, 1, is arbitrary and is type integer. Changing the dt scale, for example by scaling it by 0.001, is possible but then you have to adjust the scale of the a, b, c, d constants. And of course, the numbers of predators and prey must be a whole number in real-life, as opposed to values like 0.6 that can occur in a simulation.

Anyway, good fun. I refreshed my memory of math modeling of differential equations using simulation, and I can apply that knowledge to modeling biological neurons, such as the Hindmarsh-Rose model. Complete C# code below.



Alice in Wonderland is sort of an abstract model of reality. The 1951 Disney animated film is the best known version but the Internet has dozens of creative illustrations too.


using System;
namespace LotkaVolterra
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine("\nBegin Lotka-Volterra simulation");
 
      double x = 10.0;  // rabbits / prey
      double y = 5.0;  // foxes / predators
      int t = 0;   // time

      double a = 0.09;  // growth rate of prey
      double b = 0.01;  // death of prey
      double c = 0.01;  // growth of predators
      double d = 0.04;  // death of predators
      Console.WriteLine("Initial prey = " +
        x.ToString("F1") + " predators = " + y.ToString("F1"));
      Console.WriteLine("Prey growth     = " +
        a.ToString("F3") + " prey death     = " + 
        b.ToString("F3"));
      Console.WriteLine("Predator growth = " +
        c.ToString("F3") + " predator death = " + 
        d.ToString("F3"));

      Console.WriteLine("\ntime" + "\t" + "prey" +
        "\t" + "predators"); 
      for (int i = 0; i less-than 1000; ++i)
      {
        Console.WriteLine(t + "\t" +
          x.ToString("F1") + "\t" + y.ToString("F1"));
        double dx = (a * x) - (b * x * y);
        double dy = (c * x * y) - (d * y);
        int dt = 1;

        x = x + dx;
        y = y + dy;
        t = t + dt;
      }

      Console.WriteLine("\nEnd demo \n");
      Console.ReadLine();
    } // Main
  } // Program class
} // ns

This entry was posted in Machine Learning. Bookmark the permalink.