-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathttt_class.py
executable file
·371 lines (325 loc) · 12.4 KB
/
ttt_class.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
"""
Plays TicTacToe against a pseudo logical(simpleton) AI and prepares a Q-Table for future reference
The Qlearning agent play a number of episodes of the famous boardgame- Tic Tac Toe against a psuedo
logical AI and learn from experience to play and eventually beat the pseudo logical AI. In this
program, we simply train the agent by playing it against the pseudo logical AI for a given number
of episodes. We store it using the pickle library and we can also load an existing Q-table to
update it further.
Note that the human player playing logic is not yet defined in the program, and I hope to update it
at a later stage such that we can then make the agent learn by playing against a human player as
well.
"""
import numpy as np
import copy
import math
import random
import operator
import itertools
import time as time
from matplotlib import pyplot as plt
import pickle
__author__ = "Syed Ali Shahbaz"
__copyright__ = "Copyright 2019, TicTacToe with AI"
__credits__ = ["Abhijit Nair"]
__license__ = "MIT"
__version__ = "1.0.1"
__maintainer__ = "Syed Ali Shahbaz"
__email__ = "[email protected]"
__status__ = "Production"
class tictactoe_game():
"""
Defines the TicTacToe Game sequence class
"""
def __init__(self, player1, player2, episodes, qtable):
"""
Constructor for tic-tac-toe game initialization
"""
self.learning = True
self.qtable = qtable
self.exploration = 0.2
self.episodes = 0
print('The Learning Begins')
self.state = ['0','1','2','3','4','5','6','7','8'] #initialize current state as empty board
self.valid = ['0','1','2','3','4','5','6','7','8'] #initialize valid moves as empty slots in the current state
self.isWinner = None #initialize winner as none
self.turn = 'X' #initialize the current symbol as X
self.xwin_count = 0
self.ywin_count = 0
self.draw_count = 0
self.prevMove = None #Remember previous move for learning
self.prevState = None #Remember previous State for learning
self.player1 = player1 #player1 type(QAgent,Human,etc.)
self.player2 = player2 #player2 type(QAgent,Human,etc.)
self.current_player = player1 #the current player type
self.rewardSum = 0 #total reward sum over the current episode
self.rewardCnt = 0 #total reward adding to existing rewards in previous episodes
self.cumureward = [] #A list of cumulative reward for plotting
self.episodes = episodes
self.iter = 0
self.cumuWins = [] #A list of cumulative wins for plotting
def reset_game(self, player1, player2):
"""
Reset the board to initialization game state
"""
self.state = ['0','1','2','3','4','5','6','7','8'] #Reset current state as empty board
self.valid = ['0','1','2','3','4','5','6','7','8'] #Reset valid moves as empty slots in the current state
self.isWinner = None #Reset winner as none
self.turn = 'X' #Reset the current symbol as X
self.prevMove = None
self.prevState = None
self.player1 = player1
self.player2 = player2
self.current_player = player1
self.rewardCnt += self.rewardSum #update total reward count
self.cumureward.append(self.rewardCnt) #Update cumureward list
self.rewardSum = 0 #Reset reward Sum for new episode
def play_game(self):
"""
This is the game loop for every single episode
"""
while self.iter < self.episodes:
#self.draw_board() #Uncomment this line if you wish to visualize the text-based play on the console
self.play_move() #play a move using current player
if self.isWinner is None:
reward = self.get_reward() #get reward for self.prevState and self.prevMove
self.update_qtable(reward) #update qtable for self.prevState and prev reward
self.rewardSum += reward
self.turn = self.switch_player()
self.current_player = self.player1 if self.turn == 'X' else self.player2
else: #Meaning the game is over
reward = self.get_reward() #get reward for self.prevState and self.prevMove
self.update_qtable(reward) #update qtable for self.prevState and prev reward
self.rewardSum += reward
self.count_winner()
self.cumuWins.append(self.xwin_count)
self.reset_game(self.player1, self.player2)
self.iter +=1
def play_move(self):
"""
This function decides the mechanics and logic behind every move based on the Agent/Player type
"""
if self.current_player == 'LogicAgent':
if len(self.valid)>1:
strike, pos = self.check_strike() #if there is a strike, and what position on board to place the symbol
if strike is True:
self.state[int(pos)] = self.turn #using board position as index, since board position is same as item value in our case
self.valid.remove(pos)
self.isWinner = self.check_winner(self.state)
else:
pos = random.choice(self.valid)
self.state[int(pos)] = self.turn
self.valid.remove(pos)
elif len(self.valid) == 1:
pos = self.valid[0]
self.state[int(pos)] = self.turn
self.valid.remove(pos)
self.isWinner = self.check_winner(self.state)
state = self.list_to_string(self.state) #convert list to string for qtable dictionary manipulation
if state not in self.qtable.keys():
self.add_key(state) #add the current state as a string in the qtable as new key
elif self.current_player == 'QLAgent':
state = self.list_to_string(self.state) #convert list to string for qtable dictionary manipulation
if state not in self.qtable.keys():
self.add_key(state) #add the current state as a string in the qtable as new key
action = self.choose_action(state) #choose an action based on the specific policy design
self.prevMove = int(action)
self.prevState = self.state[:]
self.state[int(action)] = self.turn #place the symbol on current action position
self.valid.remove(action)
nextState = self.list_to_string(self.state)
if nextState not in self.qtable.keys():
self.add_key(nextState)
self.isWinner = self.check_winner(self.state)
elif self.current_player == 'Random':
pos = random.choice(self.valid)
self.state[int(pos)] = self.turn
self.valid.remove(pos)
state = self.list_to_string(self.state)
if state not in self.qtable.keys():
self.add_key(state) #add the current state as a string in the qtable as new key
self.isWinner = self.check_winner(self.state)
def switch_player(self):
"""
Switches the current player control and returns the player's symbol
"""
self.current_player = self.player2 if self.current_player == self.player1 else self.player1
return 'X' if self.turn == 'O' else 'O'
def get_reward(self):
"""
Returns reward based on the policy
"""
if self.isWinner == 'X':
return 1
elif self.isWinner == 'O':
return -1
else:
return 0
def list_to_string(self, list):
"""
Returns string converted from the provided list
"""
str = ""
for x in list:
str += x
return str
def check_strike(self):
"""
Returns the next state's possibility of game winning move for either players along with the position
"""
gameState = self.state[:]
for pos in self.valid:
gameState[int(pos)] = self.turn
if self.check_winner(gameState) == self.turn:
return True, pos
gameState = self.state[:]
opponent = 'O' if self.turn == 'X' else 'X'
gameState[int(pos)] = opponent
if self.check_winner(gameState) == opponent:
return True, pos
gameState = self.state[:]
return False, '0'
def check_winner(self, state):
"""
Returns the result of the game or None, if there's free space on the board to play and neither of the players won
"""
state = self.list_to_string(state)
winner = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]]
for line in winner:
strike = state[line[0]] + state[line[1]] + state[line[2]]
if strike == 'XXX':
return 'X'
elif strike == 'OOO':
return 'O'
elif len(self.valid)<1:
return 'Draw'
return None
def add_key(self, state):
"""
Adds the key to the qtable
"""
self.qtable.update({state:{0:0.0, 1:0.0, 2:0.0, 3:0.0, 4:0.0, 5:0.0, 6:0.0, 7:0.0, 8:0.0}})
def choose_action(self, state):
"""
Policy for choosing an action
"""
player = self.turn
if self.iter == 100000:
self.exploration = 1.01
num = random.uniform(0,1)
if num < self.exploration:
listOfQValues = []
for pos, val in self.qtable[state].items():
if str(pos) in self.valid:
listOfQValues.append( tuple((pos, val)) )
action = max(listOfQValues,key=operator.itemgetter(1))[0]
return str(action) if str(action) in self.valid else random.choice(self.valid)
else:
action = random.choice(self.valid)
return action
def update_qtable(self,reward):
"""
Qtable update policy
"""
discount = 0.01
learningRate = 0.5
state = self.list_to_string(self.state)
prevState = self.list_to_string(self.prevState)
if self.isWinner is not None:
expected = reward
else:
expected = reward + (discount * max(self.qtable[state].items(), key=operator.itemgetter(1))[0])
try:
change = learningRate * (expected - self.qtable[prevState][self.prevMove])
except:
print('error in : ' + prevState)
print('action : ' + str(self.prevMove))
print('Turn : ' + self.turn)
print(self.qtable)
self.qtable[prevState][self.prevMove] += change
def count_winner(self):
"""
Updates the win/draw count
"""
if self.isWinner == 'X':
self.xwin_count+=1
elif self.isWinner == 'O':
self.ywin_count+=1
else:
self.draw_count+=1
def draw_board(self):
"""
Provides a text-based visualization of the game
"""
print(self.current_player + '\'s turn playing : ' + self.turn )
time.sleep(2)
state = self.list_to_string(self.state)
print('_______')
print('|'+state[0]+'|'+state[1]+'|'+state[2]+'|')
print('|'+state[3]+'|'+state[4]+'|'+state[5]+'|')
print('|'+state[6]+'|'+state[7]+'|'+state[8]+'|')
print('_______')
nextKey = self.state
class Player():
"""
Defines the Player Type Class
"""
def __init__(self):
"""
Constructor for the player type with default as Random
"""
self.kind = 'Random' #random playing bot
def agent(self):
"""
Updates the calling player object to Logical Agent type
"""
self.kind = 'LogicAgent' #Pseudo-logial intelligent agent
def qlagent(self):
"""
Updates the calling player object to QLearning Agent type
"""
self.kind = 'QLAgent' #Qlearning agent
def human(self):
"""
Updates the calling player object to Human Player type
"""
self.kind = 'human' #human player
def main():
"""
Main Function of the program where we construct objects of tictactoe_game class and player class. We also plot the result in this function.
"""
player1 = Player()
player2 = Player()
player1.qlagent()
player2.agent()
player1 = player1.kind
player2 = player2.kind
episodes = 200000
qtable = {'012345678': {0:0.0, 1:0.0, 2:0.0, 3:0.0, 4:0.0, 5:0.0, 6:0.0, 7:0.0, 8:0.0}}
#########################################################################################################
####NOTE : if you want to update an existing qtable, please uncomment the following two lines of code:###
####pickle_in = open("Qlearn_new.pickle","rb")###########################################################
####qtable = pickle.load(pickle_in)######################################################################
#########################################################################################################
game = tictactoe_game(player1,player2,episodes,qtable)
game.play_game()
print (player1 + ' as X wins:' + str(game.xwin_count))
print (player2 + ' as O wins:' + str(game.ywin_count))
print ('Draws:' + str(game.draw_count))
print ('Qtable entries : ' + str( len(game.qtable) ) )
x=range(0,200000)
plt.plot(x, game.cumuWins)
plt.xlabel('Episodes')
plt.ylabel('Number of Wins')
plt.show()
pickle_out = open("Qlearn_new.pickle","wb")
pickle.dump(game.qtable, pickle_out)
pickle_out.close
def print_qtable(qtable, indent=0):
"""
Print Qtable with state(Key) and values. Useful in debugging.
"""
for key, value in qtable.items():
print(key)
print(value)
if __name__ == "__main__":
main()