コード例 #1
0
ファイル: MCTS_sweepET.py プロジェクト: Easyty123/SEEX30
def main():
    
    # Parameters
    nGames = 1000
    depth = 5
    epsilon = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
    search = 200
    temperature = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
    
    # Initialize
    infoFile = open('MCTS_sweepET_info.txt', 'w')
    infoFile.write(
        'Games = ' + str(nGames) + '\n'
        + 'Depth = ' + str(depth) + '\n'
        + 'Epsilon = ' + str(epsilon) + '\n'
        + 'Search = ' + str(search) + '\n'
        + 'Temperature = ' + str(temperature) + '\n')
    infoFile.close()
    print('Start MCTS epsilon and temperature sweep!')
    print('[Total games: %d]' % (nGames * len(epsilon) * len(temperature)))
    print(
        '[Depth: %d, Search: %d]'
        % (depth, search))
    HCAI = []
    for i in range(1):
        HCAI.append(c.actions[1])
    for i in range(35):
        HCAI.append(c.actions[0])
    bResultsFile = open('MCTS_sweepET_b.txt', 'w')
    wResultsFile = open('MCTS_sweepET_w.txt', 'w')
    
    # Run the parameter sweep
    for iEpsilon in range(len(epsilon)):
        for iTemperature in range(len(temperature)):
            print('Completed %1.1f' % (100 * (iEpsilon * len(epsilon) + iTemperature) / (len(epsilon) * len(temperature))) + '%')
            bMCTSAI = MonteCarloTreeSearchAI.AI(c.team2, c.team3, depth, epsilon[iEpsilon], search, temperature[iTemperature])
            bMCTSvHC = 0
            wMCTSAI = MonteCarloTreeSearchAI.AI(c.team3, c.team2, depth, epsilon[iEpsilon], search, temperature[iTemperature])
            wMCTSvHC = 0
            for iGame in range(nGames):
                game = Game.Game(c.team2, c.team3)
                while game.running:
                    game.trainers[0].setNextAction(bMCTSAI.getAction(game.getState(c.amBlack)))
                    game.trainers[1].setNextAction(HCAI[game.round])
                    game.progress()
                if game.win[0]:
                    bMCTSvHC += 1
                game = Game.Game(c.team2, c.team3)
                while game.running:
                    game.trainers[0].setNextAction(HCAI[game.round])
                    game.trainers[1].setNextAction(wMCTSAI.getAction(game.getState(c.amWhite)))
                    game.progress()
                if game.win[1]:
                    wMCTSvHC += 1
            bResultsFile.write(str(bMCTSvHC / nGames) + '\n')
            wResultsFile.write(str(wMCTSvHC / nGames) + '\n')
    print('Completed 100%')
    bResultsFile.close()
    wResultsFile.close()
コード例 #2
0
ファイル: MCTS_speed.py プロジェクト: Easyty123/SEEX30
def main():
    
    # Parameters
    nGames = 10
    depth = 1
    epsilon = 0.1
    limit = 100
    search = 250
    temperature = 0.2
    
    # Initialize
    AI = [MonteCarloTreeSearchAI.AI(depth, epsilon, limit, search, temperature), RandomAI.AI()]
    
    # Run speed test
    runTimes = []
    for iGame in range(nGames):
        if iGame % int(nGames / 10) == 0:
            print('Completed ' + str(int(100 * iGame / nGames)) + '%')
        iTime = time.time()
        game = Game.Game()
        while game.running and game.round < limit:
            states = [game.getState(True), game.getState(False)]
            if game.forceSwitch[0] == game.forceSwitch[1]:
                game.nextAction[0] = AI[0].getAction(states[0])
                game.nextAction[1] = AI[1].getAction(states[1])
            else:
                for iT in range(2):
                    if game.forceSwitch[iT]:
                        game.nextAction[iT] = AI[iT].getAction(states[iT])
            game.progress()
        runTimes.append(time.time() - iTime)
    tSum = sum(runTimes)
    mean = tSum / nGames
    distance = []
    for i in range(nGames):
        distance.append((runTimes[i] - mean) ** 2)
    standardDeviation = math.sqrt(sum(distance) / nGames)
    print(
        'Completed 100%' + '\n'
        + 'Sum: ' + FormatTime.getString(tSum) + '\n'
        + 'Mean: ' + FormatTime.getString(mean) + '\n'
        + 'Standard deviation: ' + FormatTime.getString(standardDeviation))
