-
Notifications
You must be signed in to change notification settings - Fork 0
/
go_mcts.py
289 lines (246 loc) · 8.68 KB
/
go_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
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
'''*****************************************
* *
* Programmer: Nikola Andric *
* Email: nikolazeljkoandric@gmail.com *
* Last Eddited: 3. Dec. 2021 *
* *
*****************************************'''
import time
import numpy as np
import sys
class Node:
def __init__(self, state, parent):
self.state = state
self.wins = 0
self.visits = 0
self.parent = parent
self.children = []
class MonteCarloTree():
def __init__(self, state):
self.root = Node(state, None)
self.curr = self.root
self.turn = True
# main function for the Monte Carlo Tree Search
def monteCarloTreeSearch(self, state):
if not np.array_equal(state, self.root.state):
if np.array_equal(state, self.curr.state):
return self.curr
else:
for child in self.curr.children:
if np.array_equal(state, child.state):
self.curr = child
# seconds = time.time()
# while (time.time() - seconds) < 1: # computational power?
for i in range(1000):
print(i)
print('s1')
leaf = self.traverse(self.curr)
print('s2')
simulationResult = self.rollout(leaf)
print('s3')
self.backpropagate(leaf, simulationResult)
print('s4')
self.curr = self.bestChild(self.curr)
return self.curr
# function for node traversal
def traverse(self, node):
self.turn = True
# selection
while node.children:
bestUcb = 0
bestNode = node
for child in node.children:
if child.visits != 0:
ucb = child.wins/child.visits + 2*np.sqrt(np.log(child.parent.visits)/child.visits)
else:
ucb = 1
if ucb >= bestUcb:
bestUcb = ucb
bestNode = child
node = bestNode
if node.children:
self.turn = not self.turn
# expansion
if not node.children:
if node.visits == 0:
return node
else:
# game over
if self.gameOver(node.state) > -1:
return node
self.createChildren(node)
# Randomly choose a child for rollout
node = np.random.choice(node.children)
self.turn = not self.turn
return node
# find all possible moves and add them as children to node
def createChildren(self, node):
for idx, move in np.ndenumerate(node.state):
if move == 0:
newState = np.copy(node.state)
if self.turn:
newState[idx] = 2
else:
newState[idx] = 1
newNode = Node(newState, node)
node.children.append(newNode)
return
# function for the result of the simulation
def rollout(self, node):
tempNode = Node(node.state, None)
turn = self.turn
while self.gameOver(tempNode.state) < 0:
# print(tempNode.state)
# print()
if not tempNode.children:
self.createChildren(tempNode)
tempNode = self.rolloutPolicy(tempNode)
turn = not turn
# print(tempNode.state)
# print()
if self.gameOver(tempNode.state) == 0:
return 0
elif self.turn:
return -1
else:
return 1
# function for randomly selecting a child node
def rolloutPolicy(self, node):
return np.random.choice(node.children)
# function for backpropagation
def backpropagate(self, node, result):
node.wins += result
node.visits += 1
if not node.parent:
return
self.backpropagate(node.parent, -result)
# function for selecting the best child
# node with highest number of visits
def bestChild(self, node):
mostVisits = 0
best = None
for child in node.children:
if child.visits > mostVisits:
mostVisits = child.visits
best = child
return best
# function to determine if game has ended.
# -1 if game has not ended.
# 0 if game ended in cats.
# 1 if game ended with a winner.
def gameOver(self, state):
victory = False
for row in state:
if (0 not in row) and (row[0] == row[1]) and (row[0] == row[2]):
victory = True
break
for col in state.T:
if (0 not in col) and (col[0] == col[1]) and (col[0] == col[2]):
victory = True
break
if (0 not in [state[0,0], state[1,1], state[2,2]]) and (state[0,0] == state[1,1]) and (state[0,0] == state[2,2]):
victory = True
if (0 not in [state[0,2], state[1,1], state[2,0]]) and (state[0,2] == state[1,1]) and (state[0,2] == state[2,0]):
victory = True
if (not victory) and (0 not in state):
return 0
if victory == True:
return 1
else:
return -1
#########################################################################################
def gameOver(state, playerOne):
victory = False
for row in state:
if (0 not in row) and (row[0] == row[1]) and (row[0] == row[2]):
victory = True
break
for col in state.T:
if (0 not in col) and (col[0] == col[1]) and (col[0] == col[2]):
victory = True
break
if (0 not in [state[0,0], state[1,1], state[2,2]]) and (state[0,0] == state[1,1]) and (state[0,0] == state[2,2]):
victory = True
if (0 not in [state[0,2], state[1,1], state[2,0]]) and (state[0,2] == state[1,1]) and (state[0,2] == state[2,0]):
victory = True
if (not victory) and (0 not in state):
print('Cats Game.')
return True
if victory and playerOne:
print(state)
print('Tic Tac Toe!')
print('Player 1 Wins.')
return True
elif victory and not playerOne:
print(state)
print('Tic Tac Toe!')
print('Player 2 Wins.')
return True
return False
def humanVsAi():
state = np.array([[0,0,0],[0,0,0],[0,0,0]])
mcts = MonteCarloTree(state)
while True:
state = np.copy(mcts.monteCarloTreeSearch(np.copy(state)).state)
if gameOver(state, False):
break
while True:
print(state)
print('Make your move.')
while True:
print('Enter row:')
try:
row = int(input())
if row in [0,1,2]:
break
except:
print('Please enter 0, 1, or 2 representing the row.')
while True:
print('Enter column:')
try:
col = int(input())
if col in [0,1,2]:
break
except:
print('Please enter 0, 1, or 2 representing the column.')
if state[row, col] == 0:
state[row, col] = 1
break
else:
print('Square already taken. Try another.')
if gameOver(state, True):
break
return mcts
#########################################################################################
if __name__ == "__main__":
state = np.array([[0,0,0],[0,0,0],[0,0,0]])
mcts = MonteCarloTree(state)
bestMove = mcts.monteCarloTreeSearch(state)
print('best move')
print(bestMove.state)
# mcts = humanVsAi()
print('#############################################')
print(mcts.root.state)
print(mcts.root.wins)
print(mcts.root.visits)
print()
level = mcts.root.children
totNodes = 0
treeSizeMb = 0
for i in range(9):
nextLevel = []
totVisits = 0
levelStr = ''
for node in level:
totNodes += 1
treeSizeMb += sys.getsizeof(node)
levelStr += ' ' + str(node.wins) + '/' + str(node.visits)
totVisits += node.visits
for child in node.children:
nextLevel.append(child)
print('#### ' + str(totVisits) + ' #############################################')
print(levelStr)
level = nextLevel.copy()
print('#############################################')
print(totNodes)
print(treeSizeMb)