Repository navigation
Expand file tree
/
Copy pathweb_player.py
More file actions
82 lines (66 loc) · 2.64 KB
/
Copy pathweb_player.py
File metadata and controls
82 lines (66 loc) · 2.64 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
from typing import List
from absl import logging
from absl import app as abslapp
import numpy as np
import go
import coords
from k2net import DualNetwork
import myconf
from strategies import MCTSPlayer
class WebPlayer(object):
def __init__(self, dnn: DualNetwork):
self.dnn = dnn
self.pos = go.Position()
self.player = MCTSPlayer(dnn, resign_threshold=-1)
def sample_move(self, probs) -> int:
return np.random.choice(len(probs), 1, p=probs)[0]
def select_move_no_mcts(self, moves_history):
""" """
pos = self.construct_game_state(moves_history)
probs, value = self.dnn.run(pos)
# explore / exploit balance: higher -> max
probs_sample = probs ** 3
probs_sample = probs_sample / probs_sample.sum()
move = self.sample_move(probs_sample)
logging.info('value=%.1f, turn %d: best-move=%s (%.2f), chosen=%s (%.2f)', value, pos.n,
coords.from_flat(np.argmax(probs)), np.max(probs), coords.from_flat(move), probs[move])
return coords.flat_to_gtp(move)
def select_move(self, moves_history):
"""
quick job: we would benefit from reuse the tree...
"""
pos = self.construct_game_state(moves_history)
player = self.player
player.initialize_game(pos, focus_area=myconf.corner_focus(9))
move = player.suggest_move(pos)
# why is root.Q np.array for minigo model, float for converted jax model
root_winrate = '%.2f' % np.squeeze(player.root.Q)
child_q = player.root.child_Q[coords.to_flat(move)]
gtp_move = coords.to_gtp(move)
info_dict = {'winrate': root_winrate, 'move': gtp_move}
logging.info('root winrate %s, mcts chose: %s, child_Q %.2f', info_dict['winrate'], gtp_move, child_q)
return gtp_move, info_dict
def construct_game_state(self, moves_history: List[str]):
""" moves are in gtp format """
pos = go.Position()
try:
for i, move in enumerate(moves_history):
if move == 'resign':
logging.info('%s resigned', pos.to_play)
break
c = coords.from_gtp(move)
pos = pos.play_move(c)
except:
logging.error('error reconstructing %d', pos.n)
return pos
def test_player():
dnn = DualNetwork(f'{myconf.EXP_HOME}/checkpoints/model3_epoch_5.h5')
player = WebPlayer(dnn)
moves_history = []
for i in range(5):
move, info = player.select_move(moves_history)
moves_history.append(move)
def main(argv):
test_player()
if __name__ == '__main__':
abslapp.run(main)