Benutzer-Werkzeuge

Webseiten-Werkzeuge


Seitenleiste

ws2021:klasse_world
from Agent import Agent
import random
 
days = 5 # one day equals to 5 steps
 
class World():
 
 
    def __init__(self):
        '''
        set attributes for the world and the simulation
        '''
        self.number_of_agents = 300
        self.steps = 500
        self.height = 100
        self.width = 100
        self.temp = 0.1
        self.agents_list = []
        # numbers at the beginning of each condition
        self.number_susceptible = self.number_of_agents-1
        self.number_exposed = 1
        self.number_infected = 1
        self.number_recovered = 0
        # lists for graphics
        self.susceptible_series = [self.number_of_agents-1]
        self.exposed_series = [1]
        self.infected_series = [1]
        self.recovered_series = [0]
 
        # list for xposition and yposition for the simulation
        self.x = []
        self.y = []
 
 
    def populate(self):
        '''
        create a list of Agents to populate the world
        '''
        self.agents_list = [Agent(color = 'red')] # first infected Agent
        self.agents_list[0].day = 5
        for i in range(1,self.number_of_agents):
            self.agents_list.append(Agent()) # all the other Agents are susceptible
 
 
    def checkForCollision(self,agent1, agent2):
        '''
        check the collision between two Agents
        '''
        if abs(agent1.xpos-agent2.xpos) <= agent1.radius and abs(agent1.ypos-agent2.ypos) <= agent1.radius:
            return True
        else:
            return False
 
    def changeCondition1(self,agent1, agent2, probability = 1): # probability of agents getting infected
        '''
        change condition of one agent in case of a collision with the infected Agent
        '''
        if self.checkForCollision(agent1, agent2):
            # check if one of the Agents is infected and the other one susceptible, in that case susceptible Agent gets exposed
            if agent1.isSusceptible() and agent2.isInfected() and random.random() < probability:
                agent1.setExposed()
                self.number_exposed += 1
                self.number_susceptible -= 1
            elif agent1.isInfected() and agent2.isSusceptible() and random.random() < probability:
                agent2.setExposed()
                self.number_exposed += 1
                self.number_susceptible -= 1
 
    def changeCondition2(self,agent, quarantine = 0): # quarantine = probability of infected Agents going into quarantine
        '''
        change condition of an agent after certain amount of days
        '''
        if agent.day >= 1 and agent.day < 4*days:
            # increase the amount of days, if the agent is exposed
            agent.day += 1
        elif agent.day == 4*days:
            # change the condition to Infected if the agent has been exposed for 4 days
            agent.setInfected()
            self.number_infected += 1
            self.number_exposed -= 1
            if random.random() < quarantine:
                # move the agent to the quarantine and change its speed to zero
                agent.xpos = random.uniform(self.width+2*Agent.radius +10,self.width+30)
                agent.ypos = random.uniform(0,(self.height+Agent.radius)/2-Agent.radius)
                agent.xspeed = 0
                agent.yspeed = 0
        elif agent.day > 4*days and agent.day < 14*days:
            # increase the amount of days, if the agent is infected
            agent.day += 1
        elif agent.day == 14*days:
            # change the condition to Recovered if the agent has been infected for 14 days
            agent.setRecovered()
            self.number_recovered += 1
            self.number_infected -= 1
        elif agent.day > 14*days and agent.day < 18*days:
            # increase the amount of days, if the agent is in quarantine
            agent.day += 1
        elif agent.day == 18*days:
            # move the agent who is in quarantine back to the main square after 14 days of being infected (18 days after the collision with infected)
            # set speed
            agent.xpos = random.uniform(0,self.width)
            agent.ypos = random.uniform(0,self.height)
            agent.xspeed = self.temp*random.uniform(-10,10)
            agent.yspeed = self.temp*random.uniform(-10,10)
            agent.day += 1
 
    def updateLists(self):
        '''
        update lists for graphs (adds number of infected, susceptible, recovered and exposed agents to the corresponding list)
        '''
        self.infected_series.append(self.number_infected)
        self.susceptible_series.append(self.number_susceptible)
        self.recovered_series.append(self.number_recovered)
        self.exposed_series.append(self.number_exposed)
 
    def savePositions(self):
        '''
        save the starting positions of all the agents in the agents_list
        '''
        self.x = [self.agents_list[i].xpos for i in range(self.number_of_agents)]
        self.y = [self.agents_list[i].ypos for i in range(self.number_of_agents)]
 
    def updatePostitions(self,agent):
        '''
        update positions of the Agent
        '''
        self.x[agent] = self.agents_list[agent].xpos
        self.y[agent] = self.agents_list[agent].ypos
 
    def lessMovement(self, n = 2):
        '''
        set speed of every n-th Agent to 0
        '''
        for i in range(0,self.number_of_agents,n):
            self.agents_list[i].xspeed = 0
            self.agents_list[i].yspeed = 0
ws2021/klasse_world.txt · Zuletzt geändert: 2021/04/13 11:25 von n.gujejiani