コード例 #3
0
def main():
    
    # Parameters
    nGames = 1000
    depth = 5
    epsilon = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
    search = 200
    temperature = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
    
    # Initialize
    HCAI = []
    HC_string = ''
    game = Game.Game()
    for i in range(game.n - 1):
        HCAI.append(0)
        HC_string += '0'
    for i in range(game.n):
        HCAI.append(1)
        HC_string += '1'
    infoFile = open('MCTS_sweepET_info.txt', 'w')
    infoFile.write(
        'Games = ' + str(nGames) + '\n'
        + 'Depth = ' + str(depth) + '\n'
        + 'Epsilon = ' + str(epsilon) + '\n'
        + 'Search = ' + str(search) + '\n'
        + 'Temperature = ' + str(temperature) + '\n')
    infoFile.close()
    print('Start MCTS epsilon and temperature sweep!')
    print('[Total games: %d]' % (nGames * len(epsilon) * len(temperature)))
    print(
        '[Depth: %d, Search: %d]'
        % (depth, search))
    resultsFile = open('MCTS_sweepET.txt', 'w')
    
    # Run the parameter sweep
    for iEpsilon in range(len(epsilon)):
        for iTemperature in range(len(temperature)):
            print(
                'Completed %1.1f'
                % (100 * (iEpsilon * len(epsilon) + iTemperature)
                    / (len(epsilon) * len(temperature)))
                + '%')
            strategies = {}
            for iGame in range(nGames):
                MCTSAI = MonteCarloTreeSearchAI.AI(
                    depth,
                    epsilon[iEpsilon],
                    search,
                    temperature[iTemperature])
                game = Game.Game()
                MCTS_string = ''
                while game.running:
                    MCTS_action = MCTSAI.getAction(game.getState(True))
                    MCTS_string += str(MCTS_action)
                    game.progress([MCTS_action, HCAI[game.round]])
                if MCTS_string in strategies:
                    strategies[MCTS_string] += 1
                else:
                    strategies[MCTS_string] = 1
            if HC_string in strategies:
                resultsFile.write(str(strategies[HC_string] / nGames) + '\n')
            else:
                resultsFile.write(str(0.0) + '\n')
    print('Completed 100%')
    resultsFile.close()
コード例 #4
0
def main():
    
    # Parameters
    nGames = 1000
    depth = 25
    epsilon = 0.2
    search = 1000
    temperature = 0.2
    
    # Initialize
    MCTSAI = MonteCarloTreeSearchAI.AI(depth, epsilon, search, temperature)
    HCAI = []
    HC_string = ''
    game = Game.Game()
    for i in range(game.n - 1):
        HCAI.append(0)
        HC_string += '0'
    for i in range(game.n):
        HCAI.append(1)
        HC_string += '1'
    print('Start MCTS benchmark!')
    print('[Total games: %d]' % (nGames))
    print(
        '[Depth: %d, Epsilon: %1.1f, Search: %d, Temperature: %1.1f]'
        % (depth, epsilon, search, temperature))
    strategies = {}
    MCTSvHC = 0
    
    # Run the benchmark
    for iGame in range(nGames):
        if iGame % int(nGames / 10) == 0:
            print('Completed ' + str(int(100 * iGame / nGames)) + '%')
        game = Game.Game()
        MCTS_string = ''
        while game.running:
            MCTS_action = MCTSAI.getAction(game.getState(True))
            MCTS_string += str(MCTS_action)
            game.progress([MCTS_action, HCAI[game.round]])
        if game.win[0]:
            MCTSvHC += 1
        if MCTS_string in strategies:
            strategies[MCTS_string] += 1
        else:
            strategies[MCTS_string] = 1
    print('Completed 100%')
    
    # Write the results
    resultsFile = open('MCTS_benchmark_results.txt', 'w')
    resultsFile.write('## Monte Carlo Tree Search\n')
    resultsFile.write(
        'Games: ' + str(nGames) + '\n'
        + 'Depth: ' + str(depth) + '\n'
        + 'Epsilon: ' + str(epsilon) + '\n'
        + 'Search: ' + str(search) + '\n'
        + 'Temperature: ' + str(temperature) + '\n\n')
    resultsFile.write('# Optimal strategy usage: \n')
    if HC_string in strategies:
        resultsFile.write(str(100 * strategies[HC_string] / nGames) + '%\n\n')
    else:
        resultsFile.write('Did not use the optimal strategy at all')
    resultsFile.write('# Win percentage: \n')
    resultsFile.write(str(100 * MCTSvHC / nGames) + '%\n\n')
    resultsFile.close()
