Ejemplo n.º 1
0
    def _simulateGame(self,
                      useModel: bool = False,
                      preferredPlayer: Player = None) -> GameResult:
        if preferredPlayer is None:
            preferredPlayer = self._player
        history: List[Move] = []
        board: Board = self._operations.createBoard(self._size)
        playerToMove: Player = self._player

        winner: Winner = self._operations.getBoardWinner(board, self._toWin)
        while winner == 'Draw' and self._operations.hasBoardAnyMoves(board):
            # Chose a move (random or use a player model if provided)
            if not useModel or self._operations.isBoardEmpty(
                    board):  # or playerToMove != preferredPlayer:
                moves = self._possibleMoves(board, playerToMove)
                moveIndex = int(min(np.random.exponential(), len(moves) - 1)) if not useModel else \
                    random.randint(0, len(moves) - 1)
                move = moves[moveIndex]
            else:
                move = self._bestMove(board, playerToMove)
            self._performMove(move, board)
            winner = self._operations.getBoardWinner(board, self._toWin)

            # Add the move to the history
            history.append(move)

            # Switch the active player
            playerToMove = getOtherPlayer(playerToMove)

        return history, winner
Ejemplo n.º 2
0
 def __init__(self,
              board: Board,
              operations: BoardOperations,
              toWin: int = 3,
              player: Player = 'X'):
     super().__init__()
     self.board = board
     self._size = len(board)
     self._toWin = toWin if toWin < len(board) else len(board)
     self._player = player
     self._opponent = getOtherPlayer(player)
     self._operations = operations
     self._playerModel = self._createModel()
     self._opponentModel = self._createModel()
Ejemplo n.º 3
0
 def __init__(self,
              board: Board,
              operations: BoardOperations,
              toWin: int = None,
              player: Player = 'X',
              maxDepth: int = -1):
     self._board = board
     self._maxDepth = maxDepth
     self._size = len(board)
     self._toWin = toWin if toWin is not None else self._size
     self._operations = operations
     self._operations3D = Board3DOperations()
     self._player = player
     self._opponent = getOtherPlayer(self._player)
     self._onMove = self._player
Ejemplo n.º 4
0
def main(argv):
    board = board2DOperations.createBoard(argv.size) if argv.dimension == 2 else board3DOperations.createBoard(
        argv.size)
    operations = board2DOperations if argv.dimension == 2 else board3DOperations
    if argv.algorithm == scripts[0]:
        createAndTrainModel(argv, board, operations)
    elif argv.algorithm == scripts[5]:
        with tf.compat.v1.Session() as sess:
            model = createAndTrainModel(argv, board, operations).playerModel()
            outDir = os.getcwd() + '/models/size-' + str(argv.size) + '-to-win-' + str(argv.to_win)
            os.makedirs(outDir, exist_ok=True)
            print(model.output.op.name)
            print(model.outputs)
            print(model.inputs)
            model.save_weights(outDir + '/weights')
            frozen_graph = freeze_session(sess, output_names=[model.output.op.name])
            tf.io.write_graph(frozen_graph, "model", outDir + "/model.bytes", as_text=False)

            # freeze_graph.freeze_graph(input_graph=outDir + '/model.pb',
            #                           input_binary=True,
            #                           input_checkpoint=tf.train.latest_checkpoint(outDir),
            #                           output_node_names=[model.output.op.name],
            #                           output_graph=outDir + '/model.bytes',
            #                           clear_devices=True, initializer_nodes="", input_saver="",
            #                           restore_op_name="save/restore_all", filename_tensor_name="save/Const:0")

    elif argv.algorithm == scripts[1]:
        minmax = MinMax(board, operations, argv.to_win, argv.player, argv.max_depth)
        st = time.time()
        i = 0
        while minmax.isDraw() and operations.hasBoardAnyMoves(board):
            # print(operations.boardToString(board))
            st = time.time()
            move = minmax.bestMove()
            minmax.performMove(move)
            i += 1
            print('Step ' + str(i) + ' in ' + str(time.time() - st))
        # print(operations.boardToString(board))
        print('Winner ' + operations.getBoardWinner(board, argv.to_win))
        print('Game played in ' + str(time.time() - st))
    elif argv.algorithm == scripts[3]:
        baseModel = NetworkModelAdaptive2D(board, operations, argv.to_win, argv.player)
        baseModel.trainModel(baseModel.simulateGames(n=argv.simulations))

        annModel = NetworkModelLarge(board, operations, argv.to_win, argv.player)
        minmax = MinMax(board, operations, argv.to_win, getOtherPlayer(argv.player), argv.max_depth)
        second = minmax

        # second.trainModel(second.simulateGames(n=argv.simulations))

        player: Player = argv.player

        pl = 0
        op = 0
        draw = 0
        for game in range(argv.validations):
            print('Validation ' + str(game + 1))
            while operations.getBoardWinner(board, argv.to_win) == 'Draw' and operations.hasBoardAnyMoves(board):
                model = baseModel if player == argv.player else second
                move = model.bestMove(player)
                baseModel.performMove(move)
                second.performMove(move)
                board = baseModel.board
                # print('Player ' + player + ' moved to ' + str(move))
                # print(operations.boardToString(board))
                player = getOtherPlayer(player)
            # print('Winner is ' + operations.getBoardWinner(board, argv.to_win))
            winner = operations.getBoardWinner(board, argv.to_win)
            if winner == argv.player:
                pl += 1
            elif winner == getOtherPlayer(argv.player):
                op += 1
            else:
                draw += 1
            baseModel.clearBoard()
            second.clearBoard()
            board = baseModel.board
        print("Results for player %s:" % argv.player)
        print("Wins: %d (%.1f%%)" % (pl, pl / argv.validations * 100))
        print("Loss: %d (%.1f%%)" % (op, op / argv.validations * 100))
        print("Draw: %d (%.1f%%)" % (draw, draw / argv.validations * 100))
    else:
        raise RuntimeError(argv[1] + ' is not valid script')
Ejemplo n.º 5
0
 def performMove(self, move: Move):
     self._onMove = getOtherPlayer(move[0])
     if isinstance(self._board[0][0], list):
         self._board[move[1]][move[2]][move[3]] = move[0]
     else:
         self._board[move[1]][move[2]] = move[0]