Anyone know how to use python to create classes, objects and methods? I working
ID: 3761202 • Letter: A
Question
Anyone know how to use python to create classes, objects and methods? I working on a comp sci project now and I'm so stuck.
Your initial implementation consists of three classes and their associated methods, described next.
class Disease():
Note: the signature of the constructor should read __init__(self, name=’influenza’, t=0.95, E=2, I=7, r=0.0).
Objects in this class describe a particular disease, like influenza, the measles, or rubella. The initialization parameters correspond to the disease name, its transmissivity t, the number of days in the exposed state E, the number of days in the infected state I, and the probability that recovery confers lifetime immunity r, respectively (these default values are reasonable values for modeling the flu).
class Agent():
Note: the signature of the constructor should read __init__(self, s=0.99).
Objects in the class model the individual agents in the simulation. The only initialization parameter is the intrinsic susceptibility of the agent to disease; a higher number may indicate a frail elderly person or someone with a compromised immune system (both highly susceptible) while a lower number may indicate a healthy young adult in peak physical condition. You’ll also need a few additional variables which are not set at initialization, like the agent’s vaccination state, v, and some sort of internal state indicating if the agent is sick or not.
This last requirement is easily met by keeping an internal state counter initially set to -1, indicating that the agent is initially in the susceptible state. When the agent is infected, the counter is set to I+E (parameters of the disease), and then decremented on each successive day of the simulation until it reaches 0, the recovered state. At this point, depending on the probability that lifetime immunity to the
disease is achieved upon recovery, the counter may be reset to -1, the susceptible state.1
Your agent class will also require a few methods. The first method, vaccinate(self, v) sets the agent’s internal v variable to indicate that some immunity to the disease has been instantly conferred by immunization. We’ll use this variable to model how quickly we can dampen how fast the disease spreads by starting to innoculate the herd: v close to 0 indicates the vaccine is very effective, while v close to 1 indicates very little protection. By default, v should be 1.0, indicating that no vaccination has taken place.
The second method, infect(self, other, disease) gives agent other the chance to infect agent self with disease. Of course, first you’ll need to check if other is infective, if self is susceptible, vaccinated, etc.
1 How can we model people who die from the disease? Even influenza can be fatal; indeed, there about 35,000 to 40,000 influenza deaths in the USA each year! Interestingly enough, in our simulation, a dead agent is no differ- ent from an agent with lifetime immunity; unlike a dead agent, a recovered but immune agent may hang around, but like a dead agent, they will never again make anyone else sick. This means we can factor the death rate into the per- manent immunity/recovery probability, r.
2 Revised November 3, 2015
before you ‘‘roll the die’’ to determine if the infection is transmitted. If successful, update self’s internal state counter as described above. The general idea here is that, if self is susceptible and other is either exposed or infected, we want to infect self with probability self . s * self . v * disease. t, and adjust self’s
internal state accordingly.2 Also, be careful not to interpret this method the other way around — since only one agent’s internal state is changed, it makes more self to represent that agent, and therefore the agent that is being infected. This better fits with the notion of encapsulation that lies at the heart of the object-oriented worldview.
The third method, state(self), returns True if self internal state corresponds to exposed or infected, and False otherwise.
The fourth method, update(self), is invoked on each agent every day, and is used to update self’s internal state indicating another day has elapsed, putting self one step closer to recovery if they are exposed or infected.
class Simulation():
Note: the signature of the constructor should read __init__(self, D=500).
The final class represents a simple simulation that runs at most D days of simulation (the simulation may terminate early if there are no longer any infected agents remaining). The class must obviously have some internal variables that contain, e.g., a list of instances of Agent that are in the simulation and the instance of Disease that is being simulated. Other variables may also be necessary, such as, for example, a mixing coefficient m that indicates the probability any one agent will encounter any other agent on any particular day. Interactions with instances of this class occur via the following methods.
The first method, join(self, agent) adds agent to the simulation.
The second method, introduce(self, disease) adds disease to the simulation.
The third method, run(self) starts the simulation. The simulation runs for at most self.D days, but may terminate early if there are no longer any infected agents. The value returned should be a list of tuples (Ne,Ni) corresponding to daily snapshots of the simulation, where Ne is the number of exposed agents and Ni is the number of infected agents on the corresponding day (the list returned will contain at most D tuples).
Explanation / Answer
Creating three calsses
1.class Disease():
2.class Agent():
3.class Simulation():
create a class for Disease
class Disease():
def __init__(self, name='influenza', t=.95, E=2, I=7, r=0.0): #Initialize the values.
self.name=name # name of the disease like influenza, the measles, or rubella.
self.t=t #transmissivity
self.E=E #number of days in the exposed state
self.I=I #number of days in the infected state
self.r=r #the probability that recovery confers lifetime immunity
create a class for Agent
class Agent():
def __init__(self, s=0.99): #Initialize the values
self.s=s # initialize intrinsic susceptibility of the agent
self.v=1.0 #vaccinate value
def vaccinate(self, vnew): #initialize method vaccinate
self.v=vnew#create a new value for vaccinate
def infect(self, other, disease): #initialize method infect
def state(self): #initialize method state
if other.state() == True:#set the value as true
self.recoverycounter=Disease.E + Disease.I #counter is set to paramaters of disease
return recoverycounter > -1 #the counter may be reset to -1
def update(self): #initialize method update
if self.state() == True: #set value as true
recoverycounter == recoverycounter - 1 #recovery counter
create a class for Simulation
class Simulation():
def __init__(self, D=500): #initialize the methods
self.D=D #days of simulation
def join(self, agent):
LOG.info('agent:%s',agent)
return self._run(agent)
def execute(self,Simulation)
return self._run(Simulation)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.