コード例 #5
0
def main():
    
    # Parameters
    nGames = 1000
    depth = [5, 10, 15, 20, 25]
    epsilon = 0.2
    search = [200, 400, 600, 800, 1000]
    temperature = 0.2
    
    # Initialize
    HCAI = []
    HC_string = ''
    game = Game.Game()
    for i in range(game.n - 1):
        HCAI.append(0)
        HC_string += '0'
    for i in range(game.n):
        HCAI.append(1)
        HC_string += '1'
    infoFile = open('MCTS_sweepDS_info.txt', 'w')
    infoFile.write(
        'Games = ' + str(nGames) + '\n'
        + 'Depth = ' + str(depth) + '\n'
        + 'Epsilon = ' + str(epsilon) + '\n'
        + 'Search = ' + str(search) + '\n'
        + 'Temperature = ' + str(temperature) + '\n')
    infoFile.close()
    print('Start MCTS depth and search sweep!')
    print('[Total games: %d]' % (nGames * len(depth) * len(search)))
    print(
        '[Epsilon: %1.1f, Temperature: %1.1f]'
        % (epsilon, temperature))
    resultsStrategyFile = open('MCTS_sweepDS_strategy.txt', 'w')
    resultsTimeFile = open('MCTS_sweepDS_time.txt', 'w')
    
    # Run the parameter sweep
    for iDepth in range(len(depth)):
        for iSearch in range(len(search)):
            print(
                'Completed %1.1f'
                % (100 * (iDepth * len(depth) + iSearch)
                    / (len(depth) * len(search)))
                + '%')
            strategies = {}
            runTime = time.time()
            for iGame in range(nGames):
                MCTSAI = MonteCarloTreeSearchAI.AI(
                    depth[iDepth],
                    epsilon,
                    search[iSearch],
                    temperature)
                game = Game.Game()
                MCTS_string = ''
                while game.running:
                    MCTS_action = MCTSAI.getAction(game.getState(True))
                    MCTS_string += str(MCTS_action)
                    game.progress([MCTS_action, HCAI[game.round]])
                if MCTS_string in strategies:
                    strategies[MCTS_string] += 1
                else:
                    strategies[MCTS_string] = 1
            if HC_string in strategies:
                resultsStrategyFile.write(str(strategies[HC_string] / nGames) + '\n')
            else:
                resultsStrategyFile.write(str(0.0) + '\n')
            resultsTimeFile.write(str((time.time() - runTime) / nGames) + '\n')
    print('Completed 100%')
    resultsStrategyFile.close()
    resultsTimeFile.close()
