-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patht.py
386 lines (315 loc) · 9.93 KB
/
t.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
"""
Quarto
0: vacant
1-16: occupied
"""
import numpy as np
from collections import Counter
def board_to_int(board):
s = 0L
for i in range(16):
s += long(board[i]) * (17 ** i)
return s
def board_to_possible_hands(board):
return [i for i in range(16) if board[i] == 0]
def init_board():
return np.zeros(16, dtype=np.int)
def init_Q():
from scipy.sparse import dok_matrix
return dok_matrix((17 ** 16, 16 * 16))
LINES = [
[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15],
[0, 4, 8, 12], [1, 5, 9, 13], [2, 6, 10, 14], [3, 7, 11, 15],
[0, 5, 10, 15], [3, 6, 9, 12]
]
def is_win(board):
for line in LINES:
xs = board[line]
if any(x == 0 for x in xs): continue
a, b, c, d = xs - 1
if a & b & c & d != 0:
return 1
if a | b | c | d != 15:
return 1
return 0
def print_board(board):
"""
>>> print_board(range(16))
. o x o | . o o x | . o o o | . o o o
x o x o | x o o x | o x x x | o o o o
x o x o | x o o x | x o o o | o x x x
x o x o | x o o x | o x x x | x x x x
"""
m = np.zeros((16, 4), dtype=np.int)
for i in range(16):
if board[i] == 0:
m[i, :] = 0
else:
v = board[i] - 1
for bit in range(4): # nth bit
m[i, bit] = ((v >> bit) & 1) + 1
for y in range(4):
print ' | '.join(
' '.join(
['.ox'[v] for v in m[y * 4 : (y + 1) * 4, bit]]
)
for bit in range(4))
print
def policy_random(env):
from random import choice
position = choice(board_to_possible_hands(env.board))
piece = choice(env.available_pieces)
return (position, piece)
class Environment(object):
def __init__(self, policy=policy_random):
self.op_policy = policy
self.result_log =[]
self.init_env()
def init_env(self):
self.board = init_board()
self.available_pieces= range(2, 17)
self.selected_piece = 1
def _update(self, action, k=1, to_print=False):
position, piece = action
if self.board[position] != 0:
# illegal move
print 'illegal pos'
self.init_env()
self.result_log.append(-1 * k)
return (self.board, -1 * k)
if piece not in self.available_pieces:
# illegal move
print 'illegal piece'
self.init_env()
self.result_log.append(-1 * k)
return (self.board, -1 * k)
self.board[position] = self.selected_piece
self.available_pieces.remove(piece)
self.selected_piece = piece
if to_print:
print k, action
print_board(self.board)
b = is_win(self.board)
if b:
self.init_env()
self.result_log.append(+1 * k)
return (self.board, +1 * k)
if not self.available_pieces:
# put selected piece
self.board[self.board==0] = self.selected_piece
b = is_win(self.board)
if to_print:
print 'last move'
print_board(self.board)
self.init_env()
if b:
# opponent win
self.result_log.append(-1 * k)
return (self.board, -1 * k)
else:
# tie
self.result_log.append(0)
return (self.board, -1)
return None
def __call__(self, action, to_print=False):
ret = self._update(action, k=1, to_print=to_print)
if ret: return ret
op_action = self.op_policy(self)
ret = self._update(op_action, k=-1, to_print=to_print)
if ret: return ret
return (self.board, 0)
def play(policy1, policy2=policy_random, to_print=False):
env = Environment()
result = 0
for i in range(9):
a = policy1(env)
s, r = env(a, to_print=to_print)
if r != 0: break
if to_print:
print env.result_log[-1]
return env.result_log[-1]
#play(policy_random, to_print=True)
class Greedy(object):
def __init__(self):
self.Qtable = init_Q()
def __call__(self, env):
from random import choice
s = board_to_int(env.board)
actions = (action_to_int((pos, piece))
for pos in board_to_possible_hands(env.board)
for piece in env.available_pieces
)
qa = [(self.Qtable[s, a], a) for a in actions]
bestQ, bestA = max(qa)
bextQ, bestA = choice([(q, a) for (q, a) in qa if q == bestQ])
return int_to_action(bestA)
class EpsilonGreedy(object):
def __init__(self, eps=0.1):
self.Qtable = init_Q()
self.eps = eps
def __call__(self, env):
from random import choice, random
s = board_to_int(env.board)
if random() < self.eps:
pos = choice(board_to_possible_hands(env.board))
piece = choice(env.available_pieces)
return (pos, piece)
actions = (action_to_int((pos, piece))
for pos in board_to_possible_hands(env.board)
for piece in env.available_pieces
)
qa = [(self.Qtable[s, a], a) for a in actions]
bestQ, bestA = max(qa)
bextQ, bestA = choice([(q, a) for (q, a) in qa if q == bestQ])
return int_to_action(bestA)
def board_to_state(board):
return board_to_int(board)
def action_to_int(action):
pos, piece = action
return pos * 16 + (piece - 1)
def int_to_action(i):
assert 0 <= i < 16 * 16
return (i / 16, i % 16 + 1)
from kagura.utils import Digest
digest = Digest(1)
battle_per_seconds = []
def sarsa(alpha, policyClass=Greedy):
global environment, policy
gamma = 0.9
num_result = batch_width * num_batch
environment = Environment()
policy = policyClass()
action = policy(environment)
state = board_to_state(environment.board)
while True:
next_board, reward = environment(action)
next_state = board_to_state(next_board)
# determine a'
next_action = policy(environment)
nextQ = policy.Qtable[next_state, action_to_int(next_action)]
# update Q(s, a)
s_a = (state, action_to_int(action))
Qsa = policy.Qtable[s_a]
estimated_reward = reward + gamma * nextQ
diff = estimated_reward - Qsa
policy.Qtable[s_a] += alpha * diff
state = next_state
action = next_action
if len(environment.result_log) == num_result:
break
t = digest.digest(len(environment.result_log))
if t:
battle_per_seconds.append(t)
vs = []
for i in range(num_batch):
c = Counter(environment.result_log[batch_width * i : batch_width * (i + 1)])
print c
vs.append(float(c[1]) / batch_width)
return vs
def qlearn(alpha, policyClass=Greedy):
global environment, policy
gamma = 0.9
num_result = batch_width * num_batch
environment = Environment()
policy = policyClass()
state = board_to_state(environment.board)
while True:
action = policy(environment)
next_board, reward = environment(action)
next_state = board_to_state(next_board)
# update Q(s, a)
maxQ = max(policy.Qtable[next_state, a] for a in board_to_possible_hands(next_board))
s_a = (state, action_to_int(action))
Qsa = policy.Qtable[s_a]
estimated_reward = reward + gamma * maxQ
diff = estimated_reward - Qsa
policy.Qtable[s_a] += alpha * diff
state = next_state
if len(environment.result_log) == num_result:
break
t = digest.digest(len(environment.result_log))
if t:
battle_per_seconds.append(t)
vs = []
for i in range(num_batch):
c = Counter(environment.result_log[batch_width * i : batch_width * (i + 1)])
print c
vs.append(float(c[1]) / batch_width)
return vs
def plot_log():
from kagura import load
result_log = load("sarsa_0.05_result_log")
batch_width = 1000
num_batch = 1000
vs = []
for i in range(num_batch):
c = Counter(result_log[batch_width * i : batch_width * (i + 1)])
print c
vs.append(float(c[1]) / batch_width)
label = 'Sarsa(0.05)'
imgname = 'sarsa_0.05.png'
plot()
def plot():
import matplotlib.pyplot as plt
plt.clf()
plt.plot([0.475] * len(vs), label = "baseline")
plt.plot(vs, label=label)
plt.xlabel("iteration")
plt.ylabel("Prob. of win")
plt.legend(loc = 4)
plt.savefig(imgname)
def f(n, m):
if m == 1: return n + 1
return n * f(n - 1, m - 1) + f(n, m - 1)
if not'ex1':
from collections import Counter
print Counter(
play(policy_random) for i in range(10000))
elif not'ex2':
batch_width = 1000
num_batch = 100
vs = sarsa(0.5)
elif not'ex3':
batch_width = 1000
num_batch = 1000
vs = sarsa(0.5)
if 0:
batch_width = 1000
num_batch = 1000
vs = qlearn(0.5)
label = 'Qlearn(0.5)'
imgname = 'qlearn.png'
elif 0:
batch_width = 1000
num_batch = 1000
vs = qlearn(0.05)
label = 'Qlearn(0.05)'
imgname = 'qlearn_0.05.png'
from kagura import dump
if 0:
batch_width = 1000
num_batch = 1000
vs = sarsa(0.5, policyClass=EpsilonGreedy)
label = 'Sarsa(0.5, eps=0.1)'
imgname = 'sarsa_0.5_eps0.1.png'
dump(environment.result_log, imgname.replace('.png', '_result_log'))
elif 0:
batch_width = 1000
num_batch = 1000
vs = sarsa(0.05, policyClass=EpsilonGreedy)
label = 'Sarsa(0.05, eps=0.1)'
imgname = 'sarsa_0.05_eps0.1.png'
dump(environment.result_log, imgname.replace('.png', '_result_log'))
if 0:
batch_width = 100
num_batch = 1000
vs = sarsa(0.05, policyClass=Greedy)
label = 'Sarsa(0.05)'
imgname = 'sarsa_0.05_2.png'
dump(environment.result_log, imgname.replace('.png', '_result_log'))
batch_width = 1000
num_batch = 100
vs = sarsa(0.5)
label = 'Sarsa(0.5)'
imgname = 'sarsa_0.5_2.png'
plot()