-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoe.py
287 lines (237 loc) · 9.53 KB
/
tictactoe.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
from pprint import pprint
import numpy as np
class TicTacToe:
def __init__(self):
self.board = np.zeros((3, 3), dtype=int) # 0: empty, 1: 'X', -1: 'O'
self.current_player = 1 # Player 1 starts
def available_actions(self):
return [(i, j) for i in range(3) for j in range(3) if self.board[i, j] == 0]
def make_move(self, action):
if self.board[action] == 0:
self.board[action] = self.current_player
self.current_player *= -1 # Switch player
return True
return False
def make_random_move(self):
available_actions = self.available_actions()
random_action = available_actions[np.random.randint(len(available_actions))]
self.make_move(random_action)
def check_winner(self):
for i in range(3):
if abs(sum(self.board[i, :])) == 3:
return self.board[i, 0]
elif abs(sum(self.board[:, i])) == 3:
return self.board[0, i]
if abs(sum([self.board[i, i] for i in range(3)])) == 3 or abs(sum([self.board[i, 2 - i] for i in range(3)])) == 3:
return self.board[1, 1]
if not any(0 in row for row in self.board):
return 0 # Draw
return None # Game continues
def reset(self):
self.board = np.zeros((3, 3), dtype=int)
self.current_player = 1
class QLearningAgent:
def __init__(self, learning_rate=0.2, discount_factor=1, epsilon=1, epsilon_decay=0.99):
self.q_table = {} # Initialize Q-table
self.learning_rate = learning_rate
self.discount_factor = discount_factor
self.epsilon = epsilon
self.epsilon_decay = epsilon_decay
def get_q_value(self, state, action):
return self.q_table.get((tuple(map(tuple, state)), action), 0)
def choose_action(self, state, available_actions):
if np.random.rand() < self.epsilon:
return available_actions[np.random.choice(len(available_actions))]
q_values = [self.get_q_value(state, action) for action in available_actions]
max_q = max(q_values)
actions_with_max_q = [action for action, q in zip(available_actions, q_values) if q == max_q]
return actions_with_max_q[np.random.choice(len(actions_with_max_q))]
def update_q_table(self, old_state, action, reward, new_state):
old_q = self.get_q_value(old_state, action)
max_future_q = max([self.get_q_value(new_state, a) for a in TicTacToe().available_actions()], default=0)
new_q = old_q + self.learning_rate * (reward + self.discount_factor * max_future_q - old_q)
self.q_table[(tuple(map(tuple, old_state)), action)] = new_q
def update_epsilon(self):
if self.epsilon * self.epsilon_decay > 0.25:
self.epsilon *= self.epsilon_decay
def export_qtable(self):
with open('qtable', 'w') as file:
pprint(self.q_table, stream=file)
def import_qtable(self, qtable_file):
with open(qtable_file, 'r') as file:
self.q_table = eval(file.read())
def train_agent_o(agent, episodes=10000):
for episode in range(episodes):
game = TicTacToe()
while True:
old_state = np.copy(game.board)
action = agent.choose_action(old_state, game.available_actions())
game.make_move(action)
winner = game.check_winner()
if winner is not None:
# Game ended, update Q-table
reward = -1
if winner == 1:
reward = 1
agent.q_table[(tuple(map(tuple, old_state)), action)] = reward
break
else:
game.make_random_move()
winner = game.check_winner()
reward = 0
if winner == -1:
reward = -1
new_state = np.copy(game.board)
agent.update_q_table(old_state, action, reward, new_state)
agent.update_epsilon() # Decay epsilon after each episode
return agent
def train_agent_x(agent, episodes=10000):
for episode in range(episodes):
game = TicTacToe()
game.make_random_move()
while True:
old_state = np.copy(game.board)
action = agent.choose_action(old_state, game.available_actions())
game.make_move(action)
winner = game.check_winner()
if winner is not None:
if winner == -1:
reward = 1
elif winner == 0:
reward = 0.5
else:
reward = -1
agent.q_table[(tuple(map(tuple, old_state)), action)] = reward
break
else:
game.make_random_move()
winner = game.check_winner()
if winner == 1:
reward = -1
elif winner == 0:
reward = 0.5
else:
reward = 0
new_state = np.copy(game.board)
agent.update_q_table(old_state, action, reward, new_state)
if winner is not None:
break
agent.update_epsilon() # Decay epsilon after each episode
return agent
def train(agent, episodes=10000):
train_agent_o(agent, episodes//2)
train_agent_x(agent, episodes//2)
def test_agent_o(agent, test_episodes=1000):
agent.epsilon = 0
win_count = 0
draw_count = 0
for _ in range(test_episodes):
game = TicTacToe()
while True:
if game.current_player == 1:
action = agent.choose_action(game.board, game.available_actions())
game.make_move(action)
else:
game.make_random_move()
winner = game.check_winner()
if winner is not None:
if winner == 1:
win_count += 1
elif winner == 0: # Draw
draw_count += 1
break
win_rate = win_count / test_episodes
draw_rate = draw_count / test_episodes
loss_rate = 1 - (win_rate + draw_rate)
print("Agent as O against random:")
print(f"Win Rate: {win_rate:.2f}, Draw Rate: {draw_rate:.2f}, Loss Rate: {loss_rate:.2f}")
print()
def test_agent_x(agent, test_episodes=1000):
agent.epsilon = 0
win_count = 0
draw_count = 0
for _ in range(test_episodes):
game = TicTacToe()
while True:
if game.current_player == 1:
game.make_random_move()
else:
action = agent.choose_action(game.board, game.available_actions())
game.make_move(action)
winner = game.check_winner()
if winner is not None:
if winner == -1:
win_count += 1
elif winner == 0:
draw_count += 1
break
win_rate = win_count / test_episodes
draw_rate = draw_count / test_episodes
loss_rate = 1 - (win_rate + draw_rate)
print("Agent as X against random:")
print(f"Win Rate: {win_rate:.2f}, Draw Rate: {draw_rate:.2f}, Loss Rate: {loss_rate:.2f}")
print()
def test_agents(agent_o, agent_x, test_episodes=1000):
agent_o.epsilon = 0
agent_x.epsilon = 0
win_count = 0
draw_count = 0
for _ in range(test_episodes):
game = TicTacToe()
while True:
if game.current_player == 1:
action = agent_o.choose_action(game.board, game.available_actions())
game.make_move(action)
else:
action = agent_x.choose_action(game.board, game.available_actions())
game.make_move(action)
winner = game.check_winner()
if winner is not None:
if winner == 1:
win_count += 1
elif winner == 0: # Draw
draw_count += 1
break
win_rate_o = win_count / test_episodes
draw_rate = draw_count / test_episodes
win_rate_x = 1 - (win_rate_o + draw_rate)
print("Agent aginst itself:")
print(f"Win Rate (O): {win_rate_o:.2f}, Win Rate (X): {win_rate_x:.2f}, Draw Rate: {draw_rate:.2f}")
print()
def get_user_move():
user_input = input("Enter the row and column indices separated by a comma (e.g., 1,2): ")
row_index, col_index = map(int, user_input.strip().split(','))
return row_index, col_index
def make_user_move(game):
user_move = get_user_move()
while user_move not in game.available_actions():
user_move = get_user_move()
game.make_move(user_move)
def play(agent, agent_type):
agent.epsilon = 0
game = TicTacToe()
while True:
if agent_type == -1:
print(game.board)
make_user_move(game)
while game.check_winner() == None:
if game.current_player == agent_type*-1:
print(game.board)
make_user_move(game)
else:
state = np.copy(game.board)
action = agent.choose_action(state, game.available_actions())
game.make_move(action)
if game.check_winner() == agent_type*-1:
print("You won!!!")
elif game.check_winner() == agent_type:
print("You lost!!!")
elif game.check_winner() == 0:
print("Tie!!!")
game.reset()
if __name__ == '__main__':
agent = QLearningAgent()
agent.import_qtable('qtable')
test_agent_o(agent, 10000)
test_agent_x(agent, 10000)
test_agents(agent, agent, 10000)