forked from bardia-ardakanian/CE351-CI-EvolutionaryGames
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
31 lines (26 loc) · 808 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
import os
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