def test_strategy(self): for action in [C, D]: m = MockPlayer(actions=[action]) p2 = axelrod.Player() self.assertEqual(action, m.strategy(p2)) actions = [C, C, D, D, C, C] m = MockPlayer(actions=actions) p2 = axelrod.Player() for action in actions: self.assertEqual(action, m.strategy(p2))
def test_cloning(self): p1 = axelrod.Cooperator() p2 = axelrod.Defector() moves = 10 for i in range(moves): p1.play(p2) m1 = MockPlayer(p1, C) m2 = MockPlayer(p2, D) self.assertEqual(m1.move, C) self.assertEqual(m1.history, p1.history) self.assertEqual(m1.cooperations, p1.cooperations) self.assertEqual(m1.defections, p1.defections) self.assertEqual(m2.move, D) self.assertEqual(m2.history, p2.history) self.assertEqual(m2.cooperations, p2.cooperations) self.assertEqual(m2.defections, p2.defections)
def test_strategy(self): if soft: self.first_play_test(C) else: self.first_play_test(D) # for example memory_depth=2 plays against [C, C, D, D] # soft actions = [(C, C), (C, C), (C, D), (C, D)] # hard actions = [(D, C), (C, C), (C, D), (D, D)] opponent_actions = [C] * memory_depth + [D] * memory_depth opponent = MockPlayer(actions=opponent_actions) if soft: first_player_action = [C] else: first_player_action = [D] if memory_depth % 2 == 1 or soft: cooperations = int(memory_depth * 1.5) else: cooperations = int(memory_depth * 1.5) - 1 defections = len(opponent_actions) - cooperations - 1 player_actions = (first_player_action + [C] * cooperations + [D] * defections) actions = list(zip(player_actions, opponent_actions)) self.versus_test(opponent, expected_actions=actions)
def test_init(self): actions = [C, C, D] history = [C, C, C] state_dist = {(C, C): 2, (D, C): 1} m = MockPlayer(actions, history, state_dist) self.assertEqual(m.history, [C, C, C]) self.assertEqual(m.state_distribution, state_dist)
def test_memory_depth_even_soft_is_true(self): memory_depth = 4 init_kwargs = {"memory_depth": memory_depth} opponent = MockPlayer([C] * memory_depth + [D] * memory_depth) actions = [(C, C)] * 4 + [(C, D)] * 3 + [(D, D)] + [(D, C)] * 2 + [(C, C)] * 2 self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs)
def test_memory_depth_infinite_soft_is_true(self): opponent_actions = [C] * 50 + [D] * 100 + [C] * 52 actions = ( [(C, C)] * 50 + [(C, D)] * 51 + [(D, D)] * 49 + [(D, C)] * 50 + [(C, C)] * 2 ) opponent = MockPlayer(actions=opponent_actions) self.versus_test(opponent, expected_actions=actions)
def test_memory_depth_even_soft_is_false(self): memory_depth = 4 init_kwargs = {'memory_depth': memory_depth} if self.default_soft: init_kwargs['soft'] = False opponent = MockPlayer(actions=[C] * memory_depth + [D] * memory_depth) actions = ([(D, C)] + [(C, C)] * 3 + [(C, D)] * 2 + [(D, D)] * 2 + [(D, C)] * 3 + [(C, C)]) self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs)
def test_memory_depth_infinite_soft_is_false(self): init_kwargs = {} if self.default_soft: init_kwargs['soft'] = False opponent_actions = [C] * 50 + [D] * 100 + [C] * 52 actions = ([(D, C)] + [(C, C)] * 49 + [(C, D)] * 50 + [(D, D)] * 50 + [(D, C)] * 51 + [(C, C)]) opponent = MockPlayer(actions=opponent_actions) self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs)
def test_memory_depth_odd(self): memory_depth = 5 init_kwargs = {'memory_depth': memory_depth} if self.default_soft: first_action = [(C, C)] else: first_action = [(D, C)] opponent = MockPlayer(actions=[C] * memory_depth + [D] * memory_depth) actions = (first_action + [(C, C)] * 4 + [(C, D)] * 3 + [(D, D)] * 2 + [(D, C)] * 3 + [(C, C)] * 2) self.versus_test(opponent, expected_actions=actions, init_kwargs=init_kwargs)
def responses_test(self, responses, player_history=None, opponent_history=None, seed=None, length=200, attrs=None, init_args=None, init_kwargs=None): """Test responses to arbitrary histories. A match is played where the histories are enforced and the sequence of plays in responses is checked to be the outcome. Internal variables can be checked with the attrs attribute and arguments to the first player can be passed in init_args. Parameters ---------- responses: History or sequence of axelrod.Actions The expected outcomes player_history, opponent_history: sequences of prior history to enforce seed: int A random seed if needed for reproducibility length: int Some players require the length of the match attrs: dict dictionary of internal attributes to check at the end of all plays in player init_args: tuple or list A list of arguments to instantiate player with init_kwargs: dictionary A list of keyword arguments to instantiate player with """ if init_args is None: init_args = () if init_kwargs is None: init_kwargs = dict() player1 = self.player(*init_args, **init_kwargs) player1.set_match_attributes(length=length) player2 = MockPlayer() player2.set_match_attributes(length=length) test_responses(self, player1, player2, responses, player_history, opponent_history, seed=seed, attrs=attrs) # Test that we get the same sequence after a reset player1.reset() player2.reset() test_responses(self, player1, player2, responses, player_history, opponent_history, seed=seed, attrs=attrs) # Test that we get the same sequence after a clone player1 = player1.clone() player2 = player2.clone() test_responses(self, player1, player2, responses, player_history, opponent_history, seed=seed, attrs=attrs)
def test_strategy(self): for action in [C, D]: m = MockPlayer(actions=[action]) p2 = Player() self.assertEqual(action, m.strategy(p2)) actions = [C, C, D, D, C, C] m = MockPlayer(actions=actions) p2 = Player() for action in actions: self.assertEqual(action, m.strategy(p2))
def test_history(self): t = TestOpponent() m1 = MockPlayer([C], history=[C] * 10) self.assertEqual(m1.actions[0], C) self.assertEqual(m1.history, [C] * 10) self.assertEqual(m1.cooperations, 10) self.assertEqual(m1.defections, 0) self.assertEqual(m1.strategy(t), C) m2 = MockPlayer([D], history=[D] * 10) self.assertEqual(m2.actions[0], D) self.assertEqual(m2.history, [D] * 10) self.assertEqual(m2.cooperations, 0) self.assertEqual(m2.defections, 10) self.assertEqual(m2.strategy(t), D)
def test_strategy(self): for move in [C, D]: m = MockPlayer(axelrod.Player(), move) p2 = axelrod.Player() self.assertEqual(move, m.strategy(p2))