From eb0ce959190d4725a6c966706cf221944311ca0f Mon Sep 17 00:00:00 2001 From: rtz19970824 Date: Tue, 9 Jan 2018 19:50:37 +0800 Subject: [PATCH 1/4] modify model.py for multi-player --- AlphaGo/model.py | 155 ++++++++++++++++++++++++++++++----------------- 1 file changed, 100 insertions(+), 55 deletions(-) diff --git a/AlphaGo/model.py b/AlphaGo/model.py index 8d4c508..88fd199 100644 --- a/AlphaGo/model.py +++ b/AlphaGo/model.py @@ -80,7 +80,8 @@ class Data(object): class ResNet(object): - def __init__(self, board_size, action_num, history_length=1, residual_block_num=10, checkpoint_path=None): + def __init__(self, board_size, action_num, history_length=1, residual_block_num=10, black_checkpoint_path=None, + white_checkpoint_path=None): """ the resnet model @@ -88,25 +89,49 @@ class ResNet(object): :param action_num: an integer, number of unique actions at any state :param history_length: an integer, the history length to use, default is 1 :param residual_block_num: an integer, the number of residual block, default is 20, at least 1 - :param checkpoint_path: a string, the path to the checkpoint, default is None, + :param black_checkpoint_path: a string, the path to the black checkpoint, default is None, + :param white_checkpoint_path: a string, the path to the white checkpoint, default is None, """ self.board_size = board_size self.action_num = action_num self.history_length = history_length - self.checkpoint_path = checkpoint_path + self.black_checkpoint_path = black_checkpoint_path + self.white_checkpoint_path = white_checkpoint_path self.x = tf.placeholder(tf.float32, shape=[None, self.board_size, self.board_size, 2 * self.history_length + 1]) self.is_training = tf.placeholder(tf.bool, shape=[]) self.z = tf.placeholder(tf.float32, shape=[None, 1]) self.pi = tf.placeholder(tf.float32, shape=[None, self.action_num]) - self._build_network(residual_block_num, self.checkpoint_path) + self._build_network('black', residual_block_num) + self._build_network('white', residual_block_num) + self.sess = multi_gpu.create_session() + self.sess.run(tf.global_variables_initializer()) + if black_checkpoint_path is not None: + ckpt_file = tf.train.latest_checkpoint(black_checkpoint_path) + if ckpt_file is not None: + print('Restoring model from {}...'.format(ckpt_file)) + self.black_saver.restore(self.sess, ckpt_file) + print('Successfully loaded') + else: + raise ValueError("No model in path {}".format(black_checkpoint_path)) + + if white_checkpoint_path is not None: + ckpt_file = tf.train.latest_checkpoint(white_checkpoint_path) + if ckpt_file is not None: + print('Restoring model from {}...'.format(ckpt_file)) + self.white_saver.restore(self.sess, ckpt_file) + print('Successfully loaded') + else: + raise ValueError("No model in path {}".format(white_checkpoint_path)) + self.update = [tf.assign(black_params, white_params) for black_params, white_params in + zip(self.black_var_list, self.white_var_list)] # training hyper-parameters: - self.window_length = 3 + self.window_length = 900 self.save_freq = 5000 self.training_data = {'states': deque(maxlen=self.window_length), 'probs': deque(maxlen=self.window_length), 'winner': deque(maxlen=self.window_length), 'length': deque(maxlen=self.window_length)} - def _build_network(self, residual_block_num, checkpoint_path): + def _build_network(self, scope, residual_block_num): """ build the network @@ -114,37 +139,34 @@ class ResNet(object): :param checkpoint_path: a string, the path to the checkpoint, if None, use random initialization parameter :return: None """ + with tf.variable_scope(scope, reuse=tf.AUTO_REUSE): + h = layers.conv2d(self.x, 256, kernel_size=3, stride=1, activation_fn=tf.nn.relu, + normalizer_fn=layers.batch_norm, + normalizer_params={'is_training': self.is_training, + 'updates_collections': tf.GraphKeys.UPDATE_OPS}, + weights_regularizer=layers.l2_regularizer(1e-4)) + for i in range(residual_block_num - 1): + h = residual_block(h, self.is_training) + self.__setattr__(scope + '_v', value_head(h, self.is_training)) + self.__setattr__(scope + '_p', policy_head(h, self.is_training, self.action_num)) + self.__setattr__(scope + '_prob', tf.nn.softmax(self.__getattribute__(scope + '_p'))) + self.__setattr__(scope + '_value_loss', tf.reduce_mean(tf.square(self.z - self.__getattribute__(scope + '_v')))) + self.__setattr__(scope + '_policy_loss', + tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=self.pi, + logits=self.__getattribute__( + scope + '_p')))) - h = layers.conv2d(self.x, 256, kernel_size=3, stride=1, activation_fn=tf.nn.relu, - normalizer_fn=layers.batch_norm, - normalizer_params={'is_training': self.is_training, - 'updates_collections': tf.GraphKeys.UPDATE_OPS}, - weights_regularizer=layers.l2_regularizer(1e-4)) - for i in range(residual_block_num - 1): - h = residual_block(h, self.is_training) - self.v = value_head(h, self.is_training) - self.p = policy_head(h, self.is_training, self.action_num) - self.prob = tf.nn.softmax(self.p) - self.value_loss = tf.reduce_mean(tf.square(self.z - self.v)) - self.policy_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=self.pi, logits=self.p)) - - self.reg = tf.add_n(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)) - self.total_loss = self.value_loss + self.policy_loss + self.reg - self.update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) - with tf.control_dependencies(self.update_ops): - self.train_op = tf.train.AdamOptimizer(1e-4).minimize(self.total_loss) - self.var_list = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES) - self.saver = tf.train.Saver(max_to_keep=0, var_list=self.var_list) - self.sess = multi_gpu.create_session() - self.sess.run(tf.global_variables_initializer()) - if checkpoint_path is not None: - ckpt_file = tf.train.latest_checkpoint(checkpoint_path) - if ckpt_file is not None: - print('Restoring model from {}...'.format(ckpt_file)) - self.saver.restore(self.sess, ckpt_file) - print('Successfully loaded') - else: - raise ValueError("No model in path {}".format(checkpoint_path)) + self.__setattr__(scope + '_reg', tf.add_n(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES, scope=scope))) + self.__setattr__(scope + '_total_loss', self.__getattribute__(scope + '_value_loss') + self.__getattribute__( + scope + '_policy_loss') + self.__getattribute__(scope + '_reg')) + self.__setattr__(scope + '_update_ops', tf.get_collection(tf.GraphKeys.UPDATE_OPS, scope=scope)) + self.__setattr__(scope + '_var_list', tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=scope)) + with tf.control_dependencies(self.__getattribute__(scope + '_update_ops')): + self.__setattr__(scope + '_train_op', + tf.train.AdamOptimizer(1e-4).minimize(self.__getattribute__(scope + '_total_loss'), + var_list=self.__getattribute__(scope + '_var_list'))) + self.__setattr__(scope + '_saver', + tf.train.Saver(max_to_keep=0, var_list=self.__getattribute__(scope + '_var_list'))) def __call__(self, state): """ @@ -154,15 +176,20 @@ class ResNet(object): :return: a list of tensor, the predicted value and policy given the history and color """ # Note : maybe we can use it for isolating test of MCTS - #prob = [1.0 / self.action_num] * self.action_num - #return [prob, np.random.uniform(-1, 1)] + # prob = [1.0 / self.action_num] * self.action_num + # return [prob, np.random.uniform(-1, 1)] history, color = state if len(history) != self.history_length: raise ValueError( 'The length of history cannot meet the need of the model, given {}, need {}'.format(len(history), self.history_length)) eval_state = self._history2state(history, color) - return self.sess.run([self.prob, self.v], feed_dict={self.x: eval_state, self.is_training: False}) + if color == +1: + return self.sess.run([self.black_prob, self.black_v], + feed_dict={self.x: eval_state, self.is_training: False}) + if color == -1: + return self.sess.run([self.white_prob, self.white_v], + feed_dict={self.x: eval_state, self.is_training: False}) def _history2state(self, history, color): """ @@ -174,10 +201,12 @@ class ResNet(object): """ state = np.zeros([1, self.board_size, self.board_size, 2 * self.history_length + 1]) for i in range(self.history_length): - state[0, :, :, i] = np.array(np.array(history[i]).flatten() == np.ones(self.board_size ** 2)).reshape(self.board_size, - self.board_size) + state[0, :, :, i] = np.array(np.array(history[i]).flatten() == np.ones(self.board_size ** 2)).reshape( + self.board_size, + self.board_size) state[0, :, :, i + self.history_length] = np.array( - np.array(history[i]).flatten() == -np.ones(self.board_size ** 2)).reshape(self.board_size, self.board_size) + np.array(history[i]).flatten() == -np.ones(self.board_size ** 2)).reshape(self.board_size, + self.board_size) # TODO: need a config to specify the BLACK and WHITE if color == +1: state[0, :, :, 2 * self.history_length] = np.ones([self.board_size, self.board_size]) @@ -187,19 +216,27 @@ class ResNet(object): # TODO: design the interface between the environment and training def train(self, mode='memory', *args, **kwargs): + """ + The method to train the network + + :param target: a string, which to optimize, can only be "both", "black" and "white" + :param mode: a string, how to optimize, can only be "memory" and "file" + """ if mode == 'memory': pass if mode == 'file': self._train_with_file(data_path=kwargs['data_path'], batch_size=kwargs['batch_size'], - checkpoint_path=kwargs['checkpoint_path']) + save_path=kwargs['save_path']) - def _train_with_file(self, data_path, batch_size, checkpoint_path): + def _train_with_file(self, data_path, batch_size, save_path): # check if the path is valid if not os.path.exists(data_path): raise ValueError("{} doesn't exist".format(data_path)) - self.checkpoint_path = checkpoint_path - if not os.path.exists(self.checkpoint_path): - os.mkdir(self.checkpoint_path) + self.save_path = save_path + if not os.path.exists(self.save_path): + os.mkdir(self.save_path) + os.mkdir(self.save_path + 'black') + os.mkdir(self.save_path + 'white') new_file_list = [] all_file_list = [] @@ -227,7 +264,8 @@ class ResNet(object): else: start_time = time.time() for i in range(batch_size): - priority = np.array(self.training_data['length']) / (0.0 + np.sum(np.array(self.training_data['length']))) + priority = np.array(self.training_data['length']) / ( + 0.0 + np.sum(np.array(self.training_data['length']))) game_num = np.random.choice(self.window_length, 1, p=priority)[0] state_num = np.random.randint(self.training_data['length'][game_num]) rotate_times = np.random.randint(4) @@ -237,11 +275,15 @@ class ResNet(object): self._preprocession(self.training_data['states'][game_num][state_num], reflect_times, reflect_orientation, rotate_times)) training_data['probs'].append(np.concatenate( - [self._preprocession(self.training_data['probs'][game_num][state_num][:-1].reshape(self.board_size, self.board_size, 1), reflect_times, - reflect_orientation, rotate_times).reshape(1, self.board_size**2), self.training_data['probs'][game_num][state_num][-1].reshape(1,1)], axis=1)) + [self._preprocession( + self.training_data['probs'][game_num][state_num][:-1].reshape(self.board_size, + self.board_size, 1), + reflect_times, + reflect_orientation, rotate_times).reshape(1, self.board_size ** 2), + self.training_data['probs'][game_num][state_num][-1].reshape(1, 1)], axis=1)) training_data['winner'].append(self.training_data['winner'][game_num][state_num].reshape(1, 1)) value_loss, policy_loss, reg, _ = self.sess.run( - [self.value_loss, self.policy_loss, self.reg, self.train_op], + [self.black_value_loss, self.black_policy_loss, self.black_reg, self.black_train_op], feed_dict={self.x: np.concatenate(training_data['states'], axis=0), self.z: np.concatenate(training_data['winner'], axis=0), self.pi: np.concatenate(training_data['probs'], axis=0), @@ -252,8 +294,11 @@ class ResNet(object): value_loss, policy_loss, reg)) if iters % self.save_freq == 0: - save_path = "Iteration{}.ckpt".format(iters) - self.saver.save(self.sess, self.checkpoint_path + save_path) + ckpt_file = "Iteration{}.ckpt".format(iters) + self.black_saver.save(self.sess, self.save_path + 'black/' + ckpt_file) + self.sess.run(self.update) + self.white_saver.save(self.sess, self.save_path + 'white/' + ckpt_file) + for key in training_data.keys(): training_data[key] = [] iters += 1 @@ -342,5 +387,5 @@ class ResNet(object): if __name__ == "__main__": - model = ResNet(board_size=9, action_num=82, history_length=8) - model.train("file", data_path="./data/", batch_size=128, checkpoint_path="./checkpoint/") + model = ResNet(board_size=8, action_num=65, history_length=1, black_checkpoint_path="./checkpoint/black", white_checkpoint_path="./checkpoint/white") + model.train(mode="file", data_path="./data/", batch_size=128, save_path="./checkpoint/") From c2775df8e676ad4a5e1fea76bc781324b5625d54 Mon Sep 17 00:00:00 2001 From: rtz19970824 Date: Tue, 9 Jan 2018 20:09:48 +0800 Subject: [PATCH 2/4] modify game.py for multi-player --- AlphaGo/game.py | 26 ++++++++++++++++++-------- AlphaGo/model.py | 6 +++--- AlphaGo/play.py | 1 + 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/AlphaGo/game.py b/AlphaGo/game.py index c105522..abb6331 100644 --- a/AlphaGo/game.py +++ b/AlphaGo/game.py @@ -12,6 +12,7 @@ import numpy as np import sys, os import model from collections import deque + sys.path.append(os.path.join(os.path.dirname(__file__), os.path.pardir)) from tianshou.core.mcts.mcts import MCTS @@ -19,6 +20,7 @@ import go import reversi import time + class Game: ''' Load the real game and trained weights. @@ -26,10 +28,11 @@ class Game: TODO : Maybe merge with the engine class in future, currently leave it untouched for interacting with Go UI. ''' - def __init__(self, name=None, role=None, debug=False, checkpoint_path=None): + + def __init__(self, name=None, role=None, debug=False, black_checkpoint_path=None, white_checkpoint_path=None): self.name = name - if role is None: - raise ValueError("Need a role!") + if role is None: + raise ValueError("Need a role!") self.role = role self.debug = debug if self.name == "go": @@ -49,8 +52,9 @@ class Game: else: raise ValueError(name + " is an unknown game...") - self.evaluator = model.ResNet(self.size, self.size ** 2 + 1, history_length=self.history_length, - checkpoint_path=checkpoint_path) + self.model = model.ResNet(self.size, self.size ** 2 + 1, history_length=self.history_length, + black_checkpoint_path=black_checkpoint_path, + white_checkpoint_path=white_checkpoint_path) self.latest_boards = deque(maxlen=self.history_length) for _ in range(self.history_length): self.latest_boards.append(self.board) @@ -72,7 +76,12 @@ class Game: self.komi = k def think(self, latest_boards, color): - mcts = MCTS(self.game_engine, self.evaluator, [latest_boards, color], + if color == +1: + role = 'black' + if color == -1: + role = 'white' + evaluator = lambda state:self.model(role, state) + mcts = MCTS(self.game_engine, evaluator, [latest_boards, color], self.size ** 2 + 1, role=self.role, debug=self.debug, inverse=True) mcts.search(max_step=100) if self.debug: @@ -98,7 +107,8 @@ class Game: if self.name == "reversi": res = self.game_engine.executor_do_move(self.history, self.latest_boards, self.board, color, vertex) if self.name == "go": - res = self.game_engine.executor_do_move(self.history, self.history_set, self.latest_boards, self.board, color, vertex) + res = self.game_engine.executor_do_move(self.history, self.history_set, self.latest_boards, self.board, + color, vertex) return res def think_play_move(self, color): @@ -128,8 +138,8 @@ class Game: print('') sys.stdout.flush() + if __name__ == "__main__": game = Game(name="reversi", role="black", checkpoint_path=None) game.debug = True game.think_play_move(utils.BLACK) - diff --git a/AlphaGo/model.py b/AlphaGo/model.py index 88fd199..7741eb6 100644 --- a/AlphaGo/model.py +++ b/AlphaGo/model.py @@ -168,7 +168,7 @@ class ResNet(object): self.__setattr__(scope + '_saver', tf.train.Saver(max_to_keep=0, var_list=self.__getattribute__(scope + '_var_list'))) - def __call__(self, state): + def __call__(self, role, state): """ :param history: a list, the history @@ -184,10 +184,10 @@ class ResNet(object): 'The length of history cannot meet the need of the model, given {}, need {}'.format(len(history), self.history_length)) eval_state = self._history2state(history, color) - if color == +1: + if role == 'black': return self.sess.run([self.black_prob, self.black_v], feed_dict={self.x: eval_state, self.is_training: False}) - if color == -1: + if role == 'white': return self.sess.run([self.white_prob, self.white_v], feed_dict={self.x: eval_state, self.is_training: False}) diff --git a/AlphaGo/play.py b/AlphaGo/play.py index 5aaa6a2..e419f5b 100644 --- a/AlphaGo/play.py +++ b/AlphaGo/play.py @@ -24,6 +24,7 @@ class Data(object): def reset(self): self.__init__() + if __name__ == '__main__': """ Starting two different players which load network weights to evaluate the winning ratio. From f425085e0a8e5af8714bbff7fa1a6f9a59acbb10 Mon Sep 17 00:00:00 2001 From: Dong Yan Date: Tue, 9 Jan 2018 21:16:35 +0800 Subject: [PATCH 3/4] fix the tf assign error of copy the trained variable from black to white --- AlphaGo/model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AlphaGo/model.py b/AlphaGo/model.py index 7741eb6..670837d 100644 --- a/AlphaGo/model.py +++ b/AlphaGo/model.py @@ -122,7 +122,7 @@ class ResNet(object): print('Successfully loaded') else: raise ValueError("No model in path {}".format(white_checkpoint_path)) - self.update = [tf.assign(black_params, white_params) for black_params, white_params in + self.update = [tf.assign(white_params, black_params) for black_params, white_params in zip(self.black_var_list, self.white_var_list)] # training hyper-parameters: From 5482815de68c87ca2c54a299af4516d5fe621869 Mon Sep 17 00:00:00 2001 From: Dong Yan Date: Wed, 10 Jan 2018 23:27:17 +0800 Subject: [PATCH 4/4] replace two isolated player process by two different set of variables in the tf graph --- AlphaGo/game.py | 25 +++++++------- AlphaGo/go.py | 3 +- AlphaGo/play.py | 84 ++++++++--------------------------------------- AlphaGo/player.py | 42 ------------------------ 4 files changed, 26 insertions(+), 128 deletions(-) delete mode 100644 AlphaGo/player.py diff --git a/AlphaGo/game.py b/AlphaGo/game.py index abb6331..da84fb3 100644 --- a/AlphaGo/game.py +++ b/AlphaGo/game.py @@ -29,11 +29,8 @@ class Game: currently leave it untouched for interacting with Go UI. ''' - def __init__(self, name=None, role=None, debug=False, black_checkpoint_path=None, white_checkpoint_path=None): + def __init__(self, name=None, debug=False, black_checkpoint_path=None, white_checkpoint_path=None): self.name = name - if role is None: - raise ValueError("Need a role!") - self.role = role self.debug = debug if self.name == "go": self.size = 9 @@ -41,7 +38,7 @@ class Game: self.history_length = 8 self.history = [] self.history_set = set() - self.game_engine = go.Go(size=self.size, komi=self.komi, role=self.role) + self.game_engine = go.Go(size=self.size, komi=self.komi) self.board = [utils.EMPTY] * (self.size ** 2) elif self.name == "reversi": self.size = 8 @@ -76,20 +73,22 @@ class Game: self.komi = k def think(self, latest_boards, color): - if color == +1: + if color == utils.BLACK: role = 'black' - if color == -1: + elif color == utils.WHITE: role = 'white' + else: + raise ValueError("game.py[think] - unknown color : {}".format(color)) evaluator = lambda state:self.model(role, state) mcts = MCTS(self.game_engine, evaluator, [latest_boards, color], - self.size ** 2 + 1, role=self.role, debug=self.debug, inverse=True) + self.size ** 2 + 1, role=role, debug=self.debug, inverse=True) mcts.search(max_step=100) if self.debug: file = open("mcts_debug.log", 'ab') - np.savetxt(file, mcts.root.Q, header="\n" + self.role + " Q value : ", fmt='%.4f', newline=", ") - np.savetxt(file, mcts.root.W, header="\n" + self.role + " W value : ", fmt='%.4f', newline=", ") - np.savetxt(file, mcts.root.N, header="\n" + self.role + " N value : ", fmt="%d", newline=", ") - np.savetxt(file, mcts.root.prior, header="\n" + self.role + " prior : ", fmt='%.4f', newline=", ") + np.savetxt(file, mcts.root.Q, header="\n" + role + " Q value : ", fmt='%.4f', newline=", ") + np.savetxt(file, mcts.root.W, header="\n" + role + " W value : ", fmt='%.4f', newline=", ") + np.savetxt(file, mcts.root.N, header="\n" + role + " N value : ", fmt="%d", newline=", ") + np.savetxt(file, mcts.root.prior, header="\n" + role + " prior : ", fmt='%.4f', newline=", ") file.close() temp = 1 prob = mcts.root.N ** temp / np.sum(mcts.root.N ** temp) @@ -140,6 +139,6 @@ class Game: if __name__ == "__main__": - game = Game(name="reversi", role="black", checkpoint_path=None) + game = Game(name="reversi", checkpoint_path=None) game.debug = True game.think_play_move(utils.BLACK) diff --git a/AlphaGo/go.py b/AlphaGo/go.py index cf6b7aa..18a1a08 100644 --- a/AlphaGo/go.py +++ b/AlphaGo/go.py @@ -18,7 +18,6 @@ class Go: def __init__(self, **kwargs): self.size = kwargs['size'] self.komi = kwargs['komi'] - self.role = kwargs['role'] def _flatten(self, vertex): x, y = vertex @@ -332,7 +331,7 @@ class Go: if __name__ == "__main__": - go = Go(size=9, komi=3.75, role = utils.BLACK) + go = Go(size=9, komi=3.75) endgame = [ 1, 0, 1, 0, 1, 1, -1, 0, -1, 1, 1, 1, 1, 1, 1, -1, -1, -1, diff --git a/AlphaGo/play.py b/AlphaGo/play.py index e419f5b..66cfef0 100644 --- a/AlphaGo/play.py +++ b/AlphaGo/play.py @@ -1,10 +1,10 @@ import argparse -import subprocess import sys import re -import Pyro4 import time import os +from game import Game +from engine import GTPEngine import utils from time import gmtime, strftime @@ -48,65 +48,13 @@ if __name__ == '__main__': if args.white_weight_path is not None and (not os.path.exists(args.white_weight_path)): raise ValueError("Can't find the network weights for white player.") - # kill the old server - # kill_old_server = subprocess.Popen(['killall', 'pyro4-ns']) - # print "kill the old pyro4 name server, the return code is : " + str(kill_old_server.wait()) - # time.sleep(1) - - # start a name server if no name server exists - if len(os.popen('ps aux | grep pyro4-ns | grep -v grep').readlines()) == 0: - start_new_server = subprocess.Popen(['pyro4-ns', '&']) - print("Start Name Sever : " + str(start_new_server.pid)) # + str(start_new_server.wait()) - time.sleep(1) - - # start two different player with different network weights. - server_list = subprocess.check_output(['pyro4-nsc', 'list']) - current_time = strftime("%Y%m%d_%H%M%S", gmtime()) - - black_role_name = 'black' + current_time - white_role_name = 'white' + current_time - - black_player = subprocess.Popen( - ['python', '-u', 'player.py', '--game=' + args.game, '--role=' + black_role_name, - '--checkpoint_path=' + str(args.black_weight_path), '--debug=' + str(args.debug)], - stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - bp_output = black_player.stdout.readline() - bp_message = bp_output - # '' means player.py failed to start, "Start requestLoop" means player.py start successfully - while bp_output != '' and "Start requestLoop" not in bp_output: - bp_output = black_player.stdout.readline() - bp_message += bp_output - print("============ " + black_role_name + " message ============" + "\n" + bp_message), - - white_player = subprocess.Popen( - ['python', '-u', 'player.py', '--game=' + args.game, '--role=' + white_role_name, - '--checkpoint_path=' + str(args.white_weight_path), '--debug=' + str(args.debug)], - stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - wp_output = white_player.stdout.readline() - wp_message = wp_output - while wp_output != '' and "Start requestLoop" not in wp_output: - wp_output = white_player.stdout.readline() - wp_message += wp_output - print("============ " + white_role_name + " message ============" + "\n" + wp_message), - - server_list = "" - while (black_role_name not in server_list) or (white_role_name not in server_list): - if python_version < (3, 0): - # TODO : @renyong what is the difference between those two options? - server_list = subprocess.check_output(['pyro4-nsc', 'list']) - else: - server_list = subprocess.check_output(['pyro4-nsc', 'list']) - print("Waiting for the server start...") - time.sleep(1) - print(server_list) - print("Start black player at : " + str(black_player.pid)) - print("Start white player at : " + str(white_player.pid)) + game = Game(name=args.game, + black_checkpoint_path=args.black_weight_path, + white_checkpoint_path=args.white_weight_path, + debug=args.debug) + engine = GTPEngine(game_obj=game, name='tianshou', version=0) data = Data() - player = [None] * 2 - player[0] = Pyro4.Proxy("PYRONAME:" + black_role_name) - player[1] = Pyro4.Proxy("PYRONAME:" + white_role_name) - role = ["BLACK", "WHITE"] color = ['b', 'w'] @@ -119,7 +67,7 @@ if __name__ == '__main__': game_num = 0 try: while True: - # while game_num < evaluate_rounds: + #while game_num < evaluate_rounds: start_time = time.time() num = 0 pass_flag = [False, False] @@ -127,7 +75,7 @@ if __name__ == '__main__': # end the game if both palyer chose to pass, or play too much turns while not (pass_flag[0] and pass_flag[1]) and num < size[args.game] ** 2 * 2: turn = num % 2 - board = player[turn].run_cmd(str(num) + ' show_board') + board = engine.run_cmd(str(num) + ' show_board') board = eval(board[board.index('['):board.index(']') + 1]) for i in range(size[args.game]): for j in range(size[args.game]): @@ -135,7 +83,7 @@ if __name__ == '__main__': print "\n", data.boards.append(board) start_time = time.time() - move = player[turn].run_cmd(str(num) + ' genmove ' + color[turn])[:-1] + move = engine.run_cmd(str(num) + ' genmove ' + color[turn])[:-1] print("\n" + role[turn] + " : " + str(move)), num += 1 match = re.search(pattern, move) @@ -147,21 +95,19 @@ if __name__ == '__main__': # print "no match" play_or_pass = ' PASS' pass_flag[turn] = True - result = player[1 - turn].run_cmd(str(num) + ' play ' + color[turn] + ' ' + play_or_pass + '\n') - prob = player[turn].run_cmd(str(num) + ' get_prob') + prob = engine.run_cmd(str(num) + ' get_prob') prob = space.sub(',', prob[prob.index('['):prob.index(']') + 1]) prob = prob.replace('[,', '[') prob = prob.replace('],', ']') prob = eval(prob) data.probs.append(prob) - score = player[0].run_cmd(str(num) + ' get_score') + score = engine.run_cmd(str(num) + ' get_score') print("Finished : {}".format(score.split(" ")[1])) if eval(score.split(" ")[1]) > 0: data.winner = utils.BLACK if eval(score.split(" ")[1]) < 0: data.winner = utils.WHITE - player[0].run_cmd(str(num) + ' clear_board') - player[1].run_cmd(str(num) + ' clear_board') + engine.run_cmd(str(num) + ' clear_board') file_list = os.listdir(args.data_path) current_time = strftime("%Y%m%d_%H%M%S", gmtime()) if os.path.exists(args.data_path + current_time + ".pkl"): @@ -173,7 +119,3 @@ if __name__ == '__main__': game_num += 1 except KeyboardInterrupt: pass - - subprocess.call(["kill", "-9", str(black_player.pid)]) - subprocess.call(["kill", "-9", str(white_player.pid)]) - print("Kill all player, finish all game.") diff --git a/AlphaGo/player.py b/AlphaGo/player.py deleted file mode 100644 index bd2a2d1..0000000 --- a/AlphaGo/player.py +++ /dev/null @@ -1,42 +0,0 @@ -import argparse -import Pyro4 -from game import Game -from engine import GTPEngine - -@Pyro4.expose -class Player(object): - """ - This is the class which defines the object called by Pyro4 (Python remote object). - It passes the command to our engine, and return the result. - """ - def __init__(self, **kwargs): - self.role = kwargs['role'] - self.engine = kwargs['engine'] - - def run_cmd(self, command): - return self.engine.run_cmd(command) - -if __name__ == '__main__': - parser = argparse.ArgumentParser() - parser.add_argument("--checkpoint_path", type=str, default="None") - parser.add_argument("--role", type=str, default="unknown") - parser.add_argument("--debug", type=str, default="False") - parser.add_argument("--game", type=str, default="go") - args = parser.parse_args() - if args.checkpoint_path == 'None': - args.checkpoint_path = None - game = Game(name=args.game, role=args.role, - checkpoint_path=args.checkpoint_path, - debug=eval(args.debug)) - engine = GTPEngine(game_obj=game, name='tianshou', version=0) - - daemon = Pyro4.Daemon() # make a Pyro daemon - ns = Pyro4.locateNS() # find the name server - player = Player(role=args.role, engine=engine) - print("Init " + args.role + " player finished") - uri = daemon.register(player) # register the greeting maker as a Pyro object - print("Start on name " + args.role) - ns.register(args.role, uri) # register the object with a name in the name server - print("Start requestLoop " + str(uri)) - daemon.requestLoop() # start the event loop of the server to wait for calls -