-
Notifications
You must be signed in to change notification settings - Fork 8
/
tictactoe_rl.py
164 lines (126 loc) · 4.64 KB
/
tictactoe_rl.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
import random
class TicTacToe:
def __init__(self, playerX, playerO):
self.board = [' '] * 9
self.playerX, self.playerO = playerX, playerO
self.playerX_turn = True
def display_board(self):
print ' | | '
print ' %s | %s | %s ' % (self.board[0],
self.board[1],
self.board[2])
print '_____|_____|_____'
print ' | | '
print ' %s | %s | %s ' % (self.board[3],
self.board[4],
self.board[5])
print '_____|_____|_____'
print ' | | '
print ' %s | %s | %s ' % (self.board[6],
self.board[7],
self.board[8])
print ' | | '
def board_full(self):
return not any([space == ' ' for space in self.board])
def player_wins(self, char):
for a, b, c in [(0, 1, 2), (3, 4, 5), (6, 7, 8),
(0, 3, 6), (1, 4, 7), (2, 5, 8),
(0, 4, 8), (2, 4, 6)]:
if char == self.board[a] == self.board[b] == self.board[c]:
return True
return False
def play_game(self, train=True):
if not train:
print '\nNew game!'
self.playerX.start_game()
self.playerO.start_game()
while True:
if self.playerX_turn:
player, char, other_player = self.playerX, 'X', self.playerO
else:
player, char, other_player = self.playerO, 'O', self.playerX
if player.breed == "human":
self.display_board()
move = player.move(self.board)
self.board[move - 1] = char
if self.player_wins(char):
player.reward(1, self.board)
other_player.reward(-1, self.board)
if not train:
self.display_board()
print char + ' wins!'
break
if self.board_full():
player.reward(0.5, self.board)
other_player.reward(0.5, self.board)
if not train:
self.display_board()
print 'Draw!'
break
other_player.reward(0, self.board)
self.playerX_turn = not self.playerX_turn
class Player(object):
def __init__(self):
self.breed = 'human'
def start_game(self):
pass
def move(self, board):
return int(raw_input('Your move? '))
def available_moves(self, board):
return [i + 1 for i in range(0, 9) if board[i] == ' ']
def reward(self, value, board):
pass
class QLearningPlayer(Player):
def __init__(self):
self.breed = 'qlearner'
self.q = {}
self.epsilon = 0.2
self.alpha = 0.1
self.gamma = 0.9
self.last_state = (' ',) * 9
self.last_move = None
def start_game(self):
self.last_state = (' ',) * 9
self.last_move = None
def getQ(self, state, action):
if self.q.get((state, action)) is None:
self.q[(state, action)] = 1.0
return self.q.get((state, action))
def move(self, board):
actions = self.available_moves(board)
if random.random() < self.epsilon:
self.last_move = random.choice(actions)
self.last_state = tuple(board)
return self.last_move
qs = [self.getQ(self.last_state, each) for each in actions]
maxQ = max(qs)
if qs.count(maxQ) > 1:
best_options = [i for i in range(len(actions)) if qs[i] == maxQ]
i = random.choice(best_options)
else:
i = qs.index(maxQ)
self.last_move = actions[i]
self.last_state = tuple(board)
return self.last_move
def reward(self, value, board):
if self.last_move:
self.learn(self.last_state, self.last_move, value, tuple(board))
def learn(self, state, action, reward, result_state):
prev = self.getQ(state, action)
maxqnew = max(
[self.getQ(result_state, a) for a in self.available_moves(state)]
)
self.q[(state, action)] = prev + \
self.alpha * ((reward + self.gamma * maxqnew) - prev)
p1 = QLearningPlayer()
p2 = QLearningPlayer()
for i in xrange(0, 20000):
t = TicTacToe(p1, p2)
t.play_game()
p1.epsilon = 0
p2 = Player()
while True:
t = TicTacToe(p1, p2)
t.play_game(train=False)
for each in p1.q:
print each, p1.q[each]