def play_seq(self, seq, verbose=False): for s in seq: (c, p) = c_cd2cp(s, self.N) res = self.play(c, p) if verbose: eprint(self) eprint('res:{} ko:{}'.format(res, self.ko)) eprint('seq:{}', seq) return res
def test_inf_play(verbose=True): board = Board(9) eprint(board) while True: eprint("Enter move (e.g: 'black A1' or '' for passing): ", end='') mov = raw_input() if mov == '': continue c, p = c_cd2cp(mov, board.N) res = board.play(c, p) if verbose: eprint(board) eprint('res:{} ko:{}'.format(res, board.ko))
def test_eye(): N = 9 board = Board(N) seq1 = ['B C4', 'B D3', 'B D5', 'B E4', 'W E6', 'W F5', 'W F7', 'W G6'] board.play_seq(seq1) eprint(board) seq2=['B D4','W D4','B F6','W F6','B E5','W E5'] for mov in seq2: c,p=utils.c_cd2cp(mov,N) eprint(c,p) eprint('mov:{} is_my_eye:{}'.format(mov, board.is_my_eye(c, p)))
def test_p2cd(): N = 9 board = Board(N) seq1 = ['B C5', 'B D3', 'B D5', 'B E4', 'W E6', 'W F5', 'W F7', 'W G6'] board.play_seq(seq1) c,p=utils.c_cd2cp(seq1[0],N) a=utils.p2a(p,N) cd=utils.p2cd(p,N) eprint('a:{} p:{} cd:{} '.format(a,p,cd)) eprint(board)
def test_predict(): with tf.Graph().as_default(): boardsize = 19 num_channels = 4 board = Board(boardsize) #seq = ['b B3', 'b C4', 'b C2', 'B D3', # 'W D4', 'w E3', 'w D2'] seq = ['b Q16'] board.play_seq(seq) print(board) #move='Q16' # get input to the neural network c, p = utils.c_cd2cp(seq[0], boardsize) board_reg_str = str(board.create_board_register(utils.Color.WHITE, 4)) #data = data.reshape(num_images, NUM_CHANNELS, IMAGE_SIZE, IMAGE_SIZE) #data = data.transpose(0, 2, 3, 1) data = np.frombuffer(board_reg_str, dtype=np.uint8) data = data.astype(np.float32) data = data.reshape(1, num_channels, boardsize, boardsize) print(data) data = data.transpose(0, 2, 3, 1) print(data) # images=tf.placeholder(tf.float64,shape=[None,num_channels,boardsize,boardsize]) logits = cnn.inference_layer(data, boardsize, num_channels, 11) sm_output = tf.nn.softmax(logits) init = tf.initialize_all_variables() saver = tf.train.Saver() with tf.Session() as sess: # Restore variables from disk. saver.restore(sess, "KGS_train_l13/model.ckpt-12000") print("Model restored.") output = sess.run(sm_output) a = np.argmax(output) print(output, utils.p2cd(utils.a2p(a, boardsize), boardsize)) #get top 5 prediction top_5 = output[0].argsort()[::-1][:5] print(top_5) for e in top_5: print(utils.p2cd(utils.a2p(e, boardsize), boardsize)) print(board)
def gtp_session(self): quit = False while not quit: # read and respond # read command of the form: # [id] command_name [arguments]\n line = raw_input().strip() if line == '': continue if line[0] == '#': continue line_list = line.split() # print(line) # print(line_list) try: int(line_list[0]) id = line_list[0] cmd = line_list[1] args = line_list[2:] except ValueError: id = '' cmd = line_list[0] args = line_list[1:] if self.verbose: eprint('line:{} command:{} args:{}'.format(line, cmd, args)) # process command and return response of the form: # =[id] result # ?[id] error_message success = True result = '' if cmd == 'protocol_version': result = '2' elif cmd == 'name': result = 'ndsgo' elif cmd == 'version': result = '' elif cmd == 'known_command': if len(args) < 1: result = 'syntax error' success = False else: command = args[0] if command in self.known_commands: result = 'true' else: result = 'false' elif cmd == 'list_commands': result = '' for e in self.known_commands: result += e + "\n" result = result[:-1] # remove the last \n elif cmd == 'quit': result = '' quit = True elif cmd == 'boardsize': # TODO: test this code if len(args) < 1: result = 'syntax error' success = False else: try: size = int(args[0]) if self.player.player_file.has_key(size): if self.Player_class is CNNPlayer: #success = False #result = 'unacceptable size' #eprint('Cannot change size because this is not implmeneted for CNNPlayer. ' # 'It would cause an error') success = True elif self.Player_class is MCPlayerQ: self.player = self.Player_class(size, epsilon=0.0) self.player.load_Q( self.player.player_file[size]) else: success = False result = 'unacceptable size' except Exception as err: if self.verbose: eprint( 'Exception in gtp_engine.boardsize. Error:{}'. format(err)) success = False result = 'syntax error' elif cmd == 'clear_board': # board.clear_board() function is called inside player.new_game() # we call new_game() since if we are learning we need to reset the episode_history for the game self.player.new_game() elif cmd == 'komi': if len(args) < 1: result = 'syntax error' success = False else: try: new_komi = float(args[0]) self.player.board.komi = new_komi except Exception as err: if self.verbose: eprint('Exception in gtp_engine.komi. Error:{}'. format(err)) success = False result = 'syntax error' elif cmd == 'play': # e.g. play black A1 if len(args) < 2: result = 'syntax error' success = False else: eprint('IN PLAY: {} {}'.format(args[0], args[1])) if args[1] == 'PASS': eprint('in play. Move is PASS.') self.last_move_pass = True success = True else: try: (c, p) = c_cd2cp(args[0] + ' ' + args[1], self.player.board.N) res = self.player.board.play(c, p) if self.verbose: eprint("args:{} c:{} p:{} res:{}".format( args, c, p, res)) if res < 0: if self.verbose: eprint( "Illegal move in gtp_engine. play. res:{}" .format(res)) success = False result = 'illegal move' else: result = '' except Exception as err: if self.verbose: eprint( 'Exception in gtp_engine.play(). Error:{}'. format(err)) success = False result = 'syntax error' elif cmd == 'genmove': if len(args) < 1: result = 'syntax error' success = False else: #FIRST VERIFY IF THE OTHER PLAYER HAS NOT PASSED if self.last_move_pass == True: eprint('Since the other program passed we also pass.') eprint( 'This is trusting the other program knows better when the game has finished.' ) self.last_move_pass = False result = 'pass' else: color = args[0].lower() if color == 'black': color = 'b' if color == 'white': color = 'w' if color != 'b' and color != 'w': success = False result = 'syntax error' else: c = color2c(color) mov = self.player.genmove(c) if mov is None: result = 'pass' else: result = p2cd(mov, self.player.board.N) elif cmd == 'undo': if len(self.player.board.move_history) == 0: success = False result = 'cannot undo' else: self.player.board.undo() # elif cmd == 'final_score': # score = self.player.board.final_score() # if score > 0: # result = "B+{}".format(score) # elif score < 0: # result = "W+{}".format(abs(score)) # else: # result = '0' elif cmd == 'showboard': result = self.player.board.__str__() else: result = 'unknown command' success = False # Return response of the form # =[id] result (SUCCESS) # ?[id] error_message (FAILURE) print('{}{} {}\n'.format('=' if success else '?', id, result)) sys.stdout.flush()