-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCTS.py
188 lines (153 loc) · 5.54 KB
/
MCTS.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
import math
import copy
import game
class Solver:
def __init__(self,
prev_board: list = None,
board: list = None,
player: int = 1,
simu_threshold: int = 10):
self.prev_board = copy.deepcopy(prev_board)
self.board = copy.deepcopy(board)
self.player, self.opponent = player, -1 * player
self.simu_threshold = simu_threshold
self.total_simu = 0
self.cg = game.CoGanh()
# depend on the ratio win_simu / nums_sime of each node
def choose_1(self, children):
best_move = None
max_ratio = 0
for node in children:
ratio = node.ratio()
if ratio >= max_ratio:
max_ratio = ratio
best_move = node
return best_move
# depend on the total numbers of simulation of each node
def choose_2(self, children):
best_move = None
max_simu = 0
for node in children:
simu = node.nums_simu
if simu >= max_simu:
max_simu = simu
best_move = node
return best_move
# Evaluate function to balance the exploration and exploitation
def evaluate(self, node, c = 1.414):
x = node.ratio()
if node.nums_simu > 0:
y = math.sqrt(math.log(self.total_simu) / node.nums_simu)
else:
y = 0
return x + c * y
def Selection(self, list):
if len(list) == 0:
return None
elif len(list) == 1:
return list[0]
max_eval = -100
best_node = None
for node in list:
eval = self.evaluate(node)
if eval >= max_eval:
max_eval = eval
best_node = node
return best_node
# Using simple simulation and selection to expand the tree for both player and opponent
def Expansion(self, node):
ply, oppo = 0, 0
if node.parent == None:
ply = self.player
else:
board1, board2 = node.parent.board, node.board
for i in range(5):
for j in range(5):
if board1[i][j] == 0 and board2[i][j] != 0:
ply, oppo = -1 * board2[i][j], board2[i][j]
trap = None
if node.parent != None:
trap = self.cg.checkTrap(node.parent.board, node.board, oppo)
pos = self.cg.getPosition(node.board, ply)
possible_moves = []
for p in pos:
possible_moves += self.cg.move_gen_2(node, p, trap)
isTrap = False
for move in possible_moves:
if move[1]:
isTrap = True
break
for move in possible_moves:
if isTrap:
if not move[1]:
continue
node.child.append(move[0])
# Just an idea that using Minimax to calculate the next step of the opponent
# instead of using simulation and selection like the original MCTS
def Expansion_2(self, node):
ply = 0
if node.parent == None:
ply = self.player
else:
board1, board2 = node.parent.board, node.board
for i in range(5):
for j in range(5):
if board1[i][j] == 0 and board2[i][j] != 0:
ply = -1 * board2[i][j]
if ply == self.opponent:
return 0
else:
return 0
def Simulation(self, node):
ply = 0
if node.parent == None:
ply = self.player
else:
board1, board2 = node.parent.board, node.board
for i in range(5):
for j in range(5):
if board1[i][j] == 0 and board2[i][j] != 0:
ply = -1 * board2[i][j]
final_step = copy.deepcopy(node)
while not self.cg.end_game(final_step.board, False):
final_step = self.cg.random_move_2(final_step, ply)
ply *= -1
self.total_simu += 1
node.nums_simu += 1
prev = node.parent
while prev != None:
prev.nums_simu += 1
prev = prev.parent
if self.player == 1:
if self.cg.X_win(final_step.board):
return True
return False
else:
if self.cg.O_win(final_step.board):
return True
return False
def Update(self, node, win):
if win:
node.win_simu += 1
prev = node.parent
while prev != None:
prev.win_simu += 1
prev = prev.parent
return
def solv(self):
node = game.Node_2(self.board)
while self.total_simu < self.simu_threshold:
# Selection
selected_node = node
while len(selected_node.child) > 0:
list = selected_node.child
selected_node = self.Selection(list)
# Expansion
self.Expansion(selected_node)
# Simulation
res = self.Simulation(selected_node)
# Update
self.Update(selected_node, res)
best_move = self.choose_1(node.child)
start, end = self.cg.back_prop(self.board, best_move.board, self.player)
return (start, end)