[Tutor] Lotka-Volterra Model Simulation Questions

Prasad, Ramit ramit.prasad at jpmorgan.com
Fri Sep 28 23:06:20 CEST 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:
> 
> 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:
> 

You posted as HTML/rich text while I recommend posting as plain text.
HTML/rich text can cause the text spacing to be wrong. My comments may 
not apply because of incorrect indention levels.
 
> def simulation():
>     a=eval(input("Growth rate of prey:"))


Usage of eval is dangerous and not recommended except for advanced
users. For your purposes, you can replace eval() with either int() 
or float() as appropriate.

>     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:"))

     return a,b,g,d,x,y # need to return the data from simulation()
Where do you ever call simulation?

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

Simulation does not return anything so this should either cause a NameError
or use the incorrect values.

> 
> n=eval(input("Number of runs:")
>     r = 0

Why did this change indentation levels?

>     count=0
>     yList = [0]
>     while r <= n:
>         r = r + 1
>         count = count + 1
>         yList.append(dx + dx)

What is the point of appending dx*2? What is the point of the loop 
appending the same things over and over? I suspect
simulation should be called in between



Not to mention that, this loop can be simplified by changing it to 
a for loop.

for _ in xrange(n):
    yList.append(dx + dx)

Or even converted into a list comprehension.

yList.extend( [ dx + dx for _ in xrange(n) ] )
count += len(yList - 1) # -1 for the original [0]

Shouldn't simulation be called or at least change the values of 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.

The lack of complete code (or issues with indentation) make it
difficult to give you advice on how to solve it. If you 
can post your code in plain text (or attach it) you will get 
better help. It would also help if you can create a small
sample that has the minimum amount of (functioning even
if logically incorrect) code that displays the problem you are 
facing.



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Tutor mailing list