コード例 #1
0
ファイル: main.py プロジェクト: AlexZzander/Dice-game
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")
コード例 #2
0
ファイル: main.py プロジェクト: AlexZzander/Dice-game
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
コード例 #3
0
ファイル: play.py プロジェクト: mjpcollins/dice-game
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
class TestOneStepLookAheadAgent(TestCase):
    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 test_calc_probs_hold_0_and_1(self):
        expected_prob = round(2 / 3, 4)
        actual_prob = self.agent._calc_better_state_prob(action=(0, 1))
        self.assertEqual(expected_prob, round(actual_prob, 4))

    def test_calc_probs_hold_all(self):
        actual_prob = self.agent._calc_better_state_prob(action=(0, 1, 2))
        self.assertEqual(0, actual_prob)

    def test_get_options_probs(self):
        expected_option_for_0_1 = round(2 / 3, 4)
        actual_options = self.agent._get_options_probs()
        self.assertEqual(8, len(actual_options))
        self.assertEqual(expected_option_for_0_1,
                         round(actual_options[0]['prob_good_result'], 4))

    def test_get_best_option(self):
        expected_option = (0, 1)
        actual_option = self.agent._get_best_option()
        self.assertEqual(expected_option, actual_option)

    def test_get_best_option_uncertain(self):
        self.agent._current_state = (1, 3, 5)
        expected_option = ()
        actual_option = self.agent._get_best_option()
        self.assertEqual(expected_option, actual_option)

    def test_get_best_option_hold(self):
        self.agent._current_state = (1, 1, 6)
        expected_option = (0, 1, 2)
        actual_option = self.agent._get_best_option()
        self.assertEqual(expected_option, actual_option)
コード例 #5
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
コード例 #6
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)
コード例 #7
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)

コード例 #8
0
 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()
