def test_zero_tables(self): """Test the corner case where n=0.""" anti_tft_pattern = "DC" parameters = Plays(self_plays=0, op_plays=1, op_openings=0) tft_vs_alternator = [(C, C)] + [(D, D), (C, C)] * 5 self.versus_test( axl.Alternator(), expected_actions=tft_vs_alternator, init_kwargs={ "parameters": parameters, "pattern": anti_tft_pattern }, )
def test_strategy(self): # Starts by Cooperating self.first_play_test(C) # Check if the turns played are greater than 5 actions = [(C, C), (C, C), (C, C), (C, C), (C, C), (C, C), (C, C)] self.versus_test(axelrod.Cooperator(), expected_actions=actions) actions = [(C, D), (D, D), (D, D), (D, D), (D, D), (D, D), (D, D)] self.versus_test(axelrod.Defector(), expected_actions=actions) # Check for TFT behavior after 5 rounds actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D), (D, C)] self.versus_test(axelrod.Alternator(), expected_actions=actions)
def main(): player = axl.ZDExtort2() opponent = axl.Alternator() axl.seed(0) match = axl.Match(players=(player, opponent), turns=20) interactions = match.play() df = pd.DataFrame(interactions, columns=["(8/9, 1/2, 1/3, 0)", "Alternator"]) df["Turn"] = df.index + 1 df = df[["Turn", "(8/9, 1/2, 1/3, 0)", "Alternator"]] df = df.transpose() string = df.to_latex(header=False).replace("\\\\", "\\\\\\midrule", 1) with open("main.tex", "w") as f: f.write(string)
def test_strategy(self): # Test cooperate after opponent cooperates actions = [(C, C)] * 5 self.versus_test(axelrod.Cooperator(), expected_actions=actions) # If opponent only defects then probability of cooperating is 0. actions = [(C, D), (D, D), (D, D), (D, D), (D, D)] self.versus_test(axelrod.Defector(), expected_actions=actions) # Stochastic response to defect actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] self.versus_test(axelrod.Alternator(), expected_actions=actions, seed=0) actions = [(C, C), (C, D), (C, C), (C, D), (D, C)] self.versus_test(axelrod.Alternator(), expected_actions=actions, seed=1) opponent = axelrod.MockPlayer(actions=[D, C, C, D]) actions = [(C, D), (D, C), (C, C), (C, D), (C, D)] self.versus_test(opponent, expected_actions=actions, seed=8) opponent = axelrod.MockPlayer(actions=[D, C, C, D]) actions = [(C, D), (D, C), (C, C), (C, D), (D, D)] self.versus_test(opponent, expected_actions=actions, seed=2)
def test_strategy(self): # If opponent defects more than once, cooperate forever. actions = [(D, C)] * 20 self.versus_test(opponent=axelrod.Cooperator(), expected_actions=actions) actions = [(D, C), (D, D)] + [(C, C), (C, D)] * 20 self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions) opponent = axelrod.MockPlayer([D] + [C] * 19) actions = [(D, D)] + [(C, C)] * 19 self.versus_test(opponent=opponent, expected_actions=actions) actions = [(D, D)] + [(C, D)] * 19 self.versus_test(opponent=axelrod.Defector(), expected_actions=actions)
def test_strategy(self): # Starts by cooperating. self.first_play_test(C) player_history = [C, C, D, C, D, C, C, C, D, C, C, C, D, D, D, D, D, D] opp_history = [C, D] * 9 actions = list(zip(player_history, opp_history)) self.versus_test(axelrod.Alternator(), expected_actions=actions) player_history = [C, D, C, D, C, C, C, C, C] opp_history = [D, C, D, C, D, C, C, C, C] actions = list(zip(player_history, opp_history)) self.versus_test(axelrod.SuspiciousTitForTat(), expected_actions=actions)
def test_strategy(self): actions = [(C, C)] * 100 self.versus_test(axelrod.Cooperator(), expected_actions=actions) actions = [(C, D), (C, D), (D, D), (D, D)] self.versus_test(axelrod.Defector(), expected_actions=actions, seed=1) actions = [(C, D), (D, D), (D, D), (C, D)] self.versus_test(axelrod.Defector(), expected_actions=actions, seed=2) actions = [(C, D), (C, C), (D, C), (C, D), (D, C), (D, D), (C, D), (D, C), (C, D)] self.versus_test(axelrod.SuspiciousTitForTat(), expected_actions=actions, seed=1) actions = [(C, C), (C, D), (D, C)] + [(D, D), (C, C)] * 3 self.versus_test(axelrod.Alternator(), expected_actions=actions, seed=2) actions = [(C, C), (C, D), (C, C)] + [(D, D), (C, C)] * 3 self.versus_test(axelrod.Alternator(), expected_actions=actions, seed=3)
def test_strategy(self): actions = [(C, C)] * 7 self.versus_test(axelrod.Cooperator(), expected_actions=actions) actions = [(C, C), (C, D), (D, C)] self.versus_test(axelrod.Alternator(), expected_actions=actions) opponent = axelrod.MockPlayer(actions=[D] * 8) actions = [(C, D)] * 2 + [(D, D)] * 5 + [(C, D)] + [(C, D)] self.versus_test(opponent, expected_actions=actions, seed=1) opponent = axelrod.MockPlayer(actions=[D] * 8) actions = [(C, D)] * 2 + [(D, D)] * 5 + [(C, D)] + [(D, D)] self.versus_test(opponent, expected_actions=actions, seed=2)
def test_strategy_mutually_cooperative(self): # tests states 2, 7, 14 and 11 actions = [(C, C), (C, D), (D, C), (D, D), (C, C), (C, D)] self.versus_test(opponent=axl.Alternator(), expected_actions=actions) # tests states 1, 4 and 8 actions = [(C, D), (C, D), (D, D), (C, C), (C, C), (C, D)] self.versus_test(opponent=axl.Cycler(["D", "D", "D", "C", "C"]), expected_actions=actions) # tests states 3, 5 actions = [(C, D), (C, C), (D, C), (D, D), (C, D)] self.versus_test(opponent=axl.SuspiciousTitForTat(), expected_actions=actions)
def test_opponent_starting_moves_table(self): """A lookup table that always repeats the opponent's first move.""" first_move_table = {((), (), (C, )): C, ((), (), (D, )): D} vs_alternator = [(C, C), (C, D)] * 5 self.versus_test(axelrod.Alternator(), expected_actions=vs_alternator, init_kwargs={'lookup_dict': first_move_table}) vs_initial_defector = [(C, D)] + [(D, C), (D, D)] * 10 opponent = axelrod.MockPlayer(actions=[D, C]) self.versus_test(opponent, expected_actions=vs_initial_defector, init_kwargs={'lookup_dict': first_move_table})
def test_defector_table_with_initial_cooperate(self): """ Testing a lookup table that always defects IF there is enough history. """ defector_table = { ((C, ), (D, ), ()): D, ((D, ), (D, ), ()): D, ((C, ), (C, ), ()): D, ((D, ), (C, ), ()): D } actions = [(C, C)] + [(D, D), (D, C)] * 4 self.versus_test(axelrod.Alternator(), expected_actions=actions, init_kwargs={'lookup_dict': defector_table})
def test_reset_history_and_attributes(self): """Make sure resetting works correctly.""" for opponent in [ axl.Defector(), axl.Random(), axl.Alternator(), axl.Cooperator(), ]: player = self.player() clone = player.clone() match = axl.Match((player, opponent), turns=10, seed=111) match.play() player.reset() self.assertEqual(player, clone)
def test_defects_after_four_defections(self): # Forgives three defections defector_actions = [(C, D), (C, D), (C, D), (C, D), (D, D), (D, D)] self.versus_test( axelrod.Defector(), expected_actions=defector_actions, match_attributes={"length": 200}, ) alternator_actions = [(C, C), (C, D)] * 4 + [(D, C), (D, D)] * 2 self.versus_test( axelrod.Alternator(), expected_actions=alternator_actions, match_attributes={"length": 200}, )
def test_strategy(self): actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] self.versus_test(opponent=axl.Alternator(), expected_actions=actions) actions = [(C, D), (D, D), (D, D), (D, D), (C, D)] self.versus_test(opponent=axl.Defector(), expected_actions=actions, seed=0) actions = [(C, D), (D, D), (C, D), (D, D), (D, D)] self.versus_test(opponent=axl.Defector(), expected_actions=actions, seed=1)
def test_wsls(self): """Tests that the player defined by the table for TFT is in fact WSLS (also known as Pavlov.""" wsls_init_kwargs = { "transitions": ((1, C, 1, C), (1, D, 2, D), (2, C, 2, D), (2, D, 1, C)), "initial_state": 1, "initial_action": C, } expected = [(C, C), (C, D), (D, C), (D, D)] * 3 self.versus_test( axelrod.Alternator(), expected_actions=expected, init_kwargs=wsls_init_kwargs, )
def test_random_behavior2(self): actions = [ (D, C), (C, D), (C, C), (C, D), (C, C), (D, D), (C, C), (D, D), (C, C), (D, D), ] self.versus_test(axl.Alternator(), expected_actions=actions, seed=2)
def test_strategy(self): # Starts by defecting. self.first_play_test(D) # Will do opposite of what opponent does. self.second_play_test(rCC=D, rCD=C, rDC=D, rDD=C) actions = [(D, C), (D, D), (C, C), (D, D), (C, C)] self.versus_test(axelrod.Alternator(), expected_actions=actions) actions = [(D, C), (D, C), (D, C), (D, C), (D, C)] self.versus_test(axelrod.Cooperator(), expected_actions=actions) actions = [(D, D), (C, D), (C, D), (C, D), (C, D)] self.versus_test(axelrod.Defector(), expected_actions=actions)
def test_strategy(self): actions = [(C, C), (C, D)] * 3 + [(D, C), (D, D)] * 5 self.versus_test( opponent=axl.Alternator(), expected_actions=actions, attrs={"is_alt": True}, ) actions = [(C, D)] * 14 self.versus_test( opponent=axl.Defector(), expected_actions=actions, attrs={"is_alt": False}, )
def test_strategy(self): # Cooperates initially (not stochastic) self.first_play_test(C, seed=1) self.first_play_test(C, seed=2) # Tests that if opponent has played all C then player chooses C. actions = [(C, C)] * 10 self.versus_test(axelrod.Cooperator(), expected_actions=actions, seed=1) # Tests that if opponent has played all D then player chooses D. actions = [(C, D)] + [(D, D)] * 9 self.versus_test(axelrod.Defector(), expected_actions=actions, seed=1) # Variable behaviour based on the history and stochastic behaviour actions = [(C, C), (C, D), (C, C), (D, D), (D, C), (C, D), (C, C), (C, D), (D, C), (D, D)] self.versus_test(axelrod.Alternator(), expected_actions=actions, seed=1) actions = [(C, C), (C, D), (D, C), (D, D), (C, C), (C, D), (D, C), (D, D), (D, C), (C, D)] self.versus_test(axelrod.Alternator(), expected_actions=actions, seed=2) opponent = axelrod.MockPlayer([C, C, D, D, D, D]) actions = [(C, C), (C, C), (C, D), (C, D), (D, D), (D, D), (C, C), (D, C), (C, D), (D, D)] self.versus_test(opponent, expected_actions=actions, seed=1) opponent = axelrod.MockPlayer([C, C, C, D, D, D]) actions = [(C, C), (C, C), (C, C), (C, D), (D, D), (D, D), (C, C), (C, C), (D, C), (D, D)] self.versus_test(opponent, expected_actions=actions, seed=2)
def test_strategy(self): self.first_play_test(C) self.second_play_test(rCC=C, rCD=D, rDC=C, rDD=D) # Play against opponents actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] self.versus_test(axelrod.Alternator(), expected_actions=actions) actions = [(C, C), (C, C), (C, C), (C, C), (C, C)] self.versus_test(axelrod.Cooperator(), expected_actions=actions) actions = [(C, D), (D, D), (D, D), (D, D), (D, D)] self.versus_test(axelrod.Defector(), expected_actions=actions) # This behaviour is independent of knowledge of the Match length actions = [(C, C), (C, D), (D, C), (C, D), (D, C)] self.versus_test(axelrod.Alternator(), expected_actions=actions, match_attributes={"length": -1}) # We can also test against random strategies actions = [(C, D), (D, D), (D, C), (C, C), (C, D)] self.versus_test(axelrod.Random(), expected_actions=actions, seed=0) actions = [(C, C), (C, D), (D, D), (D, C)] self.versus_test(axelrod.Random(), expected_actions=actions, seed=1) # If you would like to test against a sequence of moves you should use # a MockPlayer opponent = axelrod.MockPlayer(actions=[C, D]) actions = [(C, C), (C, D), (D, C), (C, D)] self.versus_test(opponent, expected_actions=actions) opponent = axelrod.MockPlayer(actions=[C, C, D, D, C, D]) actions = [(C, C), (C, C), (C, D), (D, D), (D, C), (C, D)] self.versus_test(opponent, expected_actions=actions)
def test_strategy(self): # Our Player (SteinAndRapoport) vs Cooperator # After 15th round (pvalue < alpha) still plays TitForTat. # Note it always defects on the last two rounds. opponent = axelrod.Cooperator() actions = [(C, C)] * 17 + [(D, C)] * 2 self.versus_test(opponent, expected_actions=actions, attrs={"opponent_is_random": False}) actions = actions[:-2] + [(C, C)] * 2 self.versus_test(opponent, expected_actions=actions[:-2], match_attributes={"length": -1}, attrs={"opponent_is_random": False}) # SteinAndRapoport vs Defector # After 15th round (p-value < alpha) still plays TitForTat. opponent = axelrod.Defector() actions = [(C, D)] * 4 + [(D, D)] * 15 self.versus_test(opponent, expected_actions=actions, attrs={"opponent_is_random": False}) # SteinAndRapoport vs Alternator # After 15th round (p-value > alpha) starts defecting. opponent = axelrod.Alternator() actions = [(C, C), (C, D), (C, C), (C, D)] # On 15th round carry out chi-square test. actions += [(D, C), (C, D)] * 5 + [(D, C)] # Defect throughout. actions += [(D, D), (D, C), (D, D), (D, C)] self.versus_test(opponent, expected_actions=actions, attrs={"opponent_is_random": True}) # The test is carried out again every 15 rounds. # If the strategy alternates for the first 12 rounds and then cooperates # it is no longer recognised as random. opponent = axelrod.MockPlayer([C, D] * 6 + [C] * 50) actions = [(C, C), (C, D), (C, C), (C, D)] # On 15th round carry out chi-square test. actions += [(D, C), (C, D)] * 4 + [(D, C), (C, C), (D, C)] # Defect throughout and carry out chi-square test on round 30. # Opponent is no longer recognised as random, revert to TFT. actions += [(D, C)] * 14 + [(C, C)] self.versus_test(opponent, expected_actions=actions, match_attributes={"length": -1}, attrs={"opponent_is_random": False})
def test_strategy(self): actions = [(C, C)] * 100 self.versus_test(axelrod.Cooperator(), expected_actions=actions) actions = [(C, D), (C, D), (D, D), (D, D), (D, D)] self.versus_test(axelrod.Defector(), expected_actions=actions, seed=1, attrs={"flack": 15. / 16.}) actions = [(C, C), (C, D), (C, C), (C, D), (D, C), (C, D)] self.versus_test(axelrod.Alternator(), expected_actions=actions, seed=4, attrs={"flack": 5. / 16.})
def test_strategy(self): """Test that the strategy gives expected behaviour.""" actions = [(C, C)] + [(D, C)] * 4 self.versus_test(axelrod.Cooperator(), expected_actions = actions, match_attributes={"length":5}, seed=1) # Test that behaviour does not depend on opponent actions = [(C, D)] + [(D, D)] * 4 self.versus_test(axelrod.Defector(), expected_actions = actions, match_attributes={"length":5}, seed=1) # Test that behaviour changes when does not know length. actions = [(C, C), (C, D), (C, C), (C, D), (C, C)] self.versus_test(axelrod.Alternator(), expected_actions = actions, match_attributes={"length":-1}, seed=1)
def setUpClass(cls): players = [axl.Alternator(), axl.TitForTat()] tournament = axl.Tournament(players) cls.results = tournament.play() cls.data = TournamentResultsSerializer(cls.results).data cls.expected_keys = [ 'filename', 'num_interactions', 'players', 'repetitions', 'nplayers', 'match_lengths', 'wins', 'scores', 'normalised_scores', 'payoffs', 'score_diffs', 'cooperation', 'normalised_cooperation', 'initial_cooperation_count', 'good_partner_matrix', 'total_interactions', 'good_partner_rating', 'ranking', 'ranked_names', 'payoff_matrix', 'payoff_stddevs', 'payoff_diffs_means', 'vengeful_cooperation', 'cooperating_rating', 'initial_cooperation_rate', 'eigenjesus_rating', 'eigenmoses_rating', 'wins', ]
def test_strategy(self): # Test that will forgive one D but will grudge after 2 Ds, randomly # forgets count. actions = [(C, C), (C, D), (C, C), (C, D), (D, C)] self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions, seed=2, attrs={"D_count": 2}) # Sometime eventually forget count: actions = [(C, D), (C, D)] + [(D, D)] * 18 + [(C, D)] self.versus_test(opponent=axelrod.Defector(), expected_actions=actions, seed=2, attrs={"D_count": 0})
def test_strategy(self): R, P, S, T = axelrod.Game().RPST() player = self.player(l=R) axelrod.seed(0) match = axelrod.Match((player, axelrod.Alternator()), turns=200) match.play() player = self.player(l=P) axelrod.seed(0) match = axelrod.Match((player, axelrod.Alternator()), turns=200) match.play() player = self.player(s=1) axelrod.seed(0) match = axelrod.Match((player, axelrod.Alternator()), turns=200) match.play() l = 2 s_min = - min((T - l) / (l - S), (l - S) / (T - l)) player = self.player(s=s_min, l=2) axelrod.seed(0) match = axelrod.Match((player, axelrod.Alternator()), turns=200) match.play()
def test_strategy(self): actions = [(C, C)] * 100 self.versus_test(axelrod.Cooperator(), expected_actions=actions) actions = [(C, D)] + [(D, D)] * 8 self.versus_test(axelrod.Defector(), expected_actions=actions, attrs={"score_to_beat_inc": 5}) actions = [(C, D)] + [(D, D)] * 8 # On tenth turn, try a fresh start actions += [(C, D), (C, D)] + [(D, D)] * 2 self.versus_test(axelrod.Defector(), expected_actions=actions, attrs={"last_fresh_start": 11}) actions = [(C, C), (C, D)] # Scores and score_to_beat variables are a turn behind self.versus_test(axelrod.Alternator(), expected_actions=actions, attrs={"current_score": 3, "opponent_score": 3, "score_to_beat": 0, "score_to_beat_inc": 0}) actions += [(D, C), (C, D)] self.versus_test(axelrod.Alternator(), expected_actions=actions, attrs={"current_score": 8, "opponent_score": 8, "score_to_beat": 0, "score_to_beat_inc": 5}) actions += [(D, C), (D, D)] self.versus_test(axelrod.Alternator(), expected_actions=actions, attrs={"current_score": 13, "opponent_score": 13, "score_to_beat": 5, "score_to_beat_inc": 10}) actions += [(D, C), (D, D)] self.versus_test(axelrod.Alternator(), expected_actions=actions, attrs={"current_score": 19, "opponent_score": 14, "score_to_beat": 15, "score_to_beat_inc": 15}) actions += [(D, C), (D, D)] self.versus_test(axelrod.Alternator(), expected_actions=actions, attrs={"current_score": 25, "opponent_score": 15, "score_to_beat": 30, "score_to_beat_inc": 20}) # Build an opponent who will cause us to consider a Fresh Start, but # will fail the binomial test. opponent_actions = [C] * 5 + [D] * 5 C5D5_Player = axelrod.MockPlayer(actions=opponent_actions) actions = [(C, C)] * 5 + [(C, D)] + [(D, D)] * 3 actions += [(D, D)] # No Defection here means no Fresh Start. self.versus_test(C5D5_Player, expected_actions=actions)
def test_strategies_without_countermeasures_return_their_strategy(self): tft = axl.TitForTat() inspector = axl.Alternator() match = axl.Match((tft, inspector), turns=1) match.play() self.assertEqual(tft.history, [C]) self.assertEqual(inspect_strategy(inspector=inspector, opponent=tft), C) match = axl.Match((tft, inspector), turns=2) match.play() self.assertEqual(tft.history, [C, C]) self.assertEqual(inspect_strategy(inspector=inspector, opponent=tft), D) self.assertEqual(tft.strategy(inspector), D)
def test_strategy(self): opponent = axelrod.Alternator() actions = [(C, C), (C, D), (D, C)] self.versus_test( opponent=opponent, expected_actions=actions, attrs={ "grudged": True, "grudge_memory": 0 }, ) opponent = axelrod.MockPlayer([C, D] + [C] * 10) actions = [(C, C), (C, D)] + [(D, C)] * 11 self.versus_test( opponent=opponent, expected_actions=actions, attrs={ "grudged": True, "grudge_memory": 10 }, ) # Eventually the grudge is dropped opponent = axelrod.MockPlayer([C, D] + [C] * 10) actions = [(C, C), (C, D)] + [(D, C)] * 11 + [(C, D)] self.versus_test( opponent=opponent, expected_actions=actions, attrs={ "grudged": False, "grudge_memory": 0, "mem_length": 10 }, ) # Grudged again on opponent's D opponent = axelrod.MockPlayer([C, D] + [C] * 11) actions = [(C, C), (C, D)] + [(D, C)] * 11 + [(C, C), (C, D), (D, C)] self.versus_test( opponent=opponent, expected_actions=actions, attrs={ "grudged": True, "grudge_memory": 0, "mem_length": 2 }, )
def test_play_single_interaction(self): players = [ axelrod.Alternator(), axelrod.Defector(), axelrod.TitForTat() ] rr = axelrod.RoundRobin(players=players, game=self.game, turns=20) player1 = players[0] player2 = players[2] classes = (player1.__class__, player2.__class__) scores, cooperation_rates = (rr._play_single_interaction( player1, player2, classes)) expected_scores = (53, 48) expected_cooperation_rates = (10, 11) self.assertEqual(expected_scores, scores) self.assertEqual(expected_cooperation_rates, cooperation_rates)