-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttt_performance_measure.py
executable file
·295 lines (252 loc) · 8.1 KB
/
ttt_performance_measure.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
"""
QL Agent vs other agents Performance in TicTacToe
The Qlearning agent uses the learnt QTable to play against other agents, from random to Logical .
We notice that the QL Agent performs exceptionally against the Logical agent because it learnt by
playing against the Logical Agent. However, the Logical agent outperforms the QL Agent against a
random agent.
"""
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"
__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.episodes = 0
print('The Games Begin')
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.player1 = player1 #player1 type(QAgent,Human,etc.)
self.player2 = player2 #player2 type(QAgent,Human,etc.)
self.current_player = player1
self.episodes = episodes #A list of cumulative reward for plotting
self.iter = 0
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'
self.prevMove = None
self.prevState = None
self.player1 = player1
self.player2 = player2
self.current_player = player1
def play_game(self):
"""
This is the game loop
"""
while self.iter < self.episodes:
self.play_move() #play a move using current player
if self.isWinner is None:
self.turn = self.switch_player()
self.current_player = self.player1 if self.turn == 'X' else self.player2
else: #Meaning the game is over
self.count_winner()
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 strike is True:
self.state[int(pos)] = self.turn
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)
elif self.current_player == 'QLAgent':
state = self.list_to_string(self.state)
action = self.choose_action(state)
self.state[int(action)] = self.turn
self.valid.remove(action)
self.isWinner = self.check_winner(self.state)
elif self.current_player == 'Human':
pos = input("Enter the position where you want to place " + self.turn)
print('You selected : ' + pos)
self.state[int(pos)] = self.turn
self.valid.remove(pos)
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)
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 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 choose_action(self, state):
"""
Policy for choosing an action
"""
player = self.turn
listOfQValues = []
if state in self.qtable.keys():
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]
else:
action = random.choice(self.valid)
return str(action) if str(action) in self.valid else random.choice(self.valid)
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
"""
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' #somewhat intelligent playing agent
def qlagent(self):
"""
Updates the calling player object to QLearning Agent type
"""
self.kind = 'QLAgent' #Qlearning playing 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 = 100000
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))
fig = plt.figure()
x = [player1, 'DRAW', player2, 'Total Games']
a = game.xwin_count
b = game.draw_count
c = game.ywin_count
d = a+b+c
ax1 = fig.add_subplot(1, 1, 1)
ax1.clear()
bar1 = ax1.bar(x, [a, b, c, d])
bar1[0].set_color('r')
bar1[1].set_color('b')
bar1[2].set_color('g')
ax1.set_ylim((0, d + 100))
plt.draw()
plt.show()
if __name__ == "__main__":
main()