class TestMarkovDecisionProcessAgentAdjusted(TestCase):
    def setUp(self):
        np.random.seed(1)
        self.game = DiceGame()
        self.agent = MarkovDecisionProcessAgentAdjusted(self.game,
                                                        run_iterations=False)
        self.state = self.game.reset()

    def test_init(self):
        self.assertEqual(0.95, self.agent._gamma)
        self.assertEqual(1e-06, self.agent._theta_squared)
        self.assertEqual(
            {
                (1, 1, 1): 0,
                (1, 1, 2): 0,
                (1, 1, 3): 0,
                (1, 1, 4): 0,
                (1, 1, 5): 0,
                (1, 1, 6): 0,
                (1, 2, 2): 0,
                (1, 2, 3): 0,
                (1, 2, 4): 0,
                (1, 2, 5): 0,
                (1, 2, 6): 0,
                (1, 3, 3): 0,
                (1, 3, 4): 0,
                (1, 3, 5): 0,
                (1, 3, 6): 0,
                (1, 4, 4): 0,
                (1, 4, 5): 0,
                (1, 4, 6): 0,
                (1, 5, 5): 0,
                (1, 5, 6): 0,
                (1, 6, 6): 0,
                (2, 2, 2): 0,
                (2, 2, 3): 0,
                (2, 2, 4): 0,
                (2, 2, 5): 0,
                (2, 2, 6): 0,
                (2, 3, 3): 0,
                (2, 3, 4): 0,
                (2, 3, 5): 0,
                (2, 3, 6): 0,
                (2, 4, 4): 0,
                (2, 4, 5): 0,
                (2, 4, 6): 0,
                (2, 5, 5): 0,
                (2, 5, 6): 0,
                (2, 6, 6): 0,
                (3, 3, 3): 0,
                (3, 3, 4): 0,
                (3, 3, 5): 0,
                (3, 3, 6): 0,
                (3, 4, 4): 0,
                (3, 4, 5): 0,
                (3, 4, 6): 0,
                (3, 5, 5): 0,
                (3, 5, 6): 0,
                (3, 6, 6): 0,
                (4, 4, 4): 0,
                (4, 4, 5): 0,
                (4, 4, 6): 0,
                (4, 5, 5): 0,
                (4, 5, 6): 0,
                (4, 6, 6): 0,
                (5, 5, 5): 0,
                (5, 5, 6): 0,
                (5, 6, 6): 0,
                (6, 6, 6): 0
            }, self.agent._state_action_value)
        self.assertEqual([], self.agent._deltas_squared)
        self.assertEqual(
            {
                (1, 1, 1): (),
                (1, 1, 2): (),
                (1, 1, 3): (),
                (1, 1, 4): (),
                (1, 1, 5): (),
                (1, 1, 6): (),
                (1, 2, 2): (),
                (1, 2, 3): (),
                (1, 2, 4): (),
                (1, 2, 5): (),
                (1, 2, 6): (),
                (1, 3, 3): (),
                (1, 3, 4): (),
                (1, 3, 5): (),
                (1, 3, 6): (),
                (1, 4, 4): (),
                (1, 4, 5): (),
                (1, 4, 6): (),
                (1, 5, 5): (),
                (1, 5, 6): (),
                (1, 6, 6): (),
                (2, 2, 2): (),
                (2, 2, 3): (),
                (2, 2, 4): (),
                (2, 2, 5): (),
                (2, 2, 6): (),
                (2, 3, 3): (),
                (2, 3, 4): (),
                (2, 3, 5): (),
                (2, 3, 6): (),
                (2, 4, 4): (),
                (2, 4, 5): (),
                (2, 4, 6): (),
                (2, 5, 5): (),
                (2, 5, 6): (),
                (2, 6, 6): (),
                (3, 3, 3): (),
                (3, 3, 4): (),
                (3, 3, 5): (),
                (3, 3, 6): (),
                (3, 4, 4): (),
                (3, 4, 5): (),
                (3, 4, 6): (),
                (3, 5, 5): (),
                (3, 5, 6): (),
                (3, 6, 6): (),
                (4, 4, 4): (),
                (4, 4, 5): (),
                (4, 4, 6): (),
                (4, 5, 5): (),
                (4, 5, 6): (),
                (4, 6, 6): (),
                (5, 5, 5): (),
                (5, 5, 6): (),
                (5, 6, 6): (),
                (6, 6, 6): ()
            }, self.agent._state_best_action)

    def test_calculate_action_value_hold_all(self):
        self.assertEqual(
            14,
            self.agent._calculate_action_value(action=(0, 1, 2),
                                               state=(1, 1, 2)))

    def test_calculate_action_value_hold_0_and_1(self):
        actual_value = round(
            self.agent._calculate_action_value(action=(0, 1), state=(1, 1, 2)),
            4)
        self.assertEqual(-1, actual_value)

    def test_update_state_best_action(self):
        self.agent._gamma = 0.9
        self.agent._update_state_best_action(state=(1, 1, 2))
        self.assertEqual((0, 1, 2), self.agent._state_best_action[(1, 1, 2)])
        self.assertEqual(14, self.agent._state_action_value[(1, 1, 2)])

    def test_iterate_all_states_once(self):
        self.agent._iterate_all_states()
        self.assertEqual(324, max(self.agent._deltas_squared))
        self.assertEqual(1, self.agent._iterations)

    def test_iterate_all_states_5_times(self):
        for _ in range(5):
            self.agent._iterate_all_states()
        self.assertEqual(0.013878447483153854, max(self.agent._deltas_squared))
        self.assertEqual(5, self.agent._iterations)

    def test_iterate_until_minimal_change_01(self):
        self.agent._theta_squared = 0.1**2
        self.agent._iterate_until_minimal_delta()
        self.assertEqual(0.002008600926252003, max(self.agent._deltas_squared))
        self.assertEqual(6, self.agent._iterations)

    def test_iterate_until_minimal_change_0001(self):
        self.agent._theta_squared = 0.001**2
        self.agent._iterate_until_minimal_delta()
        self.assertEqual(1.6804616828459174e-07,
                         max(self.agent._deltas_squared))
        self.assertEqual(11, self.agent._iterations)

    def test_init_with_iterations(self):
        agent = MarkovDecisionProcessAgentAdjusted(self.game)
        self.assertEqual(1.6804616828459174e-07, max(agent._deltas_squared))
        self.assertEqual(11, agent._iterations)

    def test_play(self):
        agent = MarkovDecisionProcessAgentAdjusted(self.game)
        self.assertEqual((0, 1), agent.play((1, 1, 2)))
        self.assertEqual((0, ), agent.play((1, 3, 5)))
        self.assertEqual((0, 1, 2), agent.play((4, 4, 6)))
        self.assertEqual((0, 1, 2), agent.play((2, 2, 4)))
コード例 #11
0
from dice_game import DiceGame
from time import sleep as duerme_tantito

if __name__ == "__main__":
    # FIND WINNER BETTWEEEN PLAYERS.
    play = DiceGame()
    number_dice = int(input('Enter number of dice rolls: '))
    ready = input('Ready to start? Hit any key to continue')

    # DECLARE PLAYERS
    player_1 = play.roll_dice(number_dice)
    print('Player_1 first roll:', player_1)
    duerme_tantito(1)
    player_2 = play.roll_dice(number_dice)
    print('Player_2 first roll:', player_2)
    duerme_tantito(1)
    player_3 = play.roll_dice(number_dice)
    print('Player_3 first roll:', player_3)

    # FINAL STEP
    duerme_tantito(3)
    print('FINAL STEP TO SHOW THE WINNER...')
    play.find_winner_beetween_us(chucho_dice=player_1,
                                 edgar_dice=player_2,
                                 hector_dice=player_3)