Tutorial_ABM/main.py

99 lines
3.5 KiB
Python
Raw Normal View History

2022-07-22 12:05:57 +02:00
import csv
import math
import random
import matplotlib.pyplot as plt
class Mensch(object):
def __init__(self):
#toImplement
def getCoordinates(self, bdl):
bdlDict = {'Vorarlberg': [0, 0], 'Tirol': [1, 0], 'Kärnten': [2, 0], 'Salzburg': [0, 1],
'Steiermark': [1, 1], 'Burgenland': [2, 1], 'Oberösterreich': [0, 2],
'Niederösterreich': [1, 2], 'Wien': [2, 2]}
return [int(bdlDict[bdl][0]) + random.random(), int(bdlDict[bdl][1]) + random.random()]
def die(self):
#toImplement
def infect(self, day):
#toImplement
#Distanz = math.sqrt((self.Koordinaten[0] - Population[randIndex].Koordinaten[0]) * (self.Koordinaten[0] - Population[randIndex].Koordinaten[0]) + (self.Koordinaten[1] - Population[randIndex].Koordinaten[1]) * (self.Koordinaten[1] - Population[randIndex].Koordinaten[1]))
class Simulation(object):
def __init__(self, population):
self.Population = population
def simulate(self):
#toImplement
# Visualisierung
# Scatter Plot
x_inf = []
y_inf = []
x_fit = []
y_fit = []
for human in Population:
if (human.Infiziert):
x_inf.append(human.Koordinaten[0])
y_inf.append(human.Koordinaten[1])
else:
x_fit.append(human.Koordinaten[0])
y_fit.append(human.Koordinaten[1])
plt.title("Population")
plt.scatter(x_inf, y_inf, c="red", s=5)
plt.scatter(x_fit, y_fit, c="blue", s=2)
plt.draw()
if (i % 5 == 0):
plt.savefig('result/populationOnDay' + str(i) + '.png')
plt.pause(0.0001)
plt.clf()
if __name__ == '__main__':
print("Generate Population:")
Populationsfaktor = 0.01
AnteilFrauen = 0.507
Population = []
bevAnzahl = 0
bevDict = {}
gebDict = {}
gestDict = {}
#read data from Statistik Austria
with open('./data/Bevölkerung.csv') as bevoelkerung:
csv_reader = csv.reader(bevoelkerung, delimiter = ';')
rowcnt = 0
for row in csv_reader:
rowcnt = rowcnt + 1
if rowcnt == 2:
bevAnzahl = int(int(row[-1])*Populationsfaktor)
if rowcnt > 2:
bevDict[row[0]] = row[-1]
with open('./data/Geburten2021.csv') as geburten:
csv_reader = csv.reader(geburten, delimiter=';')
rowcnt = 0
for row in csv_reader:
rowcnt = rowcnt + 1
if rowcnt > 2:
gebDict[row[0]] = [row[-2], row[-1]]
with open('./data/Gestorbene2021.csv') as gestorbene:
csv_reader = csv.reader(gestorbene, delimiter=';')
rowcnt = 0
for row in csv_reader:
rowcnt = rowcnt + 1
if rowcnt > 2:
gestDict[row[0]] = [row[-2], row[-1]]
print(bevDict)
print(gebDict)
print(gestDict)
print(bevAnzahl)
print(random.random())
for i in range(int(bevAnzahl)):
gender = 1
if(random.random()>0.507):
gender = 0
age = random.randint(0, 101)
bdl = ""
randBdl = random.random()
BdlCount = 0
for key in bevDict.keys():
BdlCount += int(bevDict[key])
checkWlsk = (BdlCount*Populationsfaktor)/int(bevAnzahl)
if(randBdl < checkWlsk):
bdl = str(key)
break
Population.append(Mensch(age, gender, bdl))
Simulation = Simulation(Population)
Simulation.simulate()