You are on page 1of 3

import numpy as np

import pylab as pl

c1 = 1.0

c2 = .1

c3 = 1.5

c4 = 0.75

K = 200

c5 = 1.0

ND=MaxTime=10.0

X0=20

Y0=10

INPUT = np.array((X0,Y0))

timestep=0.0

def stoc_eqs(INP,ts):

V = INP

Rate=np.zeros((5))

Change=np.zeros((5,2))

N=np.sum(V[range(2)])

Rate[0] = c1*V[0]*(K-V[0]); Change[0,:]=([1, 0]);

Rate[1] = c2*V[0]*V[1]; Change[1,:]=([-1, 0]);

Rate[2] = c2*c4*V[0]*V[1]; Change[2,:]=([0, 1]);

Rate[3] = c3*V[1]; Change[3,:]=([0, -1]);

Rate[4] = c5*V[1]; Change[4,:]=([0 , 1]);

R1=pl.rand(); # ALEATORIOS
R2=pl.rand();

ts = -np.log(R2)/(np.sum(Rate)); #TIEMPO DE ESPERA

m=min(pl.find(pl.cumsum(Rate)>=R1*pl.sum(Rate)));

V[range(2)]=V[range(2)]+Change[m,:]

return [V,ts]

def Stoch_Iteration(INPUT):

lop=0

ts=0

T=[0]

P=[0]

D=[0]

while T[lop] < ND:

lop=lop+1

T.append(T[lop-1]+ts)

P.append(INPUT[0])

D.append(INPUT[1])

[res,ts] = stoc_eqs(INPUT,ts)

lop=lop+1

T.append(T[lop-1])

P.append(INPUT[0])

D.append(INPUT[1])

return [T,P,D]

[T,P,D]=Stoch_Iteration(INPUT)

tT=np.array(T)[1:,]

tP=np.array(P)[1:,]

tD=np.array(D)[1:,]
rabbits, foxes = tP,tD

f1 = pl.figure()

pl.plot(tT, rabbits, 'r-', label='Rabbits')

pl.plot(tT, foxes , 'b-', label='Foxes')

pl.grid()

pl.legend(loc='best')

pl.xlabel('time')

pl.ylabel('population')

pl.title('Evolution of fox and rabbit populations')

pl.axis('tight')

f1.savefig('gillespie_rabbits_and_foxes_1.png')

rabbits, foxes = tP,tD

f2 = pl.figure()

pl.plot(rabbits,foxes)

pl.plot(rabbits[0],foxes[0],'og')

pl.plot(rabbits[-1],foxes[-1],'or')

pl.grid()

pl.xlabel('Number of foxes')

pl.ylabel('Number of rabbits')

pl.title('Trajectories')

f2.savefig('gillespie_rabbits_and_foxes_2.png')

pl.show()

You might also like