def make_pachi_policy(board, engine_type='uct', threads=1, pachi_timestr=''): engine = pachi_py.PyPachiEngine(board, engine_type, six.b('threads=%d' % threads)) def pachi_policy(curr_state, prev_state, prev_action): if prev_state is not None: assert engine.curr_board == prev_state.board, 'Engine internal board is inconsistent with provided board. The Pachi engine must be called consistently as the game progresses.' prev_coord = _action_to_coord(prev_state.board, prev_action) engine.notify(prev_coord, prev_state.color) engine.curr_board.play_inplace(prev_coord, prev_state.color) out_coord = engine.genmove(curr_state.color, pachi_timestr) out_action = _coord_to_action(curr_state.board, out_coord) engine.curr_board.play_inplace(out_coord, curr_state.color) return out_action return pachi_policy
def get(self): """ Get new move from PACHI given board matrix and stone color. --- tags: - move consumes: - application/json produces: - application/json parameters: - in: body name: body schema: properties: board_format: description: format of the board, matrix or ij_history board: description: Board data in two dimentional array type: array items: type: array items: integer board_size: description: board size stone_color: description: Stone color for the generated move. 2:Black or 1:White type: integer return_board: description: Return the board in matrix. 0:False or 1:True. type: integer example: |- {"stone_color": 1,"board": [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,0,0,0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],"return_board": 1} """ try: req_data = request.get_json(force=True) print(req_data) except: return make_response(jsonify({"error":"Invalid payload"}), 500) board_format = req_data.get('board_format', 'matrix') board = req_data.get('board',[[]]) stone_color = req_data.get('stone_color',0) return_board = req_data.get('return_board',False) pachi_board = pachi_py.CreateBoard(19) if board_format == 'matrix': self.make_board_matrix(pachi_board, board) elif board_format == 'ij_history': stone_color = 2 if stone_color=='Black' else 1 self.make_board_ij_history(pachi_board, board) else: return make_response(jsonify({"error":"Invalid payload"}), 500) _pe=pachi_py.PyPachiEngine(pachi_board,'uct','threads=8,playout=light,maximize_score') next_move = _pe.genmove(stone_color,'') _pe.curr_board.play_inplace(next_move, stone_color) print(_pe.curr_board.coord_to_ij(next_move)) resp = jsonify({ 'move_str':_pe.curr_board.coord_to_str(next_move), 'move_ij':_pe.curr_board.coord_to_ij(next_move), 'stone_color':stone_color, 'board':_pe.curr_board.encode()[0].tolist() if return_board else None}) del _pe return make_response(resp,200)
PATH = "./model.pt" go_env = gym.make('gym_go:go-v0', size=5, reward_method='heuristic') state = go_env.reset() # Reset environment and record the starting state # Train 5x5 model # Policy_grad(100, 10, 0.001, 0.99, go_env, False, True) # Load trained model policy = torch.load(PATH) b = pachi_py.CreateBoard(5) engine = pachi_py.PyPachiEngine(b, b'uct', b'') # pachi_move = engine.genmove(pachi_py.WHITE, b'0') # b.play_inplace(pachi_move, pachi_py.WHITE) # b.play_inplace(9, pachi_py.BLACK) # engine.notify(9, pachi_py.BLACK) # # pachi_move = engine.genmove(pachi_py.WHITE, b'2') # print(pachi_move) # # # print(b.coord_to_ij(8)) # b.play_inplace(9, pachi_py.BLACK) # b.play_inplace(pachi_move, pachi_py.WHITE) done = False time = 0