-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopp_self-play.py
588 lines (488 loc) · 22.1 KB
/
opp_self-play.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = '1'
import pygame, sys
from tqdm import tqdm
from collections import defaultdict
import matplotlib.pyplot as plt
import random
import tkinter as tk
from tkinter import simpledialog
board = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
graphical_board = [[[None, None], [None, None], [None, None]],
[[None, None], [None, None], [None, None]],
[[None, None], [None, None], [None, None]]]
logical_board = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
]
to_move = 'X'
def render_board(board, ximg, oimg):
global graphical_board
for i in range(3):
for j in range(3):
if board[i][j] == 'X':
graphical_board[i][j][0] = ximg
graphical_board[i][j][1] = ximg.get_rect(center=(j*300+150, i*300+150))
elif board[i][j] == 'O':
graphical_board[i][j][0] = oimg
graphical_board[i][j][1] = oimg.get_rect(center=(j*300+150, i*300+150))
def add_XO(board, graphical_board, to_move, logical_board):
current_pos = pygame.mouse.get_pos()
converted_x = (current_pos[0] - 65) / 835 * 2
converted_y = current_pos[1] / 835 * 2
row = round(converted_y)
col = round(converted_x)
if board[row][col] != 'O' and board[row][col] != 'X':
board[row][col] = to_move
if to_move == 'X':
logical_board[row][col] = 1 # 1 = X
to_move = 'O'
else:
logical_board[row][col] = 2 # 2 = O
to_move = 'X'
render_board(board, X_IMG, O_IMG)
for i in range(3):
for j in range(3):
if graphical_board[i][j][0] is not None:
SCREEN.blit(graphical_board[i][j][0], graphical_board[i][j][1])
return board, to_move, logical_board
def place_O(board, logical_board, position):
row, col = position
board[row][col] = 'O'
logical_board[row][col] = 2
return board, logical_board
def place_X(board, logical_board, position):
row, col = position
board[row][col] = 'X'
logical_board[row][col] = 1
return board, logical_board
def check_win(board):
winner = None
for row in range(0, 3):
if((board[row][0] == board[row][1] == board[row][2]) and (board [row][0] is not None)):
winner = board[row][0]
return winner
for col in range(0, 3):
if((board[0][col] == board[1][col] == board[2][col]) and (board[0][col] is not None)):
winner = board[0][col]
return winner
if (board[0][0] == board[1][1] == board[2][2]) and (board[0][0] is not None):
winner = board[0][0]
return winner
if (board[0][2] == board[1][1] == board[2][0]) and (board[0][2] is not None):
winner = board[0][2]
return winner
if winner is None:
for i in range(len(board)):
for j in range(len(board)):
if board[i][j] != 'X' and board[i][j] != 'O':
return None
return "DRAW"
def check_win_update(board):
winner = None
for row in range(0, 3):
if((board[row][0] == board[row][1] == board[row][2]) and (board [row][0] is not None)):
winner = board[row][0]
for i in range(0, 3):
graphical_board[row][i][0] = pygame.image.load(f"assets/Winning {winner}.png")
SCREEN.blit(graphical_board[row][i][0], graphical_board[row][i][1])
pygame.display.update()
return winner
for col in range(0, 3):
if((board[0][col] == board[1][col] == board[2][col]) and (board[0][col] is not None)):
winner = board[0][col]
for i in range(0, 3):
graphical_board[i][col][0] = pygame.image.load(f"assets/Winning {winner}.png")
SCREEN.blit(graphical_board[i][col][0], graphical_board[i][col][1])
pygame.display.update()
return winner
if (board[0][0] == board[1][1] == board[2][2]) and (board[0][0] is not None):
winner = board[0][0]
graphical_board[0][0][0] = pygame.image.load(f"assets/Winning {winner}.png")
SCREEN.blit(graphical_board[0][0][0], graphical_board[0][0][1])
graphical_board[1][1][0] = pygame.image.load(f"assets/Winning {winner}.png")
SCREEN.blit(graphical_board[1][1][0], graphical_board[1][1][1])
graphical_board[2][2][0] = pygame.image.load(f"assets/Winning {winner}.png")
SCREEN.blit(graphical_board[2][2][0], graphical_board[2][2][1])
pygame.display.update()
return winner
if (board[0][2] == board[1][1] == board[2][0]) and (board[0][2] is not None):
winner = board[0][2]
graphical_board[0][2][0] = pygame.image.load(f"assets/Winning {winner}.png")
SCREEN.blit(graphical_board[0][2][0], graphical_board[0][2][1])
graphical_board[1][1][0] = pygame.image.load(f"assets/Winning {winner}.png")
SCREEN.blit(graphical_board[1][1][0], graphical_board[1][1][1])
graphical_board[2][0][0] = pygame.image.load(f"assets/Winning {winner}.png")
SCREEN.blit(graphical_board[2][0][0], graphical_board[2][0][1])
pygame.display.update()
return winner
if winner is None:
for i in range(len(board)):
for j in range(len(board)):
if board[i][j] != 'X' and board[i][j] != 'O':
return None
return "DRAW"
def get_empty_spots(logical_board):
empty_spots = []
for i in range(3):
for j in range(3):
if logical_board[i][j] == 0:
empty_spots.append((i, j))
return empty_spots
def print_q_value(q_values):
print("=== Q-Values Table ===")
for state, actions in q_values.items():
print("State:")
for row in state:
print(" " + " ".join(str(cell) for cell in row))
print("Actions and Q-values:")
for action, q_value in actions.items():
print(f" Action {action}: Q-value = {q_value:.2f}")
print("------------------------")
print("========================\n")
def print_state_q_values(q_values, state):
print("=== Q-Values for the Given State ===")
print("State:")
for row in state:
print(" " + " ".join(str(cell) for cell in row))
print("\nActions and Q-values:")
if state in q_values:
actions = q_values[state]
for action, q_value in actions.items():
print(f" Action {action}: Q-value = {q_value:.2f}")
else:
print(" No actions available for this state.")
print("====================================\n")
def prompt_for_rl_params():
root = tk.Tk()
root.withdraw()
max_episodes = simpledialog.askinteger(
title="Max Episodes",
prompt="Enter the number of episodes you want to train for:",
minvalue=1,
maxvalue=10_000_000
)
if max_episodes is None:
print("No input provided for max_episodes. Using default = 20000.")
max_episodes = 20000
learning_rate = simpledialog.askfloat(
title="Learning Rate",
prompt="Enter the learning rate (alpha):\n(e.g., 0.1, 0.3, etc.)",
minvalue=0.000001,
maxvalue=1.0
)
if learning_rate is None:
print("No input provided for alpha. Using default = 0.3.")
learning_rate = 0.3
discount_factor = simpledialog.askfloat(
title="Discount Factor",
prompt="Enter the discount factor (gamma):\n(e.g., 0.9, 0.99, etc.)",
minvalue=0.0,
maxvalue=1.0
)
if discount_factor is None:
print("No input provided for gamma. Using default = 0.9.")
discount_factor = 0.9
root.destroy()
return max_episodes, learning_rate, discount_factor
def prompt_for_player_choice():
def set_choice(selected_choice):
nonlocal choice
choice = selected_choice
root.destroy() # Close the window once a choice is made
root = tk.Tk()
root.title("Player Choice")
# Set the window size and position
root.geometry("300x150")
root.eval('tk::PlaceWindow . center') # Center the window
# Add label
label = tk.Label(root, text="Would you like to go first or second?", font=("Helvetica", 12))
label.pack(pady=10)
# Add buttons for "X" and "O"
button_x = tk.Button(root, text="First", font=("Helvetica", 14), width=10, command=lambda: set_choice("X"))
button_x.pack(pady=5)
button_o = tk.Button(root, text="Second", font=("Helvetica", 14), width=10, command=lambda: set_choice("O"))
button_o.pack(pady=5)
# Initialize choice
choice = None
root.mainloop() # Run the Tkinter event loop
# Default to "X" if no choice was made (e.g., the window was closed)
if choice is None:
choice = "X"
return choice
game_finished = False
count = 0
win_count = 0
loss_count = 0
stalemate_count = 0
q_values = defaultdict(lambda: defaultdict(float))
epsilon_greedy = 1.0
x_q_values = defaultdict(lambda: defaultdict(float))
x_epsilon_greedy = 1.0
x_learning_rate = 0.3
x_discount_factor = 0.9
win_rate_history = []
game_intervals = []
max_episodes, learning_rate, discount_factor = prompt_for_rl_params()
player_choice = prompt_for_player_choice()
pbar = tqdm(total=max_episodes, desc="Training", ncols=80)
while count < max_episodes:
# (1) The user is about to place X if it's X's turn
# If game is finished, do resets
if game_finished:
board = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
graphical_board = [
[[None, None], [None, None], [None, None]],
[[None, None], [None, None], [None, None]],
[[None, None], [None, None], [None, None]]
]
logical_board = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
]
to_move = player_choice
game_finished = False
if to_move == "X":
x_last_state = tuple(tuple(row) for row in logical_board)
for i in range(3):
for j in range(3):
if logical_board[i][j] == 0:
if (i,j) not in x_q_values[x_last_state]:
x_q_values[x_last_state][(i,j)] = 0.0
if random.random() < x_epsilon_greedy:
row1, col1 = random.choice(get_empty_spots(logical_board))
else:
max_action1 = max(x_q_values.get(x_last_state, {}).items(), key=lambda x: x[1])
action1, max_value1 = max_action1
row1, col1 = action1
place_X(board, logical_board, (row1, col1))
x_new_state = tuple(tuple(row) for row in logical_board)
for i in range(3):
for j in range(3):
if logical_board[i][j] == 0:
if (i,j) not in x_q_values[x_new_state]:
x_q_values[x_new_state][(i,j)] = 0.0
reward1 = 0
actions_dict1 = x_q_values.get(x_new_state, {})
if len(actions_dict1) == 0:
max_value1 = 0.0
else:
action1, max_value1 = max(actions_dict1.items(), key=lambda x: x[1])
x_q_values[x_last_state][(row1, col1)] = x_q_values[x_last_state][(row1, col1)] + x_learning_rate*(reward1 + x_discount_factor*(max_value1) - x_q_values[x_last_state][(row1, col1)])
to_move = 'O'
else:
last_state = tuple(tuple(row) for row in logical_board)
for i in range(3):
for j in range(3):
if logical_board[i][j] == 0:
if (i,j) not in q_values[last_state]:
q_values[last_state][(i,j)] = 0.0
if random.random() < epsilon_greedy:
row, col = random.choice(get_empty_spots(logical_board))
else:
max_action = max(q_values.get(last_state, {}).items(), key=lambda x: x[1])
action, max_value = max_action
row, col = action
place_O(board, logical_board, (row,col))
new_state = tuple(tuple(row) for row in logical_board)
for i in range(3):
for j in range(3):
if logical_board[i][j] == 0:
if (i,j) not in q_values[new_state]:
q_values[new_state][(i,j)] = 0.0
reward = -0.005
actions_dict = q_values.get(new_state, {})
if len(actions_dict) == 0:
max_value = 0.0
else:
action, max_value = max(actions_dict.items(), key=lambda x: x[1])
q_values[last_state][(row, col)] = q_values[last_state][(row, col)] + learning_rate*(reward + discount_factor*(max_value) - q_values[last_state][(row, col)])
to_move = 'X'
winner = check_win(board)
if winner is not None:
if winner == "X":
reward = -1
x_q_values[x_last_state][(row1, col1)] = x_q_values[x_last_state][(row1, col1)] + x_learning_rate*(1 - x_q_values[x_last_state][(row1, col1)])
q_values[last_state][(row, col)] = q_values[last_state][(row, col)] + learning_rate*(reward - q_values[last_state][(row, col)])
loss_count += 1
elif winner == "O":
reward = 1
x_q_values[x_last_state][(row1, col1)] = x_q_values[x_last_state][(row1, col1)] + x_learning_rate*(-1 - x_q_values[x_last_state][(row1, col1)])
q_values[last_state][(row, col)] = q_values[last_state][(row, col)] + learning_rate*(reward - q_values[last_state][(row, col)])
win_count += 1
else:
reward = 0
x_q_values[x_last_state][(row1, col1)] = x_q_values[x_last_state][(row1, col1)] + x_learning_rate*(0 - x_q_values[x_last_state][(row1, col1)])
q_values[last_state][(row, col)] = q_values[last_state][(row, col)] + learning_rate*(reward - q_values[last_state][(row, col)])
stalemate_count += 1
game_finished = True
# 0.9999 Seems to yield the best results
epsilon_greedy = max(epsilon_greedy * 0.99, 0.05)
count += 1
pbar.update(1)
# if count % 100 == 0:
# current_win_rate = win_count / count
# win_rate_history.append(current_win_rate)
# game_intervals.append(count)
# print_q_value(q_values)
# print(f'At {count} games, the current stats are:')
# print(f'Wins: {win_count}')
# print(f'Losses: {loss_count}')
# print(f'Stalemate: {stalemate_count}')
# print(f'Current epsilon value: {epsilon_greedy}')
# print(f'Win rate is {current_win_rate * 100}%')
pbar.close()
print(f'=========== Training Results ===========')
print(f'At {count} games, the current stats are:')
print(f'Wins: {win_count}')
print(f'Losses: {loss_count}')
print(f'Stalemate: {stalemate_count}')
print(f'Current epsilon value: {epsilon_greedy}')
print(f'Win rate is {(win_count/count) * 100}%')
# print_q_value(q_values)
play_count = 0
play_win_count = 0
play_loss_count = 0
play_stalemate_count = 0
epsilon_greedy = 0.4
win_rate_history = []
game_intervals = []
game_finished = True
pygame.init()
WIDTH, HEIGHT = 900, 900
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Tic Tac Toe!")
BOARD = pygame.image.load("assets/Board.png")
X_IMG = pygame.image.load("assets/X.png")
O_IMG = pygame.image.load("assets/O.png")
BG_COLOR = (214, 201, 227)
SCREEN.fill(BG_COLOR)
SCREEN.blit(BOARD, (64, 64))
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
if play_count != 0:
print(f'At {play_count} games, the current stats are:')
print(f'Wins: {play_win_count}')
print(f'Losses: {play_loss_count}')
print(f'Stalemate: {play_stalemate_count}')
print(f'Current epsilon value: {epsilon_greedy}')
print(f'Win rate is {(play_win_count/play_count) * 100}%')
# print_q_value(q_values)
plt.figure(figsize=(10, 6))
plt.plot(game_intervals, win_rate_history, label="Win Rate", color='blue')
plt.title("Win Rate Over Time")
plt.xlabel("Number of Games")
plt.ylabel("Win Rate")
plt.grid(True)
plt.legend()
plt.show()
else:
print("You have not played yet!")
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
# (1) The user is about to place X if it's X's turn
# If game is finished, do resets
if game_finished:
board = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
graphical_board = [
[[None, None], [None, None], [None, None]],
[[None, None], [None, None], [None, None]],
[[None, None], [None, None], [None, None]]
]
logical_board = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
]
to_move = player_choice
SCREEN.fill(BG_COLOR)
SCREEN.blit(BOARD, (64, 64))
game_finished = False
pygame.display.update()
if to_move == "X":
add_XO(board, graphical_board, "X", logical_board)
render_board(board, X_IMG, O_IMG)
for i in range(3):
for j in range(3):
if graphical_board[i][j][0] is not None:
SCREEN.blit(graphical_board[i][j][0], graphical_board[i][j][1])
pygame.display.update()
to_move = "O"
# The reason there has to be an else is that it should check after every move if there is a winner
# For example, if X (you) moves last and you win, O will still go despite the game being over and then O will win
# Since the terminal states are checked after O in the code, even though your move (X) should've ended the game
else:
last_state = tuple(tuple(row) for row in logical_board)
for i in range(3):
for j in range(3):
if logical_board[i][j] == 0:
if (i,j) not in q_values[last_state]:
q_values[last_state][(i,j)] = 0.0
if random.random() < epsilon_greedy:
row, col = random.choice(get_empty_spots(logical_board))
print(f"We chose to explore: ({row, col})")
else:
# print_state_q_values(q_values, last_state)
max_action = max(q_values.get(last_state, {}).items(), key=lambda x: x[1])
action, max_value = max_action
print_state_q_values(q_values, last_state)
print(f'We chose {action} with value: {max_value}')
row, col = action
place_O(board, logical_board, (row,col))
render_board(board, X_IMG, O_IMG)
for i in range(3):
for j in range(3):
if graphical_board[i][j][0] is not None:
SCREEN.blit(graphical_board[i][j][0], graphical_board[i][j][1])
pygame.display.update()
new_state = tuple(tuple(row) for row in logical_board)
for i in range(3):
for j in range(3):
if logical_board[i][j] == 0:
if (i,j) not in q_values[new_state]:
q_values[new_state][(i,j)] = 0.0
reward = 0.0
actions_dict = q_values.get(new_state, {})
if len(actions_dict) == 0:
max_value = 0.0
else:
action, max_value = max(actions_dict.items(), key=lambda x: x[1])
q_values[last_state][(row, col)] = q_values[last_state][(row, col)] + learning_rate*(reward + discount_factor*(max_value) - q_values[last_state][(row, col)])
to_move = 'X'
winner = check_win_update(board)
if winner is not None:
if winner == "X":
reward = -1
q_values[last_state][(row, col)] = q_values[last_state][(row, col)] + learning_rate*(reward - q_values[last_state][(row, col)])
play_loss_count += 1
elif winner == "O":
reward = 1
q_values[last_state][(row, col)] = q_values[last_state][(row, col)] + learning_rate*(reward - q_values[last_state][(row, col)])
play_win_count += 1
else:
reward = 0
q_values[last_state][(row, col)] = q_values[last_state][(row, col)] + learning_rate*(reward - q_values[last_state][(row, col)])
play_stalemate_count += 1
game_finished = True
# 0.9999 Seems to yield the best results
epsilon_greedy = max(epsilon_greedy * 0.93, 0.1)
play_count += 1
win_rate = play_win_count / play_count
win_rate_history.append(win_rate)
game_intervals.append(play_count)
if play_count % 3 == 0:
print(f'At {play_count} games, the current stats are:')
print(f'Wins: {play_win_count}')
print(f'Losses: {play_loss_count}')
print(f'Stalemate: {play_stalemate_count}')
print(f'Current epsilon value: {epsilon_greedy}')
print(f'Win rate is {win_rate * 100}%')