コード例 #6
0
def main():

    # Parameters
    nGames = 1000
    depth = [1, 5, 10, 15, 20]
    epsilon = 0.1
    limit = 100
    search = 250
    temperature = 0.2

    # Initialize
    progressExist = os.path.isfile(
        'MCTS_sweepD_results/MCTS_sweepD_progress.txt')
    if progressExist:
        array = Load.loadFloatArray('MCTS_sweepD_results/MCTS_sweepD_progress',
                                    7)
        startDepth = int(array[0])
        startGame = int(array[1])
        abort = array[2]
        loss = array[3]
        runTime = array[4]
        tie = array[5]
        win = array[6]
    else:
        startDepth = 0
        if os.path.isfile('MCTS_sweepD_results/MCTS_sweepD_info.txt'):
            exit(
                'Info file exists but progress file doesn\'t: (Re)Move previous results before running!'
            )
    infoFile = open('MCTS_sweepD_results/MCTS_sweepD_info.txt', 'w')
    infoFile.write('Games = ' + str(nGames) + '\n' + 'Depth = ' + str(depth) +
                   '\n' + 'Epsilon = ' + str(epsilon) + '\n' + 'Limit = ' +
                   str(limit) + '\n' + 'Search = ' + str(search) + '\n' +
                   'Temperature = ' + str(temperature) + '\n')
    infoFile.close()

    # Run the parameter sweep
    for iDepth in range(startDepth, len(depth)):
        print('Completed %1.1f' % (100 * iDepth / len(depth)) + '%')
        if not progressExist:
            startGame = 0
            abort = 0
            loss = 0
            runTime = 0
            tie = 0
            win = 0
        AI = [
            MonteCarloTreeSearchAI.AI(depth[iDepth], epsilon, limit, search,
                                      temperature),
            RandomAI.AI()
        ]
        for iGame in range(startGame, nGames):
            iTime = time.time()
            game = Game.Game()
            while game.running and game.round < limit:
                states = [game.getState(True), game.getState(False)]
                if game.forceSwitch[0] == game.forceSwitch[1]:
                    game.nextAction[0] = AI[0].getAction(states[0])
                    game.nextAction[1] = AI[1].getAction(states[1])
                else:
                    for iT in range(2):
                        if game.forceSwitch[iT]:
                            game.nextAction[iT] = AI[iT].getAction(states[iT])
                game.progress()
            runTime += time.time() - iTime
            if game.win[0] and game.win[1]:
                tie += 1
            elif game.win[0]:
                win += 1
            elif game.win[1]:
                loss += 1
            else:
                abort += 1
            progressFile = open('MCTS_sweepD_results/MCTS_sweepD_progress.txt',
                                'w')
            progressFile.write(
                str(iDepth) + '\n' + str(iGame + 1) + '\n' + str(abort) +
                '\n' + str(loss) + '\n' + str(runTime) + '\n' + str(tie) +
                '\n' + str(win))
            progressFile.close()
        abortFile = open('MCTS_sweepD_results/MCTS_sweepD_abort.txt', 'a')
        abortFile.write(str(abort / nGames) + '\n')
        abortFile.close()
        lossFile = open('MCTS_sweepD_results/MCTS_sweepD_loss.txt', 'a')
        lossFile.write(str(loss / nGames) + '\n')
        lossFile.close()
        tieFile = open('MCTS_sweepD_results/MCTS_sweepD_tie.txt', 'a')
        tieFile.write(str(tie / nGames) + '\n')
        tieFile.close()
        timeFile = open('MCTS_sweepD_results/MCTS_sweepD_time.txt', 'a')
        timeFile.write(str(runTime / nGames) + '\n')
        timeFile.close()
        winFile = open('MCTS_sweepD_results/MCTS_sweepD_win.txt', 'a')
        winFile.write(str(win / nGames) + '\n')
        winFile.close()
        progressFile = open('MCTS_sweepD_results/MCTS_sweepD_progress.txt',
                            'w')
        progressFile.write(
            str(iDepth + 1) + '\n' + str(0) + '\n' + str(0) + '\n' + str(0) +
            '\n' + str(0) + '\n' + str(0) + '\n' + str(0))
        progressFile.close()
        progressExist = False
    print('Completed 100%')
    os.remove('MCTS_sweepD_results/MCTS_sweepD_progress.txt')
コード例 #7
0
ファイル: run.py プロジェクト: Easyty123/SEEX30
def main(id):

    # Parameters
    depth = 1
    epsilon = 0.1
    limit = 100
    search = 10
    temperature = 0.2

    # Initialize
    AI = [
        RandomAI.AI(),
        MonteCarloTreeSearchAI.AI(depth, epsilon, limit, search, temperature)
    ]
    idExists = os.path.isfile('run_results/gamePrint_' + str(id) + '.txt')
    while idExists:
        id += 1
        idExists = os.path.isfile('run_results/gamePrint_' + str(id) + '.txt')
    gamePrintFile = open('run_results/gamePrint_' + str(id) + '.txt', 'w')
    if os.path.isfile('run_results/results.txt'):
        array = Load.loadFloatArray('run_results/results', 5)
        initializedGames = array[0]
        initializedGames += 1
        completedGames = array[1]
        losses = array[2]
        ties = array[3]
        wins = array[4]
    else:
        initializedGames = 1
        completedGames = 0
        losses = 0
        ties = 0
        wins = 0
    resultsFile = open('run_results/results.txt', 'w')
    resultsFile.write(
        str(int(initializedGames)) + '\n' + str(int(completedGames)) + '\n' +
        str(int(losses)) + '\n' + str(int(ties)) + '\n' + str(int(wins)) +
        '\n')
    resultsFile.close()

    # Run the battle
    game = Game.Game()
    while game.running:
        states = [game.getState(True), game.getState(False)]
        if game.forceSwitch[0] == game.forceSwitch[1]:
            thread0 = threading.Thread(target=setAction,
                                       args=(game, 0, AI[0], states[0]))
            thread0.daemon = True
            thread0.start()
            thread1 = threading.Thread(target=setAction,
                                       args=(game, 1, AI[1], states[1]))
            thread1.daemon = True
            AI[1].extendSearch = True
            thread1.start()
            while thread1.isAlive():
                if not thread0.isAlive():
                    AI[1].extendSearch = False
                    if AI[1].searchNotFinished:
                        print('\nPlease wait for the AI to make a decision!')
                    break
            thread0.join()
            thread1.join()
        else:
            for iT in range(2):
                if game.forceSwitch[iT]:
                    game.nextAction[iT] = AI[iT].getAction(states[iT])
        #stdout = sys.stdout
        #sys.stdout = io.StringIO()
        game.progress()
        summary = game.getSummary()
        print(summary[0:(len(summary) - 1)])
        gamePrintFile.write(summary)
        #gamePrint = sys.stdout.getvalue()
        #gamePrintFile.write(gamePrint)
        #sys.stdout = stdout
        #print(gamePrint[0:(len(gamePrint) - 1)])
    gamePrintFile.close()

    # Save the results
    completedGames += 1
    if game.win[0] and game.win[1]:
        ties += 1
        print('Tie after ' + str(game.round) + ' rounds!')
    elif game.win[0]:
        wins += 1
        print('Win after ' + str(game.round) + ' rounds!')
    elif game.win[1]:
        losses += 1
        print('Loss after ' + str(game.round) + ' rounds!')
    resultsFile = open('run_results/results.txt', 'w')
    resultsFile.write(
        str(int(initializedGames)) + '\n' + str(int(completedGames)) + '\n' +
        str(int(losses)) + '\n' + str(int(ties)) + '\n' + str(int(wins)) +
        '\n')
    resultsFile.close()
コード例 #8
0
ファイル: MCTS_benchmark.py プロジェクト: Easyty123/SEEX30
def main():
    
    # Parameters
    nGames = 1000
    depth = 10
    search = 1000
    bEpsilon = 0.2
    bTemperature = 0.2
    wEpsilon = 0.2
    wTemperature = 0.2
    
    # Initialize
    print('Start MCTS benchmark!')
    print('[Total games: %d]' % (2 * nGames))
    print(
        '[Depth: %d, Search: %d, Black epsilon: %1.1f, Black temperature: %1.1f, White epsilon: %1.1f, White temperature: %1.1f]'
        % (depth, search, bEpsilon, bTemperature, wEpsilon, wTemperature))
    HCAI = []
    for i in range(1):
        HCAI.append(c.actions[1])
    for i in range(35):
        HCAI.append(c.actions[0])
    bMCTSAI = MonteCarloTreeSearchAI.AI(c.team2, c.team3, depth, bEpsilon, search, bTemperature)
    bMCTSvHC = 0
    wMCTSAI = MonteCarloTreeSearchAI.AI(c.team3, c.team2, depth, wEpsilon, search, wTemperature)
    wMCTSvHC = 0
    
    # Run the benchmark
    for iGame in range(nGames):
        if iGame % int(nGames / 10) == 0:
            print('Completed ' + str(int(100 * iGame / nGames)) + '%')
        game = Game.Game(c.team2, c.team3)
        while game.running:
            game.trainers[0].setNextAction(bMCTSAI.getAction(game.getState(c.amBlack)))
            game.trainers[1].setNextAction(HCAI[game.round])
            game.progress()
        if game.win[0]:
            bMCTSvHC += 1
        game = Game.Game(c.team2, c.team3)
        while game.running:
            game.trainers[0].setNextAction(HCAI[game.round])
            game.trainers[1].setNextAction(wMCTSAI.getAction(game.getState(c.amWhite)))
            game.progress()
        if game.win[1]:
            wMCTSvHC += 1
    print('Completed 100%')
    
    # Write the results
    resultsFile = open('MCTS_benchmark_results.txt', 'w')
    resultsFile.write('## Monte Carlo Tree Search\n')
    resultsFile.write(
        'Games: ' + str(nGames) + '\n'
        + 'Depth: ' + str(depth) + '\n'
        + 'Search: ' + str(search) + '\n'
        + 'Black epsilon: ' + str(bEpsilon) + '\n'
        + 'Black temperature: ' + str(bTemperature) + '\n'
        + 'White epsilon: ' + str(wEpsilon) + '\n'
        + 'White temperature: ' + str(wTemperature) + '\n\n')
    resultsFile.write('## Black \n\n')
    resultsFile.write('# Win percentage: \n')
    resultsFile.write(str(100 * bMCTSvHC / nGames) + '%\n\n')
    resultsFile.write('## White \n\n')
    resultsFile.write('# Win percentage: \n')
    resultsFile.write(str(100 * wMCTSvHC / nGames) + '%\n\n')
    resultsFile.close()