Last active
October 13, 2015 14:18
-
-
Save saltavenger/4208639 to your computer and use it in GitHub Desktop.
MIT viruses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SimpleVirus(object): | |
""" | |
Representation of a simple virus (does not model drug effects/resistance). | |
""" | |
def __init__(self, maxBirthProb, clearProb): | |
""" | |
Initialize a SimpleVirus instance, saves all parameters as attributes | |
of the instance. | |
maxBirthProb: Maximum reproduction probability (a float between 0-1) | |
clearProb: Maximum clearance probability (a float between 0-1). | |
""" | |
self.maxBirthProb = maxBirthProb | |
self.clearProb = clearProb | |
def getMaxBirthProb(self): | |
""" | |
Returns the max birth probability. | |
""" | |
return self.maxBirthProb | |
def getClearProb(self): | |
""" | |
Returns the clear probability. | |
""" | |
return self.clearProb | |
def doesClear(self): | |
""" Stochastically determines whether this virus particle is cleared from the | |
patient's body at a time step. | |
returns: True with probability self.getClearProb and otherwise returns | |
False. | |
""" | |
if random.random() <= self.getClearProb(): | |
return True | |
else: | |
return False | |
def reproduce(self, popDensity): | |
""" | |
Stochastically determines whether this virus particle reproduces at a | |
time step. Called by the update() method in the Patient and | |
TreatedPatient classes. The virus particle reproduces with probability | |
self.getMaxBirthProb * (1 - popDensity). | |
If this virus particle reproduces, then reproduce() creates and returns | |
the instance of the offspring SimpleVirus (which has the same | |
maxBirthProb and clearProb values as its parent). | |
popDensity: the population density (a float), defined as the current | |
virus population divided by the maximum population. | |
returns: a new instance of the SimpleVirus class representing the | |
offspring of this virus particle. The child should have the same | |
maxBirthProb and clearProb values as this virus. Raises a | |
NoChildException if this virus particle does not reproduce. | |
""" | |
if random.random() <= self.getMaxBirthProb() * (1-popDensity): | |
return SimpleVirus(self.getMaxBirthProb(), self.getClearProb()) | |
else: | |
raise NoChildException() | |
class Patient(object): | |
""" | |
Representation of a simplified patient. The patient does not take any drugs | |
and his/her virus populations have no drug resistance. | |
""" | |
def __init__(self, viruses, maxPop): | |
""" | |
Initialization function, saves the viruses and maxPop parameters as | |
attributes. | |
viruses: the list representing the virus population (a list of | |
SimpleVirus instances) | |
maxPop: the maximum virus population for this patient (an integer) | |
""" | |
self.viruses = viruses | |
self.maxPop = maxPop | |
def getViruses(self): | |
""" | |
Returns the viruses in this Patient. | |
""" | |
return self.viruses | |
def getMaxPop(self): | |
""" | |
Returns the max population. | |
""" | |
self.getMaxPop | |
def getTotalPop(self): | |
""" | |
Gets the size of the current total virus population. | |
returns: The total virus population (an integer) | |
""" | |
return len(self.viruses) | |
def update(self): | |
""" | |
Update the state of the virus population in this patient for a single | |
time step. update() should execute the following steps in this order: | |
- Determine whether each virus particle survives and updates the list | |
of virus particles accordingly. | |
- The current population density is calculated. This population density | |
value is used until the next call to update() | |
- Based on this value of population density, determine whether each | |
virus particle should reproduce and add offspring virus particles to | |
the list of viruses in this patient. | |
returns: The total virus population at the end of the update (an | |
integer) | |
""" | |
survived = [] | |
for virus in self.viruses: | |
if not virus.doesClear(): | |
survived.append(virus) | |
popDensity = self.getTotalPop() / float(self.maxPop) | |
try: | |
survived.append(virus.reproduce(popDensity)) | |
except NoChildException: pass | |
self.viruses = survived | |
return self.getTotalPop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment