Hare:
Lynx:
def Test_Initialization(init_func, hare_number, lynx_number) :
global hare_total, lynx_total
init_func(hare_number, lynx_number)
assert (hare_total == hare_number and lynx_total == lynx_number) , "Mismatch initial population size"
print("Pass initalization test:", init_func.__name__, hare_number, lynx_number)
from numpy import random
hare_id = 1
hare_total = 0
hare = []
lynx_id = -1
lynx_total = 0
lynx = []
random_generator = random.RandomState(1111)
param = {
"hare_birth": 0.8,
"hare_death" : 0.3, # come into contact with lynx AND die
"lynx_birth": 0.1,
"lynx_death": 0.2
}
This sets up the simulation with initial numbers of hares and lynxes.
def Initialize(hare_number=0, lynx_number=0):
global hare_total, lynx_total, hare_id, lynx_id, hare, lynx
hare_id = 0
hare = []
lynx_id = 0
lynx = []
# ask for inputs if not given
if hare_number >= 0:
hare_total = hare_number
else :
hare_total = int(input("Initial number of hares: "))
if lynx_number > 0:
lynx_total = lynx_number
else:
lynx_total = int(input("Initial number of lynxes: "))
# add individuals to approriate lists
for i in range(hare_total) :
hare_id += 1
hare.append( hare_id )
for i in range(lynx_total) :
lynx_id -= 1
lynx.append( lynx_id )
# Test_Initialization(Initialize, 10, 9)
This will do the following:
def Modify_Hare(amount_change):
global hare_total, hare_id, hare, random_generator
# if amount_change > 0 :
# for i in range(amount_change) :
# hare_id += 1
# hare.append( hare_id )
# elif amount_change + hare_total <= 0 :
# hare = []
# else :
# new_hare = []
# indices = sorted(random_generator.choice(hare_total, abs(amount_change), replace=False))
# i, j = 0, 0
# while j < hare_total :
# # done removing
# if i == abs(amount_change) :
# new_hare.append(hare[j:])
# break
# elif j != indices[i] :
# new_hare.append(hare[j])
# else :
# i += 1
# j += 1
# hare = new_hare[:]
hare_total = hare_total + amount_change if hare_total + amount_change > 0 else 0
return hare_total
def Modify_Lynx(amount_change):
global lynx_total, lynx_id, lynx, random_generator
# if amount_change > 0 :
# for i in range(amount_change) :
# lynx_id += 1
# # lynx.append( lynx_id )
# elif amount_change + lynx_total <= 0 :
# lynx = []
# else :
# new_lynx = []
# indices = sorted(random_generator.choice(lynx_total, abs(amount_change), replace=False))
# i, j = 0, 0
# while j < lynx_total :
# # done removing
# if i == abs(amount_change) :
# new_lynx.append(lynx[j:])
# break
# elif j != indices[i] :
# new_lynx.append(lynx[j])
# else :
# i += 1
# j += 1
# lynx = new_lynx[:]
lynx_total = lynx_total + amount_change if lynx_total + amount_change > 0 else 0
return lynx_total
def Simulate_One_Step():
global param
# calculate births, deaths, changes
hare_in = param["hare_birth"] * hare_total
hare_out = param["hare_death"] * hare_total * lynx_total
lynx_in = param["lynx_birth"] * hare_total * lynx_total
lynx_out = param["lynx_death"] * lynx_total
hare_change = int(hare_in - hare_out)
lynx_change = int(lynx_in - lynx_out)
# note: one can condensed all calculations above to just 2 lines
# make change to hare population
if hare_change != 0 :
Modify_Hare(hare_change)
# make change to lynx population
if lynx_change != 0 :
Modify_Lynx(lynx_change)
# param = {
# "hare_birth": 0.6,
# "hare_death" : 0.001,
# "lynx_birth": 0.00001,
# "lynx_death": 0.5
# }
# Initialize(50000, 600) # equilibrium
def Print_Day(day) :
global hare, lynx, hare_total, lynx_total
print("---day", str(day), "---")
# print(hare)
# print(lynx)
print("Hare total:", hare_total, " Lynx total:", lynx_total)
param = {
"hare_birth": 0.2,
"hare_death" : 0.00003,
"lynx_birth": 0.1,
"lynx_death": 1
}
Initialize(5000, 6)
# print(hare)
# print(lynx)
for day in range(0,101):
if hare_total == 0 or lynx_total == 0:
print("---day", str(day), "---")
print("Oops! One of the population has died out....")
print("Hare total:", hare_total, " Lynx total:", lynx_total)
break
Simulate_One_Step()
if day % 10 == 0 :
Print_Day(day)
# print(hare)
# print(lynx)
---day 0 --- Hare total: 5999 Lynx total: 300 ---day 4 --- Oops! One of the population has died out.... Hare total: 0 Lynx total: 60633847