-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcts.py
More file actions
211 lines (167 loc) · 6.25 KB
/
mcts.py
File metadata and controls
211 lines (167 loc) · 6.25 KB
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
# -*- coding: utf-8 -*-
"""
A pure implementation of the Monte Carlo Tree Search (MCTS)
@author: Junxiao Song
@author: x19
"""
import numpy as np
import copy
# from node import TreeNode # pure python
from ctree.cmcts import Node # cython
def softmax(x):
probs = np.exp(x - np.max(x))
probs /= np.sum(probs)
return probs
def policy_value_fn(board):
"""a function that takes in a state and outputs a list of (action, probability)
tuples and a score for the state"""
# return uniform probabilities and 0 score for pure MCTS
action_probs = np.ones(len(board.availables)) / len(board.availables)
return (board.availables, action_probs), 0
class MCTS:
"""A simple implementation of Monte Carlo Tree Search."""
def __init__(self, policy_value_fn, c_puct=5, n_playout=10000):
"""
policy_value_fn: a function that takes in a board state and outputs
a list of (action, probability) tuples and also a score in [-1, 1]
(i.e. the expected value of the end game score from the current
player's perspective) for the current player.
c_puct: a number in (0, inf) that controls how quickly exploration
converges to the maximum-value policy. A higher value means
relying on the prior more.
"""
self._root = Node(None, 1.0, 0)
self._policy = policy_value_fn
self._c_puct = c_puct
self._n_playout = n_playout
def _playout(self, state):
"""Run a single playout from the root to the leaf, getting a value at
the leaf and propagating it back through its parents.
State is modified in-place, so a copy must be provided.
"""
node = self._root
while 1:
if node.is_leaf():
break
# Greedily select next move.
node, action = node.select(self._c_puct)
state.do_move(action)
(action, probs), leaf_value = self._policy(state)
# Check for end of game
end, winner = state.game_end()
if not end:
node.expand(action, probs)
else:
if winner == -1:
leaf_value = 0.0
else:
leaf_value = 1.0 if winner == state.get_current_player() else -1.0
# Update value and visit count of nodes in this traversal.
node.update_recursive(-leaf_value)
def get_move(self, state, temp=1e-3):
"""Runs all playouts sequentially and returns the most visited action.
state: the current game state
Return: the selected action
"""
for _ in range(self._n_playout):
state_copy = copy.deepcopy(state)
self._playout(state_copy)
# calc the move probabilities based on visit counts at the root node
act_visits = [(node.action, node.n_visits) for node in self._root.children]
acts, visits = zip(*act_visits)
act_probs = softmax(1.0 / temp * np.log(np.array(visits) + 1e-10))
return acts, act_probs
def update_with_move(self, last_move):
"""Step forward in the tree, keeping everything we already know
about the subtree.
"""
child = self._root.get_child_by_action(last_move)
if child:
del self._root
self._root = child
else:
print("WARNING: no child found for action", last_move)
self.reset()
def reset(self):
self._root.delete_tree() # del root + children
self._root = Node(None, 1.0, 0)
def __str__(self):
return "MCTS"
class MCTSPlayer(object):
"""AI player based on MCTS"""
def __init__(
self,
policy_value_fn=policy_value_fn,
c_puct=5,
n_playout=2000,
add_noise=True,
is_self_play=False,
):
self.mcts = MCTS(policy_value_fn, c_puct, n_playout)
self.add_noise = add_noise
self.is_self_play = is_self_play
def set_player_ind(self, p):
self.player = p
def reset_player(self):
self.mcts.reset()
def get_action(self, board, with_probs=False, reset_tree=False):
sensible_moves = board.availables
if len(sensible_moves) > 0:
acts, act_probs = self.mcts.get_move(board)
# mask out illegal moves
full_probs = np.zeros(board.width * board.height)
full_probs[list(acts)] = act_probs
if self.add_noise:
# explore relative to how many moves left
game_left = len(board.availables) / (board.width * board.height)
move = np.random.choice(
acts,
p=(1 - game_left) * act_probs
+ game_left * np.random.dirichlet(0.3 * np.ones(len(act_probs))),
)
else:
move = np.random.choice(acts, p=act_probs)
if self.is_self_play and not reset_tree:
# next turn will re-use the tree as the opponent
self.mcts.update_with_move(move)
else:
self.mcts.reset()
return move if not with_probs else (move, full_probs)
else:
print("WARNING: the board is full")
def __str__(self):
return "MCTS {}".format(self.player)
class Random:
def set_player_ind(self, p):
self.player = p
def get_action(self, board, reset_tree=False):
return np.random.choice(board.availables)
if __name__ == "__main__":
from board import Board
board = Board()
board.init_board()
# mcts = MCTS(policy_value_fn, n_playout=1_000)
# acts, probs = mcts.get_move(board)
# move = acts[0]
# mcts.update_with_move(move)
# # for _ in range(100000):
# # board_copy = copy.deepcopy(board)
# # mcts._playout(board_copy)
mcts_player = MCTSPlayer()
mcts_player.set_player_ind(1)
rando = Random()
rando.set_player_ind(2)
players = [mcts_player, rando]
name = {1: "MCTS", 2: "Random"}
i = 0
while True:
player = players[i % 2]
print("player: ", name[player.player])
move = player.get_action(board)
board.do_move(move)
i += 1
end, winner = board.game_end()
if end:
break
print("WINNER: ", name[winner], "Player: ", winner)
board.show()