-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
128 lines (109 loc) · 3.58 KB
/
Copy pathmain.py
File metadata and controls
128 lines (109 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import sys
import os
import yaml
import argparse
# adds the root of the git dir to the import path
# FIXME: Directory shenanigans
root_dir = os.getcwd()
sys.path.append(root_dir)
import lib.Map.Map as mmap
from lib.Renderer.Controller import Controller
from lib.Renderer.Controller import View
from lib.Simulation.Simulator import Simulator
import random
configFileName = "config.yml"
requiredConfigs = [
"OSMfile",
"jobsFile",
"businessFile",
"numberOfAgents",
"buildingConfigPath",
"threadNumber",
"infectedAgent",
"vaccinationPercentage",
"windowWidth",
"windowHeight",
"reportDir",
"reportInterval",
]
optionalConfig = [
"buildConnFile",
"pathfindFileName",
]
def read_validate_config(file_path):
config = None
with open(file_path, "r") as f:
config = yaml.safe_load(f)
err = False
errMessage = "Missing required attributes in config file: "
for c in requiredConfigs:
if c not in config:
err = True
errMessage += c + " "
if err:
raise NameError(errMessage)
for c in optionalConfig:
if c not in config:
config[c] = None
return config
def parseArgs():
global configFileName
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--config_file", help="sets the config file")
parser.add_argument("-s", "--seed", type=int, help="sets the seed")
parser.add_argument("--no_infectious_stop", action="store_true", help="Interromps the execution if infection is no longer possible")
parser.add_argument("-nr", "--no_render", action="store_true", help="Execute the program without render" )
args = parser.parse_args()
if args.config_file:
configFileName = args.config_file
no_infectious_stop = False
if args.no_infectious_stop:
no_infectious_stop = True
render = True
if args.no_render:
render = False
seed = 1000
if args.seed:
seed = args.seed
return configFileName, no_infectious_stop, render, seed
def main():
configFileName, no_infectious_stop, render, seed = parseArgs()
c = read_validate_config(configFileName)
# Load the data
gridSize = (c["gridHeight"], c["gridWidth"])
osmMap = mmap.readFile(
OSMfilePath = c["OSMfile"],
buildConnFile = c["buildConnFile"],
grid = gridSize,
buildingCSV = c["buildingConfigPath"])
# Start Simulator
sim = Simulator(
osmMap = osmMap,
jobCSVPath = c["jobsFile"],
businessCVSPath = c["businessFile"],
pathfindFileName = c["pathfindFileName"],
agentNum = c["numberOfAgents"],
threadNumber = c["threadNumber"],
infectedAgent = c["infectedAgent"],
vaccinationPercentage = c["vaccinationPercentage"],
reportPath = c["reportDir"],
reportInterval = c["reportInterval"],
seed=seed)
if render:
# Draw
windowSize = (c["windowWidth"], c["windowHeight"])
view = View(mymap=osmMap, simulation=sim, window_size=windowSize)
app = Controller(model=sim, view=view)
app.main_loop()
else:
stepSize = c["nr_step_size"] #5 minutes
dayToSimulate = c["nr_day_to_simulate"]
for x in range(0, dayToSimulate*24*3600, stepSize):
sim.step(stepSize = stepSize)
_, seirStatus = sim.getAgentStatus()
if no_infectious_stop and seirStatus["Infectious"] + seirStatus["Exposed"] == 0:
break
sim.extract()
sim.extractVisitLog() #extract all visit log
if __name__ == "__main__":
main()