-
Notifications
You must be signed in to change notification settings - Fork 0
/
student_agent.py
632 lines (481 loc) · 17.2 KB
/
student_agent.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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
# Monte Carlo Tree Search Agent
# System
import sys
# Deepcopy
from copy import deepcopy
# Numpy
import numpy as np
# Default dictionary
from collections import defaultdict
# Agent class
from agents.agent import Agent
# Register agent
from store import register_agent
# Random library
import random
# Math
import math
# Time
import time
# Global Time Limit Variable
timeLimit = 30
currentBoard = 0
# Class to represent the nodes in the tree
class MCTSNode:
#
CurrMaxScore = None
#
CurrMove = None
# Initialize the node
def __init__(self, state, my_pos, adv_pos, is_max, move=None, parent=None, heurist_score=0):
#
self.autoplay = True
#
self.parent = parent
#
self.is_max = is_max
#
self.totalScore = 0
#
self.numVisit = 0
#
self.my_pos = my_pos
#
self.adv_pos = adv_pos
#
self.move = move
# Chess_board
self.state = state
#
self.children = []
#
self.end_game = False
#
self.heuristic_score = heurist_score
#
def __str__(self):
return f"My_pos: {self.my_pos}, Adv_pos: {self.adv_pos}, Score: {self.totalScore}, Visit: {self.numVisit} " \
f"Move: {self.move}, IsMax: {self.is_max}"
#
def __gt__(self, other):
return self.heuristic_score > other.heuristic_score
#
def isTerminal(self):
return len(self.children) == 0
#
def isNewSim(self):
return self.numVisit == 0
#
def getWinRatio(self):
return self.totalScore / self.numVisit
#
def addChild(self, node):
self.children.append(node)
#
def ucbScore(self):
num = self.totalScore
den = self.numVisit
parentVisit = self.parent.numVisit
# calculates the UCB
if parentVisit > 0 and den > 0:
top = math.log(parentVisit)
total = num / den + 2 * math.sqrt(top / den)
else:
total = math.inf
return total
# Selects the child with the best UCB
def selectBestUcb(self):
maxUcb = -math.inf
maxNode = self
divider = 1
if len(self.children) > 130:
divider = 3
elif len(self.children) > 75:
divider = 2
for i in range(len(self.children) // divider):
child = self.children[i]
currentUcb = child.ucbScore()
# Updates node with highest UCB
if currentUcb > maxUcb:
maxUcb = currentUcb
maxNode = child
return maxNode
# Selects a child to explore or exploit
def selectChild(self):
if self.isTerminal():
return self
self.children.sort()
leafNode = self.selectBestUcb()
while len(leafNode.children) > 0:
leafNode.children.sort()
leafNode = leafNode.selectBestUcb()
return leafNode
#
@register_agent("student_agent")
class StudentAgent(Agent):
"""
A dummy class for your implementation. Feel free to use this class to
add any helper functionalities needed for your agent.
"""
def __init__(self):
super(StudentAgent, self).__init__()
self.autoplay = True
self.name = "StudentAgent"
# Moves (Up, Right, Down, Left)
self.moves = ((-1, 0), (0, 1), (1, 0), (0, -1))
# Opposite Directions
self.opposites = {0: 2, 1: 3, 2: 0, 3: 1}
self.root = None
def step(self, chess_board, my_pos, adv_pos, max_step):
"""
Implement the step function of your agent here.
You can use the following variables to access the chess board:
- chess_board: a numpy array of shape (x_max, y_max, 4)
- my_pos: a tuple of (x, y)
- adv_pos: a tuple of (x, y)
- max_step: an integer
You should return a tuple of ((x, y), dir),
where (x, y) is the next position of your agent and dir is the direction of the wall
you want to put on.
Please check the sample implementation in agents/random_agent.py or agents/human_agent.py for more details.
"""
# dummy return
print("\n")
board_size = len(chess_board)
global currentBoard, timeLimit
if board_size == currentBoard:
timeLimit = 2
else:
timeLimit = 30
currentBoard = board_size
# initialize Tree nodes
self.root = MCTSNode(chess_board, my_pos, adv_pos, False)
move = self.mcts(chess_board, self.root, max_step, board_size, 50)
r, x, d = move
print("Board size: ", len(chess_board))
print("Root Score: ", self.root.totalScore, "Visit", self.root.numVisit, "Move", self.root.move)
queue = [self.root]
# parent = None
# while len(queue) != 0:
# node = queue.pop(0)
#
# # if parent == node.parent:
# # print("Root Score: ", node.totalScore, "Visit", node.numVisit, "Move", node.move)
# # else:
# # print("\n")
# # print("Root Score: ", node.totalScore, "Visit", node.numVisit, "Move", node.move)
# # parent = node
#
# for child in node.children:
# queue.append(child)
# for child in self.root.children:
# print(child)
return (r, x), d
def valid_move(self, chess_board, my_pos, max_step, board_size, adv_pos):
# gen all possible moves from starting position
move_list = []
r, c = my_pos
r2, c2 = adv_pos
for i in range(board_size):
for j in range(board_size):
new_pos = i, j
x, y = new_pos
for k in range(4):
if (self.check_valid_step(np.array([r, c]), np.array([x, y]), k, max_step, chess_board,
np.array([r2, c2]))):
move_list.append((x, y, k))
return move_list
def mcts(self, chess_board, root, max_step, board_size, num_sim):
# Start time
start = time.time()
# End time
end = time.time() + timeLimit
# Root
parentNode = root
# Retrieve list of valid moves
validMove = self.valid_move(chess_board, parentNode.my_pos, max_step, board_size, parentNode.adv_pos)
# Expansion
self.expend(chess_board, parentNode, validMove, parentNode.adv_pos, parentNode.is_max)
# for _ in range(num_sim):
while start < end:
# select a leaf to explore
leaf = parentNode.selectChild()
if leaf.numVisit > 0:
validMove = self.valid_move(chess_board, leaf.my_pos, max_step, board_size, leaf.adv_pos)
self.expend(chess_board, leaf, validMove, leaf.adv_pos, leaf.is_max)
visit = leaf.selectChild()
else:
visit = leaf
results = self.simulation(visit, max_step, board_size)
self.backProp(visit, results)
#print("The time is: ", start)
start = time.time()
mv = self.bestMove(root)
return mv
# adds children to parents
def expend(self, chess_board, node, move, adv_pos, turn):
for mv in move:
x, y, d = mv
newState = deepcopy(chess_board)
self.set_barrier(x, y, d, newState)
# get a heuristic score for the node
heur = self.heuristic(newState, (x, y), adv_pos, d, node.is_max)
# if our turn set child node to False else set to True
if turn:
tmpNode = MCTSNode(newState, adv_pos, (x, y), False, mv, node, heur)
else:
tmpNode = MCTSNode(newState, adv_pos, (x, y), True, mv, node, heur)
#check if the node is not an end-game state so we don't expand it
if not node.end_game:
node.addChild(tmpNode)
else:
break
def simulation(self, topParent, max_step, board_size):
chess_board = deepcopy(topParent.state)
turn = topParent.is_max
my_pos = topParent.my_pos
adv_pos = topParent.adv_pos
end_game, p0_score, p1_score = self.check_endgame(board_size, chess_board, my_pos, adv_pos)
#if it is a endgame state
if end_game:
topParent.end_game = True
if not turn:
if p0_score > p1_score:
return 3
elif p0_score == p1_score:
return 1
else:
return 0
else:
if p0_score < p1_score:
return 3
elif p0_score == p1_score:
return 1
else:
return 0
while not end_game:
#reverse the turns
if turn:
turn = False
else:
turn = True
validMove = self.valid_move(chess_board, my_pos, max_step, board_size, adv_pos)
selectMv = self.selectBstHeuristic(chess_board, adv_pos, turn, validMove)
r, c, d = selectMv
self.set_barrier(r, c, d, chess_board)
my_pos = adv_pos
adv_pos = (r, c)
end_game, p0_score, p1_score = self.check_endgame(board_size, chess_board, my_pos, adv_pos)
# print("Endgame: ", end_game, "P0 Score: ", p0_score, "P1 Score: ", p1_score, "Turn: ", turn)
#return the score accordingly
if not turn:
if p0_score > p1_score:
return 2
elif p0_score == p1_score:
return 1
else:
return 0
else:
if p0_score < p1_score:
return 2
elif p0_score == p1_score:
return 1
else:
return 0
def backProp(self, selectedNode, score):
tmp = selectedNode
turn = selectedNode.is_max
tracker = 0
#if it is our turn and we lost only increase the total score for nodes that aren't us or True
if turn:
if score == 0:
score = 1
tracker = 1
else:
if score == 0:
score = 1
else:
tracker = 1
#update the node score and visits
while tmp is not None:
tmp.numVisit += 1
if tracker % 2 == 0:
tmp.totalScore += score
tracker += 1
tmp = tmp.parent
def bestMove(self, root):
maxI = -100000000
node = None
for child in root.children:
if child.numVisit > 0:
ratio = child.totalScore / child.numVisit
if ratio > maxI:
maxI = ratio
node = child
# print("TotalScore: ", maxI, " Move: ", node.move, "Num visit", node.numVisit,"total score: " ,node.totalScore)
return node.move
def selectBstHeuristic(self, chess_board, adv_pos, isMax, moveList):
x, y, d = moveList[0]
finalH = self.heuristic(chess_board, (x, y), adv_pos, d, isMax)
finalMv = moveList[0]
for mv in moveList:
x, y, d = mv
tmpH = self.heuristic(chess_board, (x, y), adv_pos, d, isMax)
if tmpH > finalH:
finalH = tmpH
finalMv = mv
return finalMv
def heuristic(self, chess_board, my_pos, adv_pos, direction, turn):
# calculate the number of walls reachable by both players
# add more value on direction score if horz or vert wall is needed more
min_count = 0
r1, c1 = my_pos
r2, c2 = adv_pos
self.set_barrier(r1, c1, direction, chess_board)
new_state = deepcopy(chess_board)
end_game, p0, p1 = self.check_endgame(len(new_state), new_state, my_pos, adv_pos)
if end_game:
return p0 - p1
# calculate the direction
horz = c2 - c1
vert = r1 - r2
dirScore = 0
if horz > 0:
if direction == 3:
dirScore -= 2
elif direction == 1:
dirScore += 1
else:
if direction == 3:
dirScore += 1
elif direction == 1:
dirScore -= 2
if vert > 0:
if direction == 2:
dirScore -= 2
elif direction == 1:
dirScore += 1
else:
if direction == 2:
dirScore += 1
elif direction == 0:
dirScore -= 2
trap_scoreM = 0
trap_scoreA = 0
for i in range(4):
if chess_board[r1, c1, i]:
trap_scoreM -= 6
if chess_board[r2, c2, i]:
trap_scoreM += 10
dis = (5 / (np.sqrt(pow((r1 - r2), 2) + pow((c1 - c2), 2)))) + trap_scoreM + trap_scoreA
# count number of walls around adv and mypos
# checks if we are in a corner
for i in range(4):
if chess_board[r1, c1, i]:
min_count -= 8
if r1 % len(chess_board - 1) == 0 and c1 % len(chess_board - 1) == 0:
min_count -= 15 * dis
elif r1 % len(chess_board - 1 == 0):
min_count -= 8 * dis
elif c1 % len(chess_board - 1 == 0):
min_count -= 8 * dis
if chess_board[r2, c2, i]:
min_count += 5
count = min_count + dis + dirScore
# print("heuristic", count)
return count
def check_endgame(self, board_size, chess_board, my_pos, adv_pos):
"""
Check if the game ends and compute the current score of the agents.
Returns
-------
is_endgame : bool
Whether the game ends.
player_1_score : int
The score of player 1.
player_2_score : int
The score of player 2.
"""
# Union-Find
father = dict()
for r in range(board_size):
for c in range(board_size):
father[(r, c)] = (r, c)
def find(pos):
if father[pos] != pos:
father[pos] = find(father[pos])
return father[pos]
def union(pos1, pos2):
father[pos1] = pos2
for r in range(board_size):
for c in range(board_size):
for dir, move in enumerate(
self.moves[1:3]
): # Only check down and right
if chess_board[r, c, dir + 1]:
continue
pos_a = find((r, c))
pos_b = find((r + move[0], c + move[1]))
if pos_a != pos_b:
union(pos_a, pos_b)
for r in range(board_size):
for c in range(board_size):
find((r, c))
p0_r = find(my_pos)
p1_r = find(adv_pos)
p0_score = list(father.values()).count(p0_r)
p1_score = list(father.values()).count(p1_r)
if p0_r == p1_r:
return False, p0_score, p1_score
return True, p0_score, p1_score
def check_valid_step(self, start_pos, end_pos, barrier_dir, max_step, chess_board, adv_pos):
"""
Check if the step the agent takes is valid (reachable and within max steps).
Parameters
----------
start_pos : tuple
The start position of the agent.
end_pos : np.ndarray
The end position of the agent.
barrier_dir : int
The direction of the barrier.
"""
# Endpoint already has barrier or is boarder
r, c = end_pos
if chess_board[r, c, barrier_dir]:
return False
if np.array_equal(start_pos, end_pos):
return True
# BFS
state_queue = [(start_pos, 0)]
visited = {tuple(start_pos)}
is_reached = False
while state_queue and not is_reached:
cur_pos, cur_step = state_queue.pop(0)
r, c = cur_pos
if cur_step == max_step:
break
for dir, move in enumerate(self.moves):
if chess_board[r, c, dir]:
continue
next_pos = cur_pos + move
if np.array_equal(next_pos, adv_pos) or tuple(next_pos) in visited:
continue
if np.array_equal(next_pos, end_pos):
is_reached = True
break
visited.add(tuple(next_pos))
state_queue.append((next_pos, cur_step + 1))
return is_reached
def set_barrier(self, r, c, dir, chess_board):
# Set the barrier to True
chess_board[r, c, dir] = True
# Set the opposite barrier to True
move = self.moves[dir]
chess_board[r + move[0], c + move[1], self.opposites[dir]] = True
def undo_barrier(self, r, c, dir, chess_board):
# Set the barrier to True
chess_board[r, c, dir] = False
# Set the opposite barrier to True
move = self.moves[dir]
chess_board[r + move[0], c + move[1], self.opposites[dir]] = False