-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmcts_demo.py
190 lines (145 loc) · 4.9 KB
/
mcts_demo.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
import random
import math
import mnk
def main():
run_trials(5, 5, 4, 100, 'q', 100)
run_trials(5, 5, 4, 100, 'n', 100)
def run_trials(m, n, k, iterations, value, trials):
mcwins = 0
qlwins = 0
draws = 0
for i in range(trials):
outcome = mc_vs_ql(m, n, k, iterations, value)
if outcome == 1:
mcwins += 1
elif outcome == -1:
qlwins += 1
else:
draws += 1
print("{} trials for ({}, {}, {}) with {} iterations and using {} values".format(trials, m, n, k, iterations, value))
print("mcts: ", mcwins)
print("qlts: ", qlwins)
print("draw: ", draws)
def mc_vs_ql(m, n, k, iterations, value):
mc = random.choice([1, -1])
board = mnk.Board(m, n, k)
mcroot = Node()
mcroot.expand(board.legal_moves())
qlroot = Node()
qlroot.expand(board.legal_moves())
while board.who_won() == 2:
if board.player == mc:
move = mctsAI(board, mcroot, iterations, value).last_move
qlroot = qlroot.move(move)
mcroot = mcroot.move(move)
board.move(*move)
else:
move = qltsAI(board, qlroot, iterations, value).last_move
qlroot = qlroot.move(move)
mcroot = mcroot.move(move)
board.move(*move)
winner = board.who_won()
return winner * mc
def play_human():
m, n, k = map(int, input('Choose m, n, and k: ').split())
iterations = int(input('Choose AI Iterations: '))
human = random.choice([1, -1])
board = mnk.Board(m, n, k)
root = Node()
root.expand(board.legal_moves())
print(board)
while board.who_won() == 2:
if board.player == human:
x, y = map(int, input('Make a move (x y): ').split())
x -= 1
y -= 1
root = root.move((x, y))
board.move(x, y)
else:
print('AI is thinking')
root = qltsAI(board, root, iterations, 'n')
board.move(*root.last_move)
print(board)
winner = board.who_won()
if winner == human:
print('You win!')
elif winner == -human:
print('You lose :(')
elif winner == 0:
print('You tie.')
def mctsAI(board, node, iterations, value):
for i in range(iterations):
mcts(board, node)
if value == 'q':
return max(node.children, key=lambda child: child.q)
else:
return max(node.children, key=lambda child: child.n)
def qltsAI(board, node, iterations, value):
for i in range(iterations):
qlts(board, node)
if value == 'q':
return max(node.children, key=lambda child: child.q)
else:
return max(node.children, key=lambda child: child.n)
def mcts(board, node):
if node.isLeaf:
winner = rollout(board)
if board.who_won() == 2:
node.expand(board.legal_moves())
else:
next_state = node.max_child()
board.move(*next_state.last_move)
winner = mcts(board, next_state)
board.undo_move()
node.n += 1
node.q = node.q + (1 / node.n) * (winner * -board.player - node.q)
return winner
def qlts(board, node):
if node.isLeaf:
winner = rollout(board)
if board.who_won() == 2:
node.expand(board.legal_moves())
node.n += 1
node.q += winner * -board.player
else:
next_state = node.max_child()
board.move(*next_state.last_move)
qlts(board, next_state)
board.undo_move()
target = -max(node.children, key=lambda child: child.q).q
node.n += 1
node.q = node.q + (1 / node.n) * (target - node.q)
def rollout(board):
moves_played = 0
while True:
winner = board.who_won()
if winner != 2:
break
legal_moves = board.legal_moves()
move = random.choice(legal_moves)
board.move(*move)
moves_played += 1
for _ in range(moves_played):
board.undo_move()
return winner
class Node:
def __init__(self, last_move = None):
self.last_move = last_move
self.q = 0 # Average of rewards from rollouts
self.n = 0 # Number of times node has been visited
self.children = []
self.isLeaf = True
def move(self, move):
for child in self.children:
if child.last_move == move:
return child
def expand(self, legal_moves):
for move in legal_moves:
self.children.append(Node(move))
self.isLeaf = False
def UCT(self, N):
return self.q + 2 * math.sqrt(math.log(N + 1) / (self.n + 1))
def max_child(self):
return max(self.children, key=lambda child: child.UCT(self.n))
if __name__ == '__main__':
main()