-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_test.py
261 lines (184 loc) · 7.61 KB
/
run_test.py
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import subprocess
import time
import os
import csv
import sys
import json
from pathlib import Path
import platform
import random
"""
parametros:
1. Archivo de mundos
"""
port = 1234
def loadController(erebus_directory, controller):
erebusControllerPath = erebus_directory / "game/controllers/robot0Controller/robot0Controller.py"
with open(controller, "r") as contToLoad:
with open(erebusControllerPath, "w") as contToWrite:
contToWrite.write(contToLoad.read())
print(f"Loaded {controller}")
def getOpenScript(world):
global port
port += 1
if platform.system() == "Linux":
return f"""#!/bin/bash
webots {world} --mode=fast --minimize --no-rendering --port={port}
"""
elif platform.system() == "Windows":
return f"""webots {world} --mode=fast --minimize --no-rendering --port={port}"""
else:
raise OSError("OS not supported. Please use either Windows or Linux")
def openWebots(world):
print("Opening webots with world:", world)
script = getOpenScript(world)
rc = subprocess.Popen(script, shell=True)
def getKillScript():
if platform.system() == "Linux":
return """#!/bin/bash
pkill webots
"""
elif platform.system() == "Windows":
return """taskkill/im webots.exe /F"""
else:
raise OSError("OS not supported. Please use either Windows or Linux")
def killWebots():
script = getKillScript()
rc = subprocess.Popen(script, shell=True)
def processLog(world: Path, input_file_name: Path, output_file_name: Path, time_taken, log_directory: Path):
if "gameLog" not in input_file_name:
return
with open(log_directory / input_file_name, "r") as log:
lines = log.readlines()
# Score
for line in lines:
if "ROBOT_0_SCORE: " in line:
line = line.replace("ROBOT_0_SCORE: ", "")
line = line.replace("\n", "")
finalScore = float(line)
# Hazards
hazards_detected = 0
hazards_correctly_identified = 0
victims_detected = 0
victims_correctly_identified = 0
checkpoints_found = 0
fixture_type_missidentification = 0
completion_percentage = 0
finalTime = 8*60
for line in lines:
if "Successful Exit" in line:
finalTime = (int(line[0:2]) * 60) + int(line[3:5])
if "Successful Hazard Identification" in line:
hazards_detected += 1
elif "Successful Hazard Type Correct Bonus" in line:
hazards_correctly_identified += 1
elif "Successful Victim Identification" in line:
victims_detected += 1
elif "Successful Victim Type Correct Bonus" in line:
victims_correctly_identified += 1
elif "Found checkpoint" in line:
checkpoints_found += 1
elif "Map Correctness" in line:
completion_percentage = line[-7:-2].replace(" ", "")
completion_percentage = float(completion_percentage) / 100
elif "Misidentification" in line:
fixture_type_missidentification += 1
print("Final time:", finalTime, "seconds")
print("Final score:", finalScore)
with open(output_file_name, "a") as file:
writer = csv.writer(file)
writer.writerow([world.stem,
"",
finalScore,
"",
finalTime,
time_taken,
completion_percentage,
hazards_detected,
hazards_correctly_identified,
victims_detected,
victims_correctly_identified,
fixture_type_missidentification,
checkpoints_found])
def processLogs(world: Path, file_name: Path, time_taken, log_directory: Path, number_of_logs: int):
log_list = sorted(os.listdir(log_directory))
for log in log_list[-number_of_logs:]:
processLog(world, log, file_name, time_taken, log_directory)
def testRunsUntilDone(world: Path, fileName, log_directory: Path, reps: int, timeout):
initialLogNumber = len(os.listdir(log_directory))
newLogNumber = len(os.listdir(log_directory))
start_time = time.time()
for _ in range(reps):
time.sleep(random.randint(1, 8))
openWebots(world)
while True:
newLogNumber = len(os.listdir(log_directory))
new_logs_count = newLogNumber - initialLogNumber
if time.time() - start_time > timeout:
killWebots()
testRunsUntilDone(world, fileName, log_directory, reps - new_logs_count, timeout + 60)
if new_logs_count >= reps:
break
print("Finished run")
time_taken = time.time() - start_time
print("Time taken:", time_taken, "seconds")
time.sleep(1)
print("Closing webots...")
killWebots()
return time_taken
def testRun(world: Path, fileName, log_directory: Path, reps: int, timeout):
time_taken = testRunsUntilDone(world, fileName, log_directory, reps, timeout)
print("Processing data...")
processLogs(world, fileName, time_taken, log_directory, reps)
def get_output_file_name(run_name: str, world_set_dir: Path):
actual_time = time.strftime("%d-%m-%Y_%H-%M-%S")
return run_name + "_(" + world_set_dir.stem + ")_" + actual_time
def make_output_file(config):
try:
os.mkdir(Path("./runs") / config["run_name"])
except FileExistsError:
print("Directory already exists")
name = get_output_file_name(config["run_name"], Path(config["world_set"])) + ".csv"
output_file =Path("./runs", config["run_name"], name)
with open(output_file, "w") as output:
writer = csv.writer(output)
writer.writerow(["World",
"Max Score",
"Score",
"Score Percentage",
"Simulation Time",
"Real Time",
"final map correctness",
"hazards_detected",
"hazards_correctly_identified",
"victims_detected",
"victims_correctly_identified",
"fixture_type_missidentification",
"checkpoints found"])
return output_file
def test_runs(config):
erebus_directory = Path(config["erebus_directory"])
reps = int(config["reps"])
log_directory = erebus_directory / "game/logs/"
output_file = make_output_file(config)
loadController(erebus_directory, config["controller"])
with open(config["world_set"], "r") as worlds:
lines = worlds.readlines()
actualRuns = 0
totalRuns = len(lines) * int(config["reps"])
init_time = time.time()
for world in lines:
print("#########################################################")
world = world.replace("\n", "")
world = erebus_directory / ("game/worlds/" + world)
testRun(world, output_file, log_directory, reps, timeout=60*4)
actualRuns += reps
time.sleep(1)
print("Tested", actualRuns, "/", totalRuns, "simulations")
time_so_far = time.time() - init_time
print("Total time so far:", time_so_far / 60, "minutes")
print("Estimated time left:", time_so_far / actualRuns * (totalRuns - actualRuns) / 60, "minutes")
if __name__ == "__main__":
with open(sys.argv[1], "r") as config_file:
config = json.load(config_file)
test_runs(config)