Exemple #1
0
def test():
    conf = mconfig.EvaluateConfig()
    model = RenjuModel(conf)
    model.load('currentModel')

    while True:
        gameBoard = gameCython.game_state(conf.common.game_board_size)
        mctsTree = mcts.Mcts(conf, 1, model)
        playerIndex = -1
        while True:
            print(gameBoard.print_beautiful() + '\n')

            if playerIndex < 0:
                testerMove = input()  # type: str
                xstr, ystr = testerMove.split(',')
                move = mcts.Hand(x=int(xstr) - 1, y=int(ystr) - 1)
            else:
                move, policy_ = mctsTree.search_move(autoMoveIntoChild=False)

            if not gameBoard.play(move.x, move.y):
                break
            if gameBoard.finished:
                break
            playerIndex *= -1
            mctsTree.move_to_child(move.x, move.y)

        print(gameBoard.print_beautiful() + '\n')
        if gameBoard.finished:
            print('Game finished! The winner is ' + str(gameBoard.winner))
        else:
            print('Game didn\' finish normally')
Exemple #2
0
 def __init__(self, config, playerIndex, model, parallelSize=16):
     self.root_node = MctsNode(config.common.game_board_size)
     self.root_node.game_board = game.game_state(
         config.common.game_board_size)
     self.config = config
     self.model = model
     self.step = 0
     self.loop = asyncio.get_event_loop()
     self.playerIndex = playerIndex
     self.predictionQueue = Queue(parallelSize)
     self.searchingNodes = 0
     self.sem = asyncio.Semaphore(parallelSize)
    def single_self_train(self):
        if self.bestModel == None:
            self.bestModel = self.load_model()  #Type : RenjuModel
        #get a mcts tree model
        self.mctsTree = [
            mcts.Mcts(self.config, -1, self.bestModel),
            mcts.Mcts(self.config, 1, self.bestModel)
        ]
        #start a game
        gameBoard = game.game_state(self.config.common.game_board_size)
        #store all moves into a list.
        moveHistory = list()

        policyHistory = list()

        playerIndex = -1
        #continue the game, until player or board says no more.
        while True:
            print(gameBoard.print_beautiful() + '\n')
            sys.stdout.flush()
            mctsIndex = 0
            if playerIndex > 0:
                mctsIndex = 1
            move, policy_ = self.mctsTree[mctsIndex].search_move(
                autoMoveIntoChild=False)
            if not gameBoard.play(move.x, move.y):
                break
            moveHistory.append(move)
            policyHistory.append(policy_.tolist())
            if gameBoard.finished:
                break
            playerIndex *= -1
            for m in self.mctsTree:
                m.move_to_child(move.x, move.y)

        print(gameBoard.print_beautiful() + '\n')
        if gameBoard.finished:
            logger.warning('Game finished! The winner is ' +
                           str(gameBoard.winner))
            gameHistory = {
                'policy': policyHistory,
                'move': moveHistory,
                'winner': gameBoard.winner
            }
            self.save_game_history(gameHistory)
        else:
            logger.warning('Game didn\' finish normally')
Exemple #4
0
def test():
    import configs.normal as mconfig

    class mockModel():
        def __init__(self):
            pass

        def predict(self, gamestate):
            width = mconfig.CommonConfig().game_board_size
            policy = np.random.rand(width, width)
            policy /= np.sum(policy)

            value = np.resize([0], (gamestate.shape[0]))

            #   for i in range(gamestate.shape[0]):
            #      if gamestate[i][1][5][6] > 0.5:
            #         value[i] = -1

            return np.resize(policy, (gamestate.shape[0], width, width)), value

    m = mockModel()
    # import model
    #m = model.RenjuModel(mconfig.EvaluateConfig())
    # m.load('currentModel')
    #m.load('backupModels/model2018-1-9-8-26-11')
    while True:
        tm = Mcts(mconfig.EvaluateConfig(), -1, m)
        board = game.game_state(
            mconfig.EvaluateConfig().common.game_board_size)
        #
        for i in range(4):
            board.play(5, i)
            board.play(i * 2, 3)
        #board.play(8,8)
        print(board.print_beautiful())
        tm.set_current_game_state(board)
        action, policy = tm.search_move()
        print(policy)
        print(tm.root_node.childP)
        input()
 def single_versus(self):
     gameBoard = gameCython.game_state(EvaluateConfig().common.game_board_size)
     mctsTree = [mcts.Mcts(EvaluateConfig(),-1,self.m1),mcts.Mcts(EvaluateConfig(),1,self.m2)]
     playerIndex = -1
     while True:
         print(gameBoard.print_beautiful() + '\n')
         mctsIndex = 0
         if playerIndex > 0 :
             mctsIndex = 1
         move,policy_ = mctsTree[mctsIndex].search_move(autoMoveIntoChild = False)
         if not gameBoard.play(move.x ,move.y):
             break
         if gameBoard.finished:
             break
         playerIndex*= -1
         for m in mctsTree:
             m.move_to_child(move.x,move.y)
     print(gameBoard.print_beautiful() + '\n')
     if gameBoard.finished:
         print('Game finished! The winner is ' + str(gameBoard.winner))
         return gameBoard.winner
     else:
         print('Game didn\' finish normally')
Exemple #6
0
 def restart_game(self, otherPlayerIndex=-1):
     self.game = game_state(self.config.common.game_board_size)
     self.mcts = Mcts(modelConfig(), -otherPlayerIndex, self.model)
     self.aiplayer = -otherPlayerIndex