-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
39 lines (31 loc) · 1022 Bytes
/
util.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
import os
from _csv import writer
from os.path import join
import pickle
from pathlib import Path
import shutil
# save players of this generation in file
def save_generation(players, gen_num, mode):
path = Path(join('checkpoint', mode, str(gen_num)))
try:
shutil.rmtree(path)
except OSError as e:
pass
path.mkdir(parents=True, exist_ok=True)
for i, p in enumerate(players):
player_path = join(path, str(i))
with open(player_path, 'wb') as file:
pickle.dump(p, file)
# load players from file
def load_generation(checkpoint_path):
files = os.listdir(checkpoint_path)
prev_players = []
for f in files:
with open(join(checkpoint_path, f), 'rb') as file:
p = pickle.load(file)
prev_players.append(p)
return prev_players
def append_list_as_row(file_name, list_of_elem):
with open(file_name, 'a+', newline='') as write_obj:
csv_writer = writer(write_obj)
csv_writer.writerow(list_of_elem)