forked from ukoethe/bomberman_rl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraining_scheduler.py
72 lines (59 loc) · 2.1 KB
/
training_scheduler.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
import os
import subprocess
import sys
from datetime import datetime
from pathlib import Path
from sys import stdout
from time import sleep
ROUNDS = 300000
MODEL_ROOT_DIR = "./models/opponents"
CONFIGS_DIR = "./configs"
MAX_PARALLEL = 30
class Scheduler:
def __init__(self):
self.processes = [(None, None)] * MAX_PARALLEL
self.next_free = 0
def wait_for_free(self):
while True:
for index, process in enumerate(self.processes):
if process[0] is None:
self.next_free = index
return
if process[0].poll() is not None:
self.next_free = index
self.processes[index] = (None, None)
return
sleep(30)
def execute(self, path: Path):
if self.next_free is None:
raise Exception("No free slot")
current = Path(".")
p = subprocess.Popen(
[sys.executable, "./main.py", "play", "--my-agent", "auto_bomber", "--train", "1", "--n-rounds",
f"{ROUNDS}",
"--no-gui"],
env=dict(os.environ, MODEL_DIR=MODEL_ROOT_DIR + path.relative_to(current).__str__(),
CONFIG_FILE=path.absolute()),
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
print(f"[{datetime.now(tz=None).strftime('%m/%d/%Y, %H:%M:%S')}] Started: {path.__str__()} - pid: {p.pid}")
self.processes[self.next_free] = (p, path.stem)
self.next_free = None
def terminate(self, name):
for index, process in enumerate(self.processes):
if process[1] == name:
process[0].terminate()
self.processes[index] = (None, None)
def wait(self):
for p, n in self.processes:
if p is not None:
p.wait()
if __name__ == '__main__':
scheduler = Scheduler()
configs_to_process = Path(CONFIGS_DIR).glob("**/*.json")
for config in configs_to_process:
scheduler.wait_for_free()
scheduler.execute(config)
stdout.flush()
scheduler.wait()