MIT6_0002F16_ProblemSet4

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)

Problem 2: Running and Analyzing a Simple Simulation (No

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值