forked from gkyriopoulos/Koifish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrainer.py
84 lines (68 loc) · 2.34 KB
/
Trainer.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
#!/usr/bin/env python3
import sys
import time
import ujson
from matplotlib import pyplot as plt
import Agent
def main():
if len(sys.argv) != 3:
print("Please provide the board and the episodes as a command-line argument.")
return
board = sys.argv[1]
episodes = int(sys.argv[2])
train(board, episodes, False, True, True, True)
def train(board, episodes, plot, save, train_w, train_b):
avg_reward_w = []
avg_reward_b = []
wr_w = 0
wr_b = 0
time_w = 0
time_b = 0
if train_w:
start_time1 = time.time()
avg_reward_w, wr_w = Agent.train_agent_vs_random(board, episodes, "w")
end_time1 = time.time()
time_w = end_time1 - start_time1
if train_b:
start_time2 = time.time()
avg_reward_b, wr_b = Agent.train_agent_vs_random(board, episodes, "b")
end_time2 = time.time()
time_b = end_time2 - start_time2
# start_time3.append(time.time())
# Agent.train_agent_vs_agent(tb, episodes)
# end_time3.append(time.time())
# times_agent_3 = numpy.subtract(end_time3, start_time3).tolist()
if plot:
plt.semilogy(avg_reward_w, label="White Reward, T = " + str(episodes))
plt.semilogy(avg_reward_b, label="Black Reward, T = " + str(episodes))
plt.legend()
plt.xlabel("Time")
plt.ylabel("Average Reward")
plt.show()
if save:
folder = 'stats'
graph_file = folder + '/' + board + "_graph" + ".stats"
wr_file = folder + '/' + board + "_wr" + ".stats"
time_file = folder + '/' + board + "_time" + ".stats"
with open(graph_file, 'a+') as file:
if train_w:
file.write(ujson.dumps(avg_reward_w.tolist()))
file.write('\n')
if train_b:
file.write(ujson.dumps(avg_reward_b.tolist()))
file.write('\n')
file.write('\n')
with open(wr_file, 'a+') as file:
if train_w:
file.write(str(wr_w) + '\n')
if train_b:
file.write(str(wr_b) + '\n')
file.write('\n')
with open(time_file, 'a+') as file:
if train_w:
file.write(str(time_w) + '\n')
if train_b:
file.write(str(time_b) + '\n')
file.write('\n')
if __name__ == "__main__":
main()