Exemplo n.º 1
0
 def fillWithRandomIndividuals(self, n):
     print("Emptying the individuals set")
     self.individuals = []
     print("Generating random Ais")
     for k in range(n):
         D = [random.random() for i in range(1000)]
         self.individuals.append(ai(D))
     print("GeneratedAll")
Exemplo n.º 2
0
def benchmark(game, episode_num, grid_size):
    winTotal = 0.0
    drawTotal = 0.0
    for i in range(episode_num):
        if (i % 100 == 0): print('hundered episodes completed:', i)

        game = dotsnboxes(grid_size)
        firstTurn = random.randint(0, 1)
        # print(firstTurn)
        if firstTurn == 1:
            aiCharacter = 2
            userLetter = 1
        else:
            aiCharacter = 1
            userLetter = 2

        aiAgent = ai(game, grid_size, 1, 0.1, 0.95, 1.0, 0.01, 100)
        aiAgent.loadFromCsv()
        turn = firstTurn
        gameOn = 1
        while gameOn == 1:
            ## 1 means AI turn
            if game.player == aiCharacter:
                aiAgent.getMoveVsHuman()
                if game.isBoardFull():
                    # game.drawBoard()
                    gameOn = 0

            # user turn
            else:
                # game.drawBoard()
                # print(game.availMove())
                # print('Its player ' + str(game.player) + ' turn')
                i, j = random.choice(
                    game.availMove()
                )  #raw_input('Enter the moves from the above listed moves:  ').split()
                # print('User entered move')
                game.makeMove([], int(i), int(j))
                if game.isBoardFull():
                    # game.drawBoard()
                    gameOn = 0

        # print('winner is ' + str(game.winner()))
        if game.winner() == aiCharacter:
            winTotal = winTotal + 1
        elif game.winner() == 0:
            drawTotal = drawTotal + 1
        else:
            continue
    print('Total Episode :', episode_num)
    print('Win percentage is: ', winTotal / episode_num)
    print('Draw percentage is: ', drawTotal / episode_num)
Exemplo n.º 3
0
 def mutate(self, n):
     """mutate indivudual n, with a probability of 1/5
     'x-man' appears once every 500 times"""
     if random.randint(1, 250) == 1:
         D = [random.random() for i in range(1000)]
         self.individuals[n] = ai(D)
     else:
         t = self.individuals[n].decisions
         for k in range(len(t)):
             if random.randint(1, 3) == 1:
                 i = t[k]
                 m = min(i, 1 - i)
                 mutation = random.choice([-1, 1]) * m * random.random() / 3
                 t[k] = i + mutation
Exemplo n.º 4
0
def play(grid_size):
    game = dotsnboxes(grid_size)
    firstTurn = random.randint(0, 1)
    print(firstTurn)
    if firstTurn == 1:
        aiCharacter = 2
        userLetter = 1
    else:
        aiCharacter = 1
        userLetter = 2

    aiAgent = ai(game, grid_size, 1, 0.1, 0.95, 1.0, 0.01, 100)
    aiAgent.loadFromCsv()
    turn = firstTurn
    gameOn = 1
    while gameOn == 1:
        ## 1 means AI turn
        if game.player == aiCharacter:
            aiAgent.getMoveVsHuman()
            if game.isBoardFull():
                game.drawBoard()
                gameOn = 0

        # user turn
        else:
            game.drawBoard()
            print(game.availMove())
            print('Its player ' + str(game.player) + ' turn')
            i, j = raw_input(
                'Enter the moves from the above listed moves:  ').split()
            # print('User entered move')
            game.makeMove([], int(i), int(j))
            if game.isBoardFull():
                game.drawBoard()
                gameOn = 0

    print('winner is ' + str(game.winner()))
Exemplo n.º 5
0
def trainAI(grid_size, episode_num):
    # ticGame = tictactoe('X', ' '*9)
    aiAgent = ai(dotsnboxes, grid_size, 1, 0.1, 0.95, 1.0, 0.01, episode_num)
    aiAgent.trainFromEpisode()
    aiAgent.write2csv()
Exemplo n.º 6
0
 def reproduct(self, first, second):
     D = [0 for j in range(1000)]
     for j in range(1000):
         D[j] = random.choice([first.decisions[j], second.decisions[j]])
     return ai(D)