具有可变繁殖力的斐波那契凡人兔
Fibonacci mortal rabbits with variable fecundity
我正在尝试修改 Fibonacci 凡人兔子的 python 代码,以便根据它们的年龄改变兔子的繁殖力。
举个例子吧。
我的兔子3个月后成熟,6个月后死亡。在它们 4 个月的生育期中,它们会根据年龄产生不同数量的后代。 3个月大时生2对小兔,4个月大时生3对小兔,以此类推直到第6个月。每对兔子由一雌一雄组成。最后我会计算对的数量而不是个人的数量。
从出生到死亡的生育力值:
fecundity = [0, 0, 2, 3, 3, 1]
我使用的 python 代码 (https://github.com/jschendel/Rosalind/blob/master/011_FIBD.py) 是:
n = 12
m = 6
#n = months to run
#m = how many months the rabbits live
# Populate the initial rabbits.
Rabbits = [1]+[0]*(m-1)
# Calculate the new rabbits (bunnies), in a given month.
# Start at use range(1,n) since our initial population is 0 month old.
for month in range(1, n):
Bunnies = 0
# Get the number of Rabbits able to old enough to give birth.
for j in range(1,m):
Bunnies += Rabbits[(month-j-1)%m]
# Bunnies replace the old rabbits who died.
Rabbits[(month)%m] = Bunnies
# Total rabbits is the sum of the living rabbits.
Total_Rabbits = sum(Rabbits)
我不确定如何实现繁殖力的变化。感谢您的帮助!
谢谢,
瓦伦蒂娜
定义你的生育力数组在一只兔子死亡时停止:
fecundity = [0, 0, 2, 3, 3, 1]
表示您的兔子在 7 个月大时死亡。
之后,你只要写一个递归函数来计算特定步骤中new_borns的个数。我将步骤初始化为 1 对用于步骤 0,0 对用于步骤 < 0。您当然可以更改它以适合您的情况。 (我所说的一步是一个时间单位,这里是月份)。
这是函数:
def new_borns(step):
if step < 0:
return 0
if step == 0:
return 1
nb_newborns = 0
# We create a loop on living pairs
for old_step in range(1, len(fecondity) +1):
nb_newborns += (fecundity[old_step -1]) * new_borns(step - old_step)
return nb_newborns
特定步骤的总人口是前面步骤的 new_borns 的总和,仍然活着(即,对于您的繁殖力数组的长度)。
def FIBD(step):
population = 0
for i in range(len(fecundity)):
population += new_borns(step - i)
return population
要知道你在第7步有多少对,只需调用FIBD(7)
兔子可以活的月数就是繁殖力数组的长度。
当然,这个递归函数非常非常慢而且很糟糕。您需要一个缓存系统来避免多次计算同一步骤。这是要写入的完整文件。
#!/usr/bin/env python
fecundity = [0, 0, 2, 3, 3, 1]
new_borns_cache = [1]
def new_borns(step):
if step < 0:
return 0
try :
return new_borns_cache[step]
except IndexError:
if step == 0:
return 1
sum = 0
for old_step in range(1, len(fecundity) +1):
sum += (fecundity[old_step -1]) * new_borns(step - old_step)
return sum
def fibd(step):
population = 0
for i in range(len(fecundity)):
population += new_borns(step - i)
return population
要使用它,只需导入 ,然后调用 fibd(7)
我自己得出答案,我真的修改了我之前发布的代码。我觉得现在简单多了
import numpy as np
m = 15
n = 18
fecundity = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 1, 2, 1, 1, 1])
Bunnies = np.array([0]*m)
Rabbits = np.array([1]+[0]*(m-1))
for month in range(0, 18):
# every month I shift the list of 1 since they're getting older
Rabbits = np.roll(Rabbits, 1)
# I set the newborns as 0
Rabbits[0] = 0
# I calculate the newborns
Bunnies = Rabbits * fecundity
# and then I assign them to the rabbits 0 month old
Rabbits[0] = sum(Bunnies)
# the sum of Rabbits is the number of final pairs of Rabbits after n months
Total_Rabbits = sum(Rabbits)
# 26 Rabbits
我正在尝试修改 Fibonacci 凡人兔子的 python 代码,以便根据它们的年龄改变兔子的繁殖力。 举个例子吧。
我的兔子3个月后成熟,6个月后死亡。在它们 4 个月的生育期中,它们会根据年龄产生不同数量的后代。 3个月大时生2对小兔,4个月大时生3对小兔,以此类推直到第6个月。每对兔子由一雌一雄组成。最后我会计算对的数量而不是个人的数量。 从出生到死亡的生育力值:
fecundity = [0, 0, 2, 3, 3, 1]
我使用的 python 代码 (https://github.com/jschendel/Rosalind/blob/master/011_FIBD.py) 是:
n = 12
m = 6
#n = months to run
#m = how many months the rabbits live
# Populate the initial rabbits.
Rabbits = [1]+[0]*(m-1)
# Calculate the new rabbits (bunnies), in a given month.
# Start at use range(1,n) since our initial population is 0 month old.
for month in range(1, n):
Bunnies = 0
# Get the number of Rabbits able to old enough to give birth.
for j in range(1,m):
Bunnies += Rabbits[(month-j-1)%m]
# Bunnies replace the old rabbits who died.
Rabbits[(month)%m] = Bunnies
# Total rabbits is the sum of the living rabbits.
Total_Rabbits = sum(Rabbits)
我不确定如何实现繁殖力的变化。感谢您的帮助!
谢谢, 瓦伦蒂娜
定义你的生育力数组在一只兔子死亡时停止:
fecundity = [0, 0, 2, 3, 3, 1]
表示您的兔子在 7 个月大时死亡。 之后,你只要写一个递归函数来计算特定步骤中new_borns的个数。我将步骤初始化为 1 对用于步骤 0,0 对用于步骤 < 0。您当然可以更改它以适合您的情况。 (我所说的一步是一个时间单位,这里是月份)。 这是函数:
def new_borns(step):
if step < 0:
return 0
if step == 0:
return 1
nb_newborns = 0
# We create a loop on living pairs
for old_step in range(1, len(fecondity) +1):
nb_newborns += (fecundity[old_step -1]) * new_borns(step - old_step)
return nb_newborns
特定步骤的总人口是前面步骤的 new_borns 的总和,仍然活着(即,对于您的繁殖力数组的长度)。
def FIBD(step):
population = 0
for i in range(len(fecundity)):
population += new_borns(step - i)
return population
要知道你在第7步有多少对,只需调用FIBD(7)
兔子可以活的月数就是繁殖力数组的长度。
当然,这个递归函数非常非常慢而且很糟糕。您需要一个缓存系统来避免多次计算同一步骤。这是要写入的完整文件。
#!/usr/bin/env python
fecundity = [0, 0, 2, 3, 3, 1]
new_borns_cache = [1]
def new_borns(step):
if step < 0:
return 0
try :
return new_borns_cache[step]
except IndexError:
if step == 0:
return 1
sum = 0
for old_step in range(1, len(fecundity) +1):
sum += (fecundity[old_step -1]) * new_borns(step - old_step)
return sum
def fibd(step):
population = 0
for i in range(len(fecundity)):
population += new_borns(step - i)
return population
要使用它,只需导入 ,然后调用 fibd(7)
我自己得出答案,我真的修改了我之前发布的代码。我觉得现在简单多了
import numpy as np
m = 15
n = 18
fecundity = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 1, 2, 1, 1, 1])
Bunnies = np.array([0]*m)
Rabbits = np.array([1]+[0]*(m-1))
for month in range(0, 18):
# every month I shift the list of 1 since they're getting older
Rabbits = np.roll(Rabbits, 1)
# I set the newborns as 0
Rabbits[0] = 0
# I calculate the newborns
Bunnies = Rabbits * fecundity
# and then I assign them to the rabbits 0 month old
Rabbits[0] = sum(Bunnies)
# the sum of Rabbits is the number of final pairs of Rabbits after n months
Total_Rabbits = sum(Rabbits)
# 26 Rabbits