[Tutor] Lotka-Volterra Model Simulation Questions

Wayne Werner wayne at waynewerner.com
Fri Sep 28 23:06:42 CEST 2012

On Fri, 28 Sep 2012, Jim Apto wrote:

> Hello folks,
> I'm relatively new to python, and was asked to program a lotka-volterra model (predator and prey
> relation) simulator.  The program basically will basically have a menu that takes user input, collect
> data, and then create a graph.  Currently i've been working on the simulator section; I can't seem to
> get the lists right.  I've assigned the following variables and parameters to the model for the program:

I don't know anything about the Lotka-Volterra model, but I've definitely 
got some recommendation

> x represents prey population
> y represents predator population
> dy/dt and dx/dt represents growth rate of the two populations over time
> t represents time
> a is the growth rate of prey
> b is the rate at which predators kill prey
> g is the death rate of predators
> d is the rate at which the predators population increases by consuming prey
> The equation:
> dx/dt = x(a-by)
> dy/dt = -y(g-dx)
> The code I have for this section is:
> def deltaX(a,b,x,y):
>     dx = x*(a-b*y)
> def deltaY(g,d,x,y):
>     dy = -y*(g-d*x)
> The simulation function is where I am having trouble.
> For the simulation function, I need to ask the user for the number of runs and then save it in a
> variable, create a list for prey and predator.  For each run, i need to calculate the increment of
> change in prey and predator populations by calling the deltaX and deltaY functions, then save these in a
> variable, and then update the population information.  The newly calculated populations then need to be
> added to the existing lists.  After this is completed, a function for the graph is called.
> The following is my current simulation function:
> def simulation():
>     a=eval(input("Growth rate of prey:"))
>     b=eval(input("Rate at which predators eat prey:"))
>     g=eval(input("Death rate of predators:"))
>     d=eval(input("Rate at which predators increase by consuming prey:"))
>     x=eval(input("Current prey population:"))
>     y=eval(input("Current predator population:"))

Woah! Stop that right now - eval is an *incredibly* dangerous function. If 
you want to convert these numbers to integer or float, there's the int() 
and float() function. Additionally, single letter variables are really 
horrible. You could do:

prey_growth_rate = float(input("Growth rate of prey: "))
predator_consumption_rate = #your code here
predator_death_rate = ...
predator_growth_rate = ...
initial_prey_population = ...
initial_predator_population = ...

> deltaX(a,b,x,y)
> deltaY(g,d,x,y)

I don't see where you defined deltaX or deltaY...

> n=eval(input("Number of runs:")
>     r = 0
>     count=0
>     yList = [0]
>     while r <= n:
>         r = r + 1
>         count = count + 1
>         yList.append(dx + dx)
>     zList= [0]
>        while r <= n:
>        r = r + 1
>        count = count +1
>        zList.append(dy + dy)
> It seems terribly wrong.  The following is my graph function:
> def drawCurve(yList,zList,n):
>     x = pylab.arange(n)
>     pylab.title("Foxes and Rabbits")
>     pylab.ylabel("Number of predator (Foxes)")
>     pylab.xlabel("\nNumber of prey  (Rabbits)")
>     pylab.plot(x, yList, 'b') 
>     pylab.plot(x, zList, 'r')
>     pylab.legend(('Rabbits','Foxes'),loc='upper left')
>     pylab.show()
> The issue i'm having is the logic in the lists.  How can I create the simulation function using lists
> and make it perform the expected task of creating a graph?  I can't seem to get the logic right.

Posting an image of what you expect, and what you got instead to imgur or 
some other free hosting site would be a good thing to do.

When asking a question you should always post what you wanted to happen, 
and what happened instead.


More information about the Tutor mailing list