-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel.py
489 lines (430 loc) · 16.7 KB
/
kernel.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
# core data
# search algorithm in this file
from board import Tiles, Board
import shape
import random
import copy
import numpy as np
DecisionFunc = [] # the list of search functions
EvalFunc = [] # the list of evaluation functions
evalWeight = [20, 10]
# weights of greedyEval
minimaxDepth = 2
# depth = n means search n rounds
'''
All search functions have the same format
of arguments and return values.
Arguments:
board: class Board. The current board.
player: class Player. The current player.
opponent: class Player. The opponent of the current player.
**info: nothing
Return values:
list [
the type of tile,
rotation,
flip, (rotation and flip infer the shape of tile)
x,
y (x and y represent the position of tile)
]
If none of tile can be dropped, the functions return
[-1, 0, 0, 0, 0]
'''
def greedyEval(board, player, opponent, **info):
'''
Evaluation function.
score = player.score * w1
+ the difference of valid corners
between the player and opponents * w2
'''
def validCornerNumber(player):
return board.getCorners(player)[0].size
score = (player.score - opponent.score) * evalWeight[0]
cor = validCornerNumber(player) - validCornerNumber(opponent)
score += cor * evalWeight[1]
score += random.random()
# avoid the same choice
return score
EvalFunc.append(greedyEval)
def mctsEval(board, player, opponent, **info):
score = 0
rev = False
tot = 6
stp = 4
if 'setTot' in info:
tot = info['setTot']
if 'setReverse' in info:
rev = info['setReverse']
if 'setStep' in info:
stp = info['setStep']
tmpDecMaker = [player.decisionMaker, opponent.decisionMaker]
player.decisionMaker = opponent.decisionMaker = randomRandom
tmpPlayer = player
tmpOpponent = opponent
if rev:
tmpPlayer = opponent
tmpOpponent = player
initScore = greedyEval(board, tmpPlayer, tmpOpponent)
for game in range(tot):
tmpHistory = []
flag = False
for step in range(stp):
flag = False
result = tmpPlayer.action(board, tmpOpponent, setEvalFunc = 0)
if result['action']:
flag = True
result['id'] = 0
tmpHistory.append(result)
result = tmpOpponent.action(board, tmpPlayer, setEvalFunc = 0)
if result['action']:
flag = True
result['id'] = 1
tmpHistory.append(result)
if not flag:
break
if flag:
if greedyEval(board, tmpPlayer, tmpOpponent) > initScore:
score += 1
else:
if tmpPlayer.score > tmpOpponent.score:
score += 1
for h in tmpHistory:
if h['id'] == 0:
tmpPlayer.used[h['tileType']] = False
tmpPlayer.score -= shape.tileSizes[h['tileType']]
else:
tmpOpponent.used[h['tileType']] = False
tmpOpponent.score -= shape.tileSizes[h['tileType']]
board.retraceDrop(Tiles(h['tileType'], h['rotation'], h['flip']), h['x'], h['y'])
player.decisionMaker, opponent.decisionMaker = tmpDecMaker
return score / tot if not rev else 1 - score / tot
EvalFunc.append(mctsEval)
def randomRandom(board, player, opponent, **info):
'''
Purely random
'''
tileList = np.where(player.used == False)[0]
used = np.zeros(21, dtype = bool)
cnt = 0
while cnt < tileList.size:
t = np.random.choice(tileList)
while used[t]:
t = np.random.choice(tileList)
direction = np.random.permutation(shape.tileMaxRotation[t])
for d in direction:
f = 0
tile = Tiles(t, d, f)
possiblePos = board.canDropPos(player, tile)
if possiblePos[0].size != 0:
i = np.random.choice(possiblePos[0].size)
x, y = possiblePos[0][i], possiblePos[1][i]
return [t, d, f, x, y]
if shape.tileMaxFlip[t]:
f = 1
tile = Tiles(t, d, f)
possiblePos = board.canDropPos(player, tile)
if possiblePos[0].size != 0:
i = np.random.choice(possiblePos[0].size)
x, y = possiblePos[0][i], possiblePos[1][i]
return [t, d, f, x, y]
used[t] = True
cnt += 1
return [-1, 0, 0, 0, 0]
DecisionFunc.append(randomRandom)
def randomGreedy(board, player, opponent, **info):
'''
Randomly choose one of the biggest tile
which can be dropped and drop it on a
random legal position.
'''
remain = []
nowSize = 0
for i in range(21):
if not player.used[i]:
if nowSize == shape.tileSizes[i]:
remain[nowSize - 1].append(i)
else:
remain.append([])
nowSize = nowSize + 1
remain[nowSize - 1].append(i)
for size in range(5, 0, -1):
if size > len(remain):
continue
random.shuffle(remain[size - 1])
for i in range(len(remain[size - 1])):
t = remain[size - 1][i]
direction = [i for i in range(shape.tileMaxRotation[t])]
random.shuffle(direction)
for k in direction:
f = 0
tile = Tiles(t, k, f)
xlist, ylist = board.canDropPos(player, tile)
if xlist.size != 0:
j = np.random.choice(xlist.size)
return [
t, # type of tile
k, # rotation
f, # flip
xlist[j], # x
ylist[j] # y
]
if shape.tileMaxFlip[t]:
f = 1
tile = Tiles(t, k, f)
xlist, ylist = board.canDropPos(player, tile)
if xlist.size == 0:
continue
j = np.random.choice(xlist.size)
return [
t, # type of tile
k, # rotation
f, # flip
xlist[j], # x
ylist[j] # y
]
return [-1, 0, 0, 0, 0]
DecisionFunc.append(randomGreedy)
def greedy(board, player, opponent, evalFunc = 0, **info):
'''
Enumerate all possible drops, and get a score
using EvalFunc[evalFunc]
'''
if 'setEvalFunc' in info:
evalFunc = info['setEvalFunc']
global evalWeight
if 'setEvalWeight' in info:
evalWeight = info['setEvalWeight']
maxScore = -32768
maxDecision = []
remain = np.where(player.used == False)[0]
if remain.size == 0:
return [-1, 0, 0, 0, 0]
remain = remain[::-1]
for i in remain:
for p in range(shape.tileMaxRotation[i]):
f = [0]
if shape.tileMaxFlip[i]:
f.append(1)
for q in f:
tile = Tiles(i, p, q)
xlist, ylist = board.canDropPos(player, tile)
if xlist.size == 0:
continue
for k in range(xlist.size):
x = xlist[k]
y = ylist[k]
result = board.dropTile(player, tile, x, y, False)
if result:
player.score += tile.size
player.used[tile.type] = True
score = EvalFunc[evalFunc](board, player, opponent, setReverse = True)
if score > maxScore:
maxScore = score
maxDecision = [
i, # type of tile
p, # rotation
q, # flip
x, # x
y # y
]
board.retraceDrop(tile, x, y)
player.used[tile.type] = False
player.score -= tile.size
if maxScore > -32768:
return maxDecision
else:
return [-1, 0, 0, 0, 0]
DecisionFunc.append(greedy)
def _alphaBeta(depth, board, player, opponent, evalFunc, alpha, beta, desPlayer):
'''
Internal recursive alphabeta pruning
'''
if depth == minimaxDepth:
return EvalFunc[evalFunc](board, player, opponent)
bestMove = [-1, 0, 0, 0, 0]
remain = np.where(player.used == False)[0]
remain = remain[::-1]
#reverse the array to search in a specific order
for i in remain:
for p in range(shape.tileMaxRotation[i]):
f = [0]
if shape.tileMaxFlip[i]:
f.append(1)
for q in f:
tile = Tiles(i, p, q)
xlist, ylist = board.canDropPos(player, tile)
if xlist.size == 0:
continue
for k in range(xlist.size):
x = xlist[k]
y = ylist[k]
result = board.dropTile(player, tile, x, y, False)
if result:
player.score += tile.size
score = -_alphaBeta(depth + 1, board, opponent, player, evalFunc, -beta, -alpha, desPlayer)
board.retraceDrop(tile, x, y)
player.score -= tile.size
if score >= alpha:
alpha = score
if depth == 0:
bestMove = [i, p, q, x, y]
if alpha >= beta:
return [alpha, bestMove]
else:
if alpha >= beta:
return alpha
return alpha if depth != 0 else [alpha, bestMove]
def alphaBeta(board, player, opponent, evalFunc = 0, **info):
'''
Alphabeta pruning
'''
if 'setEvalFunc' in info:
evalFunc = info['setEvalFunc']
global evalWeight
if 'setEvalWeight' in info:
evalWeight = info['setEvalWeight']
alpha = -32768
beta = 32767
bestMove = _alphaBeta(0, board, player, opponent, evalFunc, alpha, beta, player.order)
return bestMove[1]
DecisionFunc.append(alphaBeta)
def mcts(board, player, opponent, evalFunc = 0, **info):
'''
Enumerate all possible drops and select the best
5 of them, then use mcstEval to reevaluate them
'''
if 'setEvalFunc' in info:
evalFunc = info['setEvalFunc']
global evalWeight
if 'setEvalWeight' in info:
evalWeight = info['setEvalWeight']
totGame = 80
if 'setTotalGame' in info:
totGame = info['setTotalGame']
maxDecision = []
remain = np.where(player.used == False)[0]
for i in remain:
for p in range(shape.tileMaxRotation[i]):
f = [0]
if shape.tileMaxFlip[i]:
f.append(1)
for q in f:
tile = Tiles(i, p, q)
xlist, ylist = board.canDropPos(player, tile)
if xlist.size == 0:
continue
for k in range(xlist.size):
x = xlist[k]
y = ylist[k]
board.dropTile(player, tile, x, y, False)
player.score += tile.size
player.used[tile.type] = True
score = EvalFunc[evalFunc](board, player, opponent, setReverse = True)
if len(maxDecision) < 5:
maxDecision.append({
'score' : score,
'tileType' : i, # type of tile
'rot' : p, # rotation
'flip' : q, # flip
'x' : x, # x
'y' : y # y
})
elif score > maxDecision[-1]['score']:
m = 4
while m > 0:
if maxDecision[m]['score'] > score:
break
maxDecision[m] = maxDecision[m - 1]
m -= 1
maxDecision[m] = {
'score' : score,
'tileType' : i, # type of tile
'rot' : p, # rotation
'flip' : q, # flip
'x' : x, # x
'y' : y # y
}
board.retraceDrop(tile, x, y)
player.used[tile.type] = False
player.score -= tile.size
if maxDecision != []:
maxscore = -1
maxd = -1
for i, dec in enumerate(maxDecision):
tile = Tiles(dec['tileType'], dec['rot'], dec['flip'])
board.dropTile(player, tile, dec['x'], dec['y'], False)
player.used[dec['tileType']] = True
score = mctsEval(board, player, opponent, setTot = totGame, setReverse = True)
if score > maxscore:
maxd = i
board.retraceDrop(tile, dec['x'], dec['y'])
player.used[dec['tileType']] = False
return [
maxDecision[maxd]['tileType'],
maxDecision[maxd]['rot'],
maxDecision[maxd]['flip'],
maxDecision[maxd]['x'],
maxDecision[maxd]['y']
]
else:
return [-1, 0, 0, 0, 0]
DecisionFunc.append(mcts)
def analBoard(board, player, opponent, **info):
'''
return the best 5 drops and their winning rate
'''
maxDecision = []
remain = np.where(player.used == False)[0]
for i in remain:
for p in range(shape.tileMaxRotation[i]):
f = [0]
if shape.tileMaxFlip[i]:
f.append(1)
for q in f:
tile = Tiles(i, p, q)
xlist, ylist = board.canDropPos(player, tile)
if xlist.size == 0:
continue
for k in range(xlist.size):
x = xlist[k]
y = ylist[k]
board.dropTile(player, tile, x, y, False)
player.score += tile.size
player.used[tile.type] = True
score = greedyEval(board, player, opponent, setEvalWeight = [20, 10])
if len(maxDecision) < 5:
maxDecision.append({
'score' : score,
'tileType' : i, # type of tile
'rot' : p, # rotation
'flip' : q, # flip
'x' : x, # x
'y' : y # y
})
elif score > maxDecision[-1]['score']:
m = 4
while m > 0:
if maxDecision[m]['score'] > score:
break
maxDecision[m] = maxDecision[m - 1]
m -= 1
maxDecision[m] = {
'score' : score,
'tileType' : i, # type of tile
'rot' : p, # rotation
'flip' : q, # flip
'x' : x, # x
'y' : y # y
}
board.retraceDrop(tile, x, y)
player.used[tile.type] = False
player.score -= tile.size
for i, dec in enumerate(maxDecision):
tile = Tiles(dec['tileType'], dec['rot'], dec['flip'])
board.dropTile(player, tile, dec['x'], dec['y'], False)
player.used[dec['tileType']] = True
winningRate = mctsEval(board, player, opponent, setTot = 61, setReverse = True)
maxDecision[i]['winningRate'] = int(winningRate * 1000)
board.retraceDrop(tile, dec['x'], dec['y'])
player.used[dec['tileType']] = False
return maxDecision