-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcts_player.py
More file actions
31 lines (25 loc) · 953 Bytes
/
Copy pathmcts_player.py
File metadata and controls
31 lines (25 loc) · 953 Bytes
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
from game import IPlayer
from prob_reversi import Move, Position
from mcts import SearchResult, MoveEval, UCTConfig, UCT
class MCTSPlayer(IPlayer):
def __init__(self, config: UCTConfig, num_playout=1000):
self.__pos = Position(4)
self.__uct = UCT(config)
self.__num_playout = num_playout
@property
def name(self):
return "MCTS Player"
def set_position(self, pos: Position):
self.__pos = pos
self.__uct.set_root_pos(pos)
def gen_move(self) -> int:
result = self.__uct.search(self.__num_playout)
best = max(result.move_values, key=lambda e: e.playout_count)
print(f"win_rate: {best.action_value * 100.0:.2f}%")
return best.coord
def do_move(self, move: Move):
self.__pos.do_move(move)
self.__uct.set_root_pos(self.__pos)
def do_pass(self):
self.__pos.do_pass()
self.__uct.set_root_pos(self.__pos)