-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.py
40 lines (32 loc) · 1.1 KB
/
client.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
# Client process
# Repeatedly does the following:
# Gets latest neural network from server, plays many games of self play
# with the MCTS algorithm, thereby generating training data, which it
# reports to the server upon termination of the game.
import model
import config
import selfplay
import requests
import tempfile
net = model.Model()
def load_model_weights():
data = requests.get(config.server_address + "/weights")
tfile, tname = tempfile.mkstemp(".h5")
open(tname, "wb").write(data.content)
net.load(tname)
def play_game():
# use MCTS to play a game, and return training data
# encode the data in a string format
mcts = selfplay.MCTS(net)
game_history = mcts.selfplay()
history_str = mcts.encode_history(game_history)
return history_str
def upload_data(string):
# send back training data to server
resp = requests.post(config.server_address + "/train", data=string.encode())
assert(resp.content == b'"OK"\n')
for i in range(1):
load_model_weights()
for i in range(config.client_play_games_num):
data = play_game()
upload_data(data)