MIT6_0002F16_ProblemSet4
实验内容:
本次实验旨在设计并实现一个细菌种群动态的随机模拟模型,并根据模拟结果得出各种治疗方案如何影响细菌的传播的结论。
实验流程:
阅读实验pdf,可知实验具体要求,按照要求一步一步解决。
Problem 1: Implementing a Simple Simulation
该部分为最简易的细菌模拟,不添加抗生素,简单模拟细菌的种群增长情况。
主要任务是填写 SimpleBacteria类和Patient类。SimpleBacteria 类表示单个细菌的状态。Patient维护与病人相关的细菌种群的状态。
代码如下:(根据提示较易完成)
class SimpleBacteria(object):
def __init__(self, birth_prob, death_prob):
#max出生概率
self.birth_prob=birth_prob
#max死亡概率
self.death_prob=death_prob
def is_killed(self):
#利用随机的办法判断是否死亡
return random.random() <= self.death_prob
def reproduce(self, pop_density):
#利用随机的办法判断是否将繁殖,繁殖可能性为self.birth_prob * (1 - pop_density).
if random.random() < (self.birth_prob * (1 - pop_density)):
return SimpleBacteria(self.birth_prob, self.death_prob)
else:
raise NoChildException
class Patient(object):
def __init__(self, bacteria, max_pop):
#病人身上的细菌
self.bacteria=bacteria
#最大的细菌种群大小
self.max_pop=max_pop
def get_total_pop(self):
#返回细菌数目
return len(self.bacteria)
def update(self):
#根据提示即可完成
#判断一下细菌是否死亡或者产生新的细菌
surviving_bacteria = []
for bacterium in self.bacteria:
if not bacterium.is_killed():
surviving_bacteria.append(bacterium)
#计算当前种群密度,用细菌数/菌落数
pop_density = len(surviving_bacteria) / self.max_pop
#判断存活细菌是否将产生新细菌
child_bacteria = []
for bacterium in surviving_bacteria:
try:
child_bacteria.append(bacterium.reproduce(pop_density))
except NoChildException:
pass
self.bacteria = surviving_bacteria + child_bacteria
return len(self.bacteria)