Пример #1
0
def run_tests(n=1, gamma=0.9):
    import time

    total_score = 0
    total_time = 0

    np.random.seed()

    # print("Testing basic rules.")
    # print()

    game = DiceGame()

    start_time = time.process_time()
    test_agent = MyAgent(game, gamma)
    total_time += time.process_time() - start_time

    total_actions = 0
    for i in range(n):
        start_time = time.process_time()
        score, actions = play_game_with_agent(test_agent, game)
        total_time += time.process_time() - start_time
        total_actions += actions
        # print(f"Game {i} score: {score}")
        total_score += score

    print(f"Average amount of actions:{total_actions / n}")
    print(f"Average score: {total_score / n}")
    print(f"Total time: {total_time:.4f} seconds")
    return total_score / n
Пример #2
0
def run_extended_tests():
    total_score = 0
    total_time = 0
    n = 10

    print("Testing extended rules – two three-sided dice.")
    print()

    game = DiceGame(dice=2, sides=3)

    start_time = time.process_time()
    test_agent = MyAgent(game)
    total_time += time.process_time() - start_time

    for i in range(n):
        start_time = time.process_time()
        score = play_game_with_agent(test_agent, game)
        total_time += time.process_time() - start_time

        print(f"Game {i} score: {score}")
        total_score += score

    print()
    print(f"Average score: {total_score / n}")
    print(f"Average time: {total_time / n:.5f} seconds")
Пример #3
0
def comparisons(iterations=1):
    agent_names = [
        "AlwaysHoldAgent", "PerfectionistAgent", "OneStepLookAheadAgent",
        "RiskyAgent", "CautiousAgent", "MarkovDecisionProcessAgent",
        "MarkovDecisionProcessAgentAdjusted"
    ]
    total_agent_scores = [0 for i in range(len(agent_names))]
    for game_round in range(iterations):
        seed = np.random.randint(1000)
        game = DiceGame()
        agent_scores = [
            play_agent(AlwaysHoldAgent, game, seed),
            play_agent(PerfectionistAgent, game, seed),
            play_agent(OneStepLookAheadAgent, game, seed),
            play_agent(RiskyAgent, game, seed),
            play_agent(CautiousAgent, game, seed),
            play_agent(MarkovDecisionProcessAgent, game, seed),
            play_agent(MarkovDecisionProcessAgentAdjusted, game, seed)
        ]
        total_agent_scores = [
            agent_scores[idx] + total_agent_scores[idx]
            for idx in range(len(agent_scores))
        ]
        for i in range(len(agent_names)):
            print(f"{agent_names[i]}: {agent_scores[i]}")
        winners = [
            idx for idx in range(len(agent_scores))
            if agent_scores[idx] == max(agent_scores)
        ]
        if len(winners) > 1:
            print("Draw between")
            for winner_idx in winners:
                print(f"\t{agent_names[winner_idx]}")
        else:
            winner_idx = agent_scores.index(max(agent_scores))
            print(
                f"Winner is {agent_names[winner_idx]} with a score of {agent_scores[winner_idx]}"
            )
        print("\n-----------------------\n")
        write_results(agent_scores, agent_names, game_round)
    average_agent_scores = [score / iterations for score in total_agent_scores]
    print(average_agent_scores)
    best_agent_indx = average_agent_scores.index(max(average_agent_scores))
    print(
        f"Best agent is {agent_names[best_agent_indx]} with an average score of {max(average_agent_scores)}"
    )
Пример #4
0
def game_test(gamma=0.95, theta=0.001):

    total_score = 0
    total_time = 0
    n = 10

    np.random.seed(5)
    game = DiceGame()

    start_time = time.process_time()
    test_agent = MarkovDecisionProcessAgent(game,
                                            gamma=gamma,
                                            theta=theta)
    total_time += time.process_time() - start_time

    for i in range(n):
        start_time = time.process_time()
        score = play_game_with_agent(test_agent, game)
        total_time += time.process_time() - start_time
        total_score += score

    avg_score = total_score/n
    avg_time = total_time/n
    return avg_score, avg_time
Пример #5
0
            the input state will be one of self.game.states
            you must return one of self.game.actions

        read the code in dicegame.py to learn more
        """
        # YOUR CODE HERE

        return (0, 1, 2)


if __name__ == "__main__":
    # random seed makes the results deterministic
    # change the number to see different results
    # or delete the line to make it change each time it is run
    np.random.seed(1)

    game = DiceGame()

    agent1 = AlwaysHoldAgent(game)
    play_game_with_agent(agent1, game, verbose=True)

    print("\n")

    agent2 = PerfectionistAgent(game)
    play_game_with_agent(agent2, game, verbose=True)

    print("\n")

    myagent = MyAgent(game)
    play_game_with_agent(myagent, game, verbose=True)
Пример #6
0
from dice_game import DiceGame

import time

if __name__ == "__main__":
    # play es la instanciacion de la clase DiceGame
    play = DiceGame()

    # step 1 in main program area - start game
    number_dice = int(input('Enter number of dice rolls:'))
    ready = input('Ready to start? Hit any key to continue')

    # step 2 in main program are - roll dice
    # User turn to roll
    user_rolls = play.roll_dice(number_dice)
    print('User first roll:', user_rolls)

    # step 3 in main program
    time.sleep(3)
    computer_rolls = play.roll_dice(number_dice)
    print('Computer fisrt roll:', computer_rolls)

    # step 4 in main program
    time.sleep(2)
    print('FINAL STEP TO SHOW THE WINNER...')
    play.find_winner_versus_computer(udice=user_rolls, cdice=computer_rolls)

 def setUp(self):
     np.random.seed(1)
     self.game = DiceGame()
     self.agent = OneStepLookAheadAgent(self.game)
     self.agent._current_state = (1, 1, 2)
     self.state = self.game.reset()
 def setUp(self):
     np.random.seed(1)
     self.game = DiceGame()
     self.agent = MarkovDecisionProcessAgentAdjusted(self.game,
                                                     run_iterations=False)
     self.state = self.game.reset()