def setInfection(human, area, d): human.isInfected = True human.infectionNumber += 1 human.infectionDate = d human.isTested = False illness = rd.setIllnessDevelopment() human.incubationPeriod = illness illness += rd.setIllnessDevelopment() human.illnessPeriod = illness human.evolutionState = 0 #Defining if infected humans will be symptomatic or will need treatment auxRandom = rd.aRandom() symptomsThreshold = vr.getSymptomsThreshold( ) * human.deathRiskFactor * vr.getDeathRiskSymptomsW() treatmentThreshold = vr.getTreatmentThreshold( ) * human.deathRiskFactor * vr.getDeathRiskTreatmentW() reinfectionFactor = vr.getReinfectionFactor(human.infectionNumber) if auxRandom < symptomsThreshold * reinfectionFactor: human.willBeSymptomatic = True auxRandom = rd.aRandom() if auxRandom < treatmentThreshold * reinfectionFactor: human.willNeedTreatment = True Simulation.increaseV("totalInfected") Simulation.increaseV("actualInfected") if area == "A": Simulation.increaseV("actualAInfected") elif area == "B": Simulation.increaseV("actualBInfected")
def setHumanContacts(humans, density): contacts = [] quantity = rd.setContactsQuantity(gov.getIsolationFactor(), density) if quantity > 0: auxRandoms = rd.aRandomIntList(0, len(humans) - 1, int(quantity)) for i in range(len(auxRandoms)): contacts.append( Simulation.getHumanNumber(humans, auxRandoms[i])) return contacts
def decideInfection(infectedHuman, human, area, d): Simulation.checkImmunity(human, d, vr.getImmunityPeriod()) if human.hasImmunity == False: r = rd.aRandom() c = rd.isCarefulAverage(infectedHuman, human) * gov.getInfoFactor() s = rd.isDistancedAverage(infectedHuman, human) * gov.getSocialDistanceFactor() infectionP = r * rd.getInfectionThresholdVar(c, s) if infectionP < vr.getInfectionThreshold( ) * infectedHuman.contagiousFactor: Simulation.setInfection(human, area, d) infectedHuman.transmission += 1
def decideDeath(human): death = False d = rd.aRandom() d = d * human.deathRiskFactor * gov.treatmentColapseFactor( infectedPopulationRatio, "polynomial") if d < vr.getDeathThreshold(): death = True return death
def contactTracking(family, contacts, area): for f in range(len(family)): Simulation.lookForContact(family[f], area) for c in contacts: r = rd.aRandom() if r < gov.getActiveTrackingThreshold(infectedPopulationRatio): if c in humansInA: Simulation.lookForContact(c, "A") elif c in humansInB: Simulation.lookForContact(c, "B")
def humanExchange(areaAH, areaBH): if gov.getLockDown() == False: areaAExits = rd.setExchangeCount(gov.getExchangeFactor()) indexesA = rd.aRandomIntList(0, len(areaAH) - 1, areaAExits) humanNumbersA = [] for n in range(len(indexesA)): humanNumbersA.append( Simulation.getHumanNumber(areaAH, indexesA[n])) for a in range(areaAExits): family = areaAH[Simulation.getHumanIndex( areaAH, humanNumbersA[a])].family for f in range(len(family)): if Simulation.checkInTreatmentFamily(areaAH, family) == False: member = Simulation.getHumanIndex(areaAH, family[f]) humansInB.add(family[f]) areaBH.append(areaAH[member]) Simulation.correctStatistics("A", areaAH[member]) humansInA.discard(family[f]) del areaAH[member] areaBExits = rd.setExchangeCount(gov.getExchangeFactor()) indexesB = rd.aRandomIntList(0, len(areaBH) - 1, areaBExits) humanNumbersB = [] for n in range(len(indexesB)): humanNumbersB.append( Simulation.getHumanNumber(areaBH, indexesB[n])) for a in range(areaBExits): family = areaBH[Simulation.getHumanIndex( areaBH, humanNumbersB[a])].family for f in range(len(family)): if Simulation.checkInTreatmentFamily(areaBH, family) == False: member = Simulation.getHumanIndex(areaBH, family[f]) humansInA.add(family[f]) areaAH.append(areaBH[member]) Simulation.correctStatistics("B", areaBH[member]) humansInB.discard(family[f]) del areaBH[member]
def assignAreas(humans): #Assigning urban area print("Assigning urban areas... ", end="\r") p = 0 while p < len(humans): area = rd.aRandom() if area > 0.5: for f in range(len(humans[p].family)): areaAHumans.append(humans[p]) humansInA.add(humans[p].humanNumber) p += 1 else: for f in range(len(humans[p].family)): areaBHumans.append(humans[p]) humansInB.add(humans[p].humanNumber) p += 1 print("Assigning urban areas complete! ", end="\n")
def assignFamilies(humans): hcount = 0 actualFamily = 0 print("Assigning families... ", end="\r") while hcount < len(humans): members = rd.setFamilySize() for i in range(members): if i + hcount < len(humans): humans[i + hcount].familyNumber = actualFamily humans[hcount].family.append(i + hcount) if members > 1: for i in range(members - 1): if i + hcount + 1 < len(humans): humans[hcount + i + 1].family = humans[hcount].family hcount += members actualFamily += 1 print("Assigning families complete! ", end="\n")
def decideTest(notTestedHuman, area): r = rd.aRandom() tested = False if notTestedHuman.isSymptomatic == True: if r < gov.getTestingResponseThreshold(): tested = True elif notTestedHuman.isSymptomatic == False: if r < gov.getTestingResponseASThreshold(): tested = True if notTestedHuman.isInTreatment == True: #Probability of being tested in treatment is 1 tested = True if tested == True: Simulation.testHuman(notTestedHuman, area) if gov.getActiveTracking() == True: Simulation.contactTracking(notTestedHuman.family, notTestedHuman.contactsHistory, area)
def createHumans(population, casesCero, simulationName, autoIsolationThreshold, startingImmunity): #Humans initialization humans = [] auxRandom = 0 auxRandoms = [] for p in range(population): humans.append(hn(p)) if p % 100 == 0: print("Creating humans: " + str(p) + " ", end="\r") tm.sleep(0.0001) #Human configuration humans[p].age = rd.setAge() humans[p].sex = rd.setSex() humans[p].carefulFactor = rd.setCarefulFactor() humans[p].socialDistanceFactor = rd.setSocialDistanceFactor() humans[p].contagiousFactor = vr.getBaseASContagiousFactor() humans[p].deathRiskFactor = vr.deathRiskFactor( humans[p].age, "polynomial") #Defining if human will isolate himself in case of having symptoms auxRandom = rd.aRandom() if auxRandom <= autoIsolationThreshold: humans[p].autoIsolation = True auxRandom = rd.aRandom() if auxRandom <= startingImmunity: humans[p].hasImmunity = True Simulation.increaseV("actualImmunity") Simulation.assignFamilies(humans) Simulation.assignAreas(humans) print("Creating humans complete! ", end="\n")
def __init__(self, populationcount, periodindays, simNumber, casesCero, simulationName, bDensity, \ govActions, govActionsList, govFailureList, autoIsolationThreshold, startingImmunity, \ behaviorB, behaviorTrigger, behaviorOff, behaviorFactor): if simNumber > 1: #We need to clean some variables Simulation.deleteSimulation() global simulationStartTime simulationStartTime = tm.time( ) #To know how much time a simulation takes global population population = populationcount if simNumber == 1: #Some things will be the same in all iterations global period period = periodindays global simulationName0 simulationName0 = simulationName global areaBDensity areaBDensity = bDensity global govMode govMode = govActionsList[0] vr.startRiskVariables() gov.startColapseVariables() gov.setAutoActionsTrigger(govActionsList[1]) gov.setAutoActionsOff(govActionsList[2]) gov.setStartCaseCount(govActionsList[3]) gov.setActionsPeriod(govActionsList[4]) gov.setActiveIsolation(govActionsList[10]) gov.setActiveTracking(govActionsList[11]) gov.setActiveTrackingThreshold(govActionsList[12]) gov.setActiveTrackingPressureW(govActionsList[13]) gov.setTestingResponseThreshold(govActionsList[14]) gov.setTestingResponseASThreshold(govActionsList[15]) global govFailure govFailure = govFailureList[0] global govActionsFailurePeriod govActionsFailurePeriod = govFailureList[2] print("Saving starting point data...", end="\r") gov.saveSimulationConfig(simulationName) vr.saveSimulationConfig(simulationName) rd.saveSimulationConfig(simulationName) print("Starting point data saved! ", end="\n") #Now we create the population that will suffer the outbreak Simulation.createHumans(population, casesCero, simulationName, autoIsolationThreshold, startingImmunity) #We inject desire number of infected humans in area A print("Injecting infected humans in urban area A...", end="\r") auxRandoms = rd.aRandomIntList(0, len(areaAHumans) - 1, casesCero) for i in range(casesCero): Simulation.setInfection(areaAHumans[auxRandoms[i]], "A", 1) if areaAHumans[auxRandoms[i]].hasImmunity == True: areaAHumans[auxRandoms[i]].hasImmunity = False Simulation.decreaseV("actualImmunity") print("Infected humans injected! ", end="\n") dt.savePopulationData(areaAHumans, areaBHumans, simulationName) print("", end="\n") #Simulating each day, one by one... for d in range(period): print("Simulating day " + str(d + 1) + "... ", end="\r") Simulation.simulateDay(d + 1) Simulation.saveDay(d + 1) if behaviorB == True: Simulation.checkHumansBehavior(d, behaviorTrigger, behaviorOff, behaviorFactor) if govActions == True: Simulation.checkGovActions(d, govActionsList, govFailureList) if d < period: Simulation.humanExchange(areaAHumans, areaBHumans) print("Simulation Complete! ", end="\n") print("Infected: " + str(totalInfected) + ", Recovered: " + str(totalRecovered) + ", Deaths: " + str(totalDeaths), end="\n") print("Time needed for this simulation: " + Simulation.getSimulationTime(simulationStartTime, tm.time()), end="\n") print("", end="\n") print("Saving simulation's data...", end="\r") evolutionData.to_csv("SimulationData/Simulations/" + simulationName + ".csv", index=False) virusData.to_csv("SimulationData/Infections/" + simulationName + "_infections.csv", index=False) Simulation.saveSimulationConfig(simulationName0, simNumber, casesCero, govActionsCycles, \ govActions, behaviorB, behaviorCycles) print("Simulation's data saved! ", end="\n")