-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraining.py
193 lines (146 loc) · 5.52 KB
/
training.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import numpy as np
import pandas as pd
from tqdm import tqdm
import matplotlib.pyplot as plt
import algorithms
from algorithms import win_eval
def train_as_X(X_player,O_player,episodes,plot=False):
t = tqdm(desc='Training Q-Learning Algorithm! for '+str(episodes)+' Games. PLEASE WAIT!!')
results = []
for episode in range(episodes):
board = np.zeros((3,3),int)
while win_eval(board) == 0:
move = X_player.get_move(board,2)
S = np.copy(board)
A = move
board[move[0],move[1]] = 2
if win_eval(board) != 0:
reward = algorithms.score_eval(board,2)
prev = X_player.q_storage(S,X_player.shape_it(A))
X_player.q_table[(X_player.conceal(S),X_player.shape_it(A))] = prev + X_player.alpha * (reward + X_player.gamma*reward - prev)
break
move = O_player.get_move(board,1)
board[move[0],move[1]] = 1
S1 = np.copy(board)
X_player.epsilon = 0
A1 = X_player.get_move(board,2)
X_player.epsilon = 0.2 if episode <0.95*episodes else 0
reward = algorithms.score_eval(board,2)
X_player.learn(S,A,S1,A1,reward)
results.append([episode,algorithms.score_eval(board,2)])
t.update(1)
t.close()
win = []
lose = []
draw = []
for el in results:
if el[1] == 1:
win.append([el[0],1])
lose.append([el[0],0])
draw.append([el[0],0])
elif el[1] == -1:
win.append([el[0],0])
lose.append([el[0],1])
draw.append([el[0],0])
elif el[1] == 0:
win.append([el[0],0])
lose.append([el[0],0])
draw.append([el[0],1])
if plot:
df = pd.DataFrame(results,columns=['Episode','Result'])
dfwin = pd.DataFrame(win,columns=['Episode','Result'])
dflose = pd.DataFrame(lose,columns=['Episode','Result'])
dfdraw = pd.DataFrame(draw,columns=['Episode','Result'])
import pickle
pickle.dump([df,dfwin,dflose,dfdraw], open("train_stats.pkl","wb"))
span = int(0.05*episodes)
smaw = dfwin.rolling(window=span, min_periods=span).mean()[:span]
smal = dfwin.rolling(window=span, min_periods=span).mean()[:span]
smad = dfwin.rolling(window=span, min_periods=span).mean()[:span]
restw = dfwin[span:]
restl = dflose[span:]
restd = dfdraw[span:]
win = pd.concat([smaw, restw]).ewm(span=span, adjust=False).mean()
lose = pd.concat([smal, restl]).ewm(span=span, adjust=False).mean()
draw = pd.concat([smad, restd]).ewm(span=span, adjust=False).mean()
plt.plot(win.Episode,win.Result, label='Win')
plt.plot(lose.Episode,lose.Result, label='Loss')
plt.plot(draw.Episode,draw.Result, label='Draw')
plt.legend()
plt.xlabel('Episode')
plt.ylabel('Result')
plt.ylim(0,1)
plt.show()
return(X_player.q_table)
def train_as_O(X_player,O_player,episodes,plot=False):
t = tqdm(desc='Training Q-Learning Algorithm! for '+str(episodes)+' Games. PLEASE WAIT!!')
results = []
for episode in range(episodes):
board = np.zeros((3,3),int)
move = X_player.get_move(board,2)
while win_eval(board) == 0:
move = O_player.get_move(board,1)
S = np.copy(board)
A = move
board[move[0],move[1]] = 1
if win_eval(board) != 0:
reward = algorithms.score_eval(board,1)
prev = O_player.q(S,O_player.format(A))
O_player.q_table[(O_player.encode(S),O_player.format(A))] = prev + O_player.alpha * (reward + O_player.gamma*reward - prev)
break
move = X_player.get_move(board,2)
board[move[0],move[1]] = 2
S1 = np.copy(board)
O_player.epsilon = 0
A1 = O_player.get_move(board,1)
O_player.epsilon = 0.2 if episode < 0.95*episodes else 0
reward = algorithms.score_eval(board,1)
O_player.learn(S,A,S1,A1,reward)
results.append([episode,algorithms.score_eval(board,1)])
t.update(1)
t.close()
win = []
lose = []
draw = []
for el in results:
if el[1] == 1:
win.append([el[0],1])
lose.append([el[0],0])
draw.append([el[0],0])
elif el[1] == -1:
win.append([el[0],0])
lose.append([el[0],1])
draw.append([el[0],0])
elif el[1] == 0:
win.append([el[0],0])
lose.append([el[0],0])
draw.append([el[0],1])
if plot:
df = pd.DataFrame(results,columns=['Episode','Result'])
dfwin = pd.DataFrame(win,columns=['Episode','Result'])
dflose = pd.DataFrame(lose,columns=['Episode','Result'])
dfdraw = pd.DataFrame(draw,columns=['Episode','Result'])
import pickle
pickle.dump([df,dfwin,dflose,dfdraw], open("train_stats.pkl","wb"))
span = int(0.05*episodes)
smaw = dfwin.rolling(window=span, min_periods=span).mean()[:span]
smal = dfwin.rolling(window=span, min_periods=span).mean()[:span]
smad = dfwin.rolling(window=span, min_periods=span).mean()[:span]
restw = dfwin[span:]
restl = dflose[span:]
restd = dfdraw[span:]
win = pd.concat([smaw, restw]).ewm(span=span, adjust=False).mean()
lose = pd.concat([smal, restl]).ewm(span=span, adjust=False).mean()
draw = pd.concat([smad, restd]).ewm(span=span, adjust=False).mean()
expw = dfwin.Result.ewm(span=span, adjust=False).mean()
expl = dflose.Result.ewm(span=span, adjust=False).mean()
expd = dfdraw.Result.ewm(span=span, adjust=False).mean()
plt.plot(lose.Episode,lose.Result, label='Win')
plt.plot(win.Episode,win.Result, label='Loss')
plt.plot(draw.Episode,draw.Result, label='Draw')
plt.legend()
plt.xlabel('Episode')
plt.ylabel('Result')
plt.ylim(0,1)
plt.show()
return(O_player.q_table)