Esempio n. 1
0
def limited_simulate_play(player_1, player_2, h1):
    """Here we want to replay player_1's history to player_2, allowing
    player_2's strategy method to set any internal variables as needed. If you
    need a more complete simulation, see `simulate_play` in player.py. This
    function is specifically designed for the needs of MindReader."""
    h2 = player_2.strategy(player_1)
    update_history(player_1, h1)
    update_history(player_2, h2)
Esempio n. 2
0
 def test_update_history(self):
     player = Player()
     self.assertEqual(player.history, [])
     self.assertEqual(player.cooperations, 0)
     self.assertEqual(player.defections, 0)
     update_history(player, D)
     self.assertEqual(player.history, [D])
     self.assertEqual(player.defections, 1)
     self.assertEqual(player.cooperations, 0)
     update_history(player, C)
     self.assertEqual(player.history, [D, C])
     self.assertEqual(player.defections, 1)
     self.assertEqual(player.cooperations, 1)
Esempio n. 3
0
 def test_update_history(self):
     player = Player()
     self.assertEqual(player.history, [])
     self.assertEqual(player.cooperations, 0)
     self.assertEqual(player.defections, 0)
     update_history(player, D)
     self.assertEqual(player.history, [D])
     self.assertEqual(player.defections, 1)
     self.assertEqual(player.cooperations, 0)
     update_history(player, C)
     self.assertEqual(player.history, [D, C])
     self.assertEqual(player.defections, 1)
     self.assertEqual(player.cooperations, 1)
Esempio n. 4
0
def simulate_play(player1: Player, player2: Player, action1: Action =None, action2: Action =None) -> Tuple[Action, Action]:
    """
    Simulates play with or without forced history. If action1 and action2 are given, these
    actions are enforced in the players strategy. This generally should not be
    necessary, but various tests may force impossible or unlikely histories.
    """

    if action1 and action2:
        mock_player1 = MockPlayer(actions=[action1], history=player1.history)
        mock_player2 = MockPlayer(actions=[action2], history=player2.history)
        # Force plays
        s1 = player1.strategy(mock_player2)
        s2 = player2.strategy(mock_player1)
        if (s1 != action1) or (s2 != action2):
            warnings.warn(
            "Simulated play mismatch with expected history: Round was "
            "({}, {}) but ({}, {}) was expected for player: {}".format(
                s1, s2, action1, action2, str(player1))
            )
        # Record intended history
        # Update Cooperation / Defection counts
        update_history(player1, action1)
        update_history(player2, action2)
        update_state_distribution(player1, action1, action2)
        update_state_distribution(player2, action2, action1)
        return (s1, s2)
    else:
        s1 = player1.strategy(player2)
        s2 = player2.strategy(player1)
        # Record history
        update_history(player1, s1)
        update_history(player2, s2)
        update_state_distribution(player1, s1, s2)
        update_state_distribution(player2, s2, s1)
        return (s1, s2)
Esempio n. 5
0
 def __init__(self, actions: List[Action] =None, history: List[Action] =None, state_dist: defaultdict =None) -> None:
     # Need to retain history for opponents that examine opponents history
     # Do a deep copy just to be safe
     super().__init__()
     if history:
         # Make sure we both copy the history and get the right counts
         # for cooperations and defections.
         for action in history:
             update_history(self, action)
     if state_dist:
         self.state_distribution = dict(state_dist)
     if actions:
         self.actions = cycle(actions)
     else:
         self.actions = iter([])
Esempio n. 6
0
def _limited_simulate_play(player_1, player_2, h1):
    """Simulates a player's move.

    After inspecting player_2's next move (allowing player_2's strategy
    method to set any internal variables as needed), update histories
    for both players. Note that player_1's move is an argument.

    If you need a more complete simulation, see `simulate_play` in
    player.py. This function is specifically designed for the needs
    of MindReader.

    Parameters
    ----------
    player_1: Player
        The player whose move is already known.
    player_2: Player
        The player the we want to inspect.
    h1: Action
        The next action for first player.
    """
    h2 = inspect_strategy(player_1, player_2)
    update_history(player_1, h1)
    update_history(player_2, h2)
Esempio n. 7
0
 def update_history(self, history_list):
     for move in history_list:
         update_history(self.player, move)
Esempio n. 8
0
 def update_history(self, history_list):
     for move in history_list:
         update_history(self.player, move)