def simulate_match(player_1, player_2, strategy, rounds=10): """Simulates a number of matches.""" for match in range(rounds): play_1, play_2 = strategy, player_2.strategy(player_1) # Update histories and counts update_history(player_1, play_1) update_history(player_2, play_2)
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)
def simulate_play(player1, player2, action1=None, action2=None): """ 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)
def simulate_play(P1, P2, h1=None, h2=None): """ Simulates play with or without forced history. If h1 and h2 are given, these moves are enforced in the players strategy. This generally should not be necessary, but various tests may force impossible or unlikely histories. """ if h1 and h2: # Simulate Plays s1 = P1.strategy(MockPlayer(P2, h2)) s2 = P2.strategy(MockPlayer(P1, h1)) # Record intended history # Update Cooperation / Defection counts update_history(P1, h1) update_history(P2, h2) get_state_distribution_from_history(P1, h1, h2) get_state_distribution_from_history(P2, h2, h1) return (h1, h2) else: s1 = P1.strategy(P2) s2 = P2.strategy(P1) # Record history update_history(P1, s1) update_history(P2, s2) update_state_distribution(P1, s1, s2) update_state_distribution(P2, s2, s1) return (s1, s2)
def __init__(self, actions=None, history=None, state_dist=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 = list(actions) else: self.actions = []
def test_various(self): p1 = TestOpponent() p2 = TestOpponent() update_history(p1, C) update_history(p2, C) self.assertEqual(p1.history, [C]) self.assertEqual(p2.history, [C]) self.assertEqual(p1.cooperations, 1) self.assertEqual(p2.cooperations, 1) self.assertEqual(p1.defections, 0) self.assertEqual(p2.defections, 0) update_history(p1, D) update_history(p2, D) self.assertEqual(p1.history, [C, D]) self.assertEqual(p2.history, [C, D]) self.assertEqual(p1.cooperations, 1) self.assertEqual(p2.cooperations, 1) self.assertEqual(p1.defections, 1) self.assertEqual(p2.defections, 1)
def simulate_play(P1, P2, h1=None, h2=None): """ Simulates play with or without forced history. If h1 and h2 are given, these moves are enforced in the players strategy. This generally should not be necessary, but various tests may force impossible or unlikely histories. """ if h1 and h2: # Simulate Plays s1 = P1.strategy(MockPlayer(P2, h2)) s2 = P2.strategy(MockPlayer(P1, h1)) # Record intended history # Update Cooperation / Defection counts update_history(P1, h1) update_history(P2, h2) return (h1, h2) else: s1 = P1.strategy(P2) s2 = P2.strategy(P1) # Record history update_history(P1, s1) update_history(P2, s2) return (s1, s2)