fcp2021 / test_infection_simulation / person.py
person.py
Raw
import math
import numpy as np

class Person:
    def __init__(self,i, posx, posy, objx, objy, v, t_infected, fixed):
        # movement speed
        self.v=v

        # target position
        self.objx=objx
        self.objy=objy

        #ID and name
        self.index=i
        self.number="Person"+str(i)

        #Initial State: Susceptible, Infected, retired or die
        self.infected = False
        self.suceptible = True
        self.retired = False
        self.die = False

        #Current position
        self.posx = posx
        self.posy = posy

        #is it fixed (in quarantine)?
        self.fixed = fixed

        # displacement per iteration
        if self.fixed:
            self.deltax = 0
            self.deltay = 0
        else:
            self.deltax = (self.objx - self.posx) / self.v
            self.deltay = (self.objy - self.posy) / self.v

        #time in which the person was infected
        self.i_contagious=-1
        
        #time that the infection lasts, recover time
        self.t_infected = t_infected


    def __str__(self):
        return self.number+" in position "+str(self.posx)+", "+str(self.posy)

    def infect(self,i):
        #infect
        self.infected=True
        self.suceptible=False
        self.retired = False
        self.die = False
        self.i_contagious=i

    def retire(self):
        #heal
        self.retired=True
        self.suceptible=False
        self.infected=False
        self.die = False

    def kill(self):
        #kill
        self.retired=False
        self.suceptible=False
        self.infected=False
        self.die=True


    def set_objetive(self,objx,objy):
        #this function is used to create a new target position
        self.objx=objx
        self.objy=objy
        if self.fixed:
            self.deltax = 0
            self.deltay=0
        else:
            self.deltax = (self.objx - self.posx) / self.v
            self.deltay = (self.objy - self.posy) / self.v
        print("New OBJ   ", self.objx,self.objy,"  ",self.index)

    def check_contagious(self,i):
        #this function is used to heal the person if the established infection time has passed
        if self.die == False:
            if self.i_contagious>-1:

                if i - self.i_contagious>self.t_infected:
                    self.retire()
        else:
            pass
          
    def check_death(self):
        #this function is used to fix the person's position if dead
        if self.die:
            self.fixed = True
        else:
            pass
    
    def fix_position(self):
        #this function is used to fix position
        if np.random.random()<0.7: 
            self.fixed = True
        else:
            pass

    


    def update_pos(self, n_posx, n_posy):
        #this funcion animates the movement
        if(n_posx==0 and n_posy==0):
            self.posx=self.posx+self.deltax
            self.posy=self.posy+self.deltay
        else:
            self.posx=n_posx
            self.posy=n_posy

        if abs(self.posx-self.objx)<3 and abs(self.posy-self.objy)<3: #if moves less than 3 units
            self.set_objetive(np.random.random()*100, np.random.random()*100) #randint from 0-100
        if self.posx>100:
            self.posx=100
        if self.posy>100:
            self.posy=100
        if self.posx<0:
            self.posx=0
        if self.posy<0:
            self.posy=0

    def get_color(self):
        if self.infected:
            return 'red'
        if self.suceptible:
            return 'blue'
        if self.retired:
            return 'gray'
        if self.die:
            return 'black'

    def get_pos(self):
        return (self.posx,self.posy)

    def get_dist(self,x,y):
        #this funcion calculates the distance between this person and another.
        return math.sqrt((self.posx-x)**2+(self.posy-y)**2)