# Enter your definitions for the SimpleVirus and Patient classes in this box. 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. """ return random.random() > self.clearProb 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.maxBirthProb * (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. """ repoProbability = self.maxBirthProb * (1 - popDensity) if(random.random() > repoProbability): raise NoChildException('In reproduce()') return SimpleVirus(self.maxBirthProb, self.clearProb) 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. """ return self.maxPop 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) """ self.viruses[virus for virus in self.viruses if not virus.doesClear()] virusPopDensity = len(self.viruses) / float(self.maxPop) for virus in self.viruses: try: self.viruses.append(v.reproduce(virusPopDensity) except NoChildException: pass return len(self.viruses) # Problem 3: simulationWithoutDrug def simulationWithoutDrug(numViruses, maxPop, maxBirthProb, clearProb, numTrials): steps = 300 trialResults = [[] for s in range(steps)] for __ in range(numTrials): viruses = [SimpleVirus(maxBirthProb, clearProb) for v in range(numViruses)] patient = Patient(viruses, maxPop) for step in range(steps): trialResults[step].append(patient.update()) resultsSummary = [sum(l) / float(numTrials) for l in trialResults] pylab.plot(resultsSummary, label="Total Virus Population") pylab.title("SimpleVirus simulation") pylab.xlabel("Time Steps") pylab.ylabel("Average Virus Population") pylab.legend() pylab.show() simulationWithoutDrug(100, 1000, 0.1, 0.05, 100)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question