-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfill_mem.py
68 lines (49 loc) · 1.95 KB
/
fill_mem.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
from game import Game
from joblib import Parallel, delayed
from memory import Memory
from config import MCTS_SIMS
from timeit import default_timer as time
from ISMCTS import ISMCTS as mc
import pickle
import random
class testing_agent():
def __init__(self, mcts_simulations, name):
self.name = name
self.MCTSsimulations = mcts_simulations
self.cpuct = 0
def act(self, state, epsilon):
child_nodes = mc(state, self.MCTSsimulations)
action = max(child_nodes, key = lambda c: c.visits).move
return action
def worker(count, agent):
memories = []
env = Game()
while len(memories) < count:
state = env.reset()
states = []
done = False
while not done:
states.append(state)
action = agent.act(state, 0)
(state, value, done, _) = env.step(action)
for s in random.sample(states, 5):
memories.append((s, value))
return memories
def fill_mem(memories):
# get remaining memory count
remaining = memories.MEMORY_SIZE - len(memories.ltmemory)
print("Filling {} memories".format(remaining))
n_jobs = 4
batch_size = remaining / n_jobs if remaining % n_jobs == 0 else int(remaining / n_jobs) + 1
executor = Parallel(n_jobs=n_jobs, backend="multiprocessing", prefer="processes")
start = time()
chunks = executor(delayed(worker)(batch_size, testing_agent(MCTS_SIMS, 'best_player')) for _ in range(n_jobs))
for chunk in chunks:
for state, value in chunk:
memories.commit_stmemory(state)
memories.stmemory[-1]["value"] = value
memories.commit_ltmemory()
total = time() - start
print("Total time: {}, time per memory: {}".format(total, total/len(memories.ltmemory)))
#print(59*total/len(memories.ltmemory))
pickle.dump(memories, open("bo3texas42_50k.p", "wb"))