def tscizzle_strategies(): """The list of strategies used in @tscizzle's Morality Metrics paper.""" strategies = [ axelrod.Cooperator(), axelrod.Defector(), axelrod.Eatherley(), axelrod.Champion(), axelrod.GTFT(p=0.1), axelrod.GTFT(p=0.3), axelrod.GoByMajority(soft=True), axelrod.GoByMajority(soft=False), axelrod.TitFor2Tats(), axelrod.Random(0.8), axelrod.Random(0.5), axelrod.Random(0.2), axelrod.WinStayLoseShift(), # Pavlov axelrod.TitForTat(), axelrod.TwoTitsForTat(), axelrod.Grudger(), # Friedman axelrod.Tester(), axelrod.SuspiciousTitForTat(), axelrod.Joss(0.9), axelrod.Joss(0.7), ] return strategies
def test_initial_strategy(self): """ Starts by cooperating """ P1 = axelrod.GoByMajority() P2 = axelrod.Player() self.assertEqual(P1.strategy(P2), 'C')
def test_tournament(self): """Test tournament.""" outcome = [('Cooperator', [1800, 1800, 1800, 1800, 1800]), ('Defector', [1612, 1612, 1612, 1612, 1612]), ('Go By Majority', [1999, 1999, 1999, 1999, 1999]), ('Grudger', [1999, 1999, 1999, 1999, 1999]), ('Tit For Tat', [1999, 1999, 1999, 1999, 1999])] outcome.sort() P1 = axelrod.Cooperator() P2 = axelrod.TitForTat() P3 = axelrod.Defector() P4 = axelrod.Grudger() P5 = axelrod.GoByMajority() tournament = axelrod.Tournament(name='test', players=[P1, P2, P3, P4, P5], game=self.game, turns=200, repetitions=5) names = [str(p) for p in tournament.players] results = tournament.play().results scores = [[ sum([r[i] for ir, r in enumerate(res) if ir != ires]) for i in range(5) ] for ires, res in enumerate(results)] self.assertEqual(sorted(zip(names, scores)), outcome)
def sp_strategies(): """The list of strategies used in Stewart and Plotkin's 2012 tournament.""" strategies = [ axelrod.Cooperator(), # ALLC axelrod.Defector(), # ALLD axelrod.GTFT(), axelrod.GoByMajority(soft=False), # HARD_MAJO #axelrod.GoByMajority(soft=True), # SOFT_MAJO axelrod.TitFor2Tats(), # TFT2 axelrod.HardTitFor2Tats(), # HARD_TFT2 axelrod.Random(), # RANDOM axelrod.WinStayLoseShift(), # WSLS axelrod.TitForTat(), axelrod.HardTitForTat(), # HARD_TFT axelrod.Grudger(), # GRIM axelrod.Joss(), # HARD_JOSS axelrod.ZDGTFT2(), axelrod.ZDExtort2(), axelrod.Prober(), axelrod.Prober2(), axelrod.Prober3(), axelrod.HardProber(), axelrod.Calculator(), ] return strategies
def test_output_from_literature(self): """ This strategy is not fully described in the literature, however the scores for the strategy against a set of opponents is reported Bruno Beaufils, Jean-Paul Delahaye, Philippe Mathie "Our Meeting With Gradual: A Good Strategy For The Iterated Prisoner's Dilemma" Proc. Artif. Life 1996 This test just ensures that the strategy is as was originally defined. See https://github.com/Axelrod-Python/Axelrod/issues/1294 for another discussion of this. """ players = [ axl.Cooperator(), axl.Defector(), axl.Random(), axl.TitForTat(), axl.Grudger(), axl.CyclerDDC(), axl.CyclerCCD(), axl.GoByMajority(), axl.SuspiciousTitForTat(), axl.Prober(), self.player(), axl.WinStayLoseShift(), ] turns = 1000 tournament = axl.Tournament(players, turns=turns, repetitions=1, seed=75) results = tournament.play(progress_bar=False) scores = [ round(average_score_per_turn * 1000, 1) for average_score_per_turn in results.payoff_matrix[-2] ] expected_scores = [ 3000.0, 915.0, 2763.0, 3000.0, 3000.0, 2219.0, 3472.0, 3000.0, 2996.0, 2999.0, 3000.0, 3000.0, ] self.assertEqual(scores, expected_scores)
def test_round_robin_cooperator_v_titfortat_v_defector_v_grudger_v_go_by_majority(self): """ Test round robin: Tit for tat now wins """ P1 = axelrod.Cooperator() P2 = axelrod.TitForTat() P3 = axelrod.Defector() P4 = axelrod.Grudger() P5 = axelrod.GoByMajority() tournament = axelrod.Axelrod(P1, P2, P3, P4, P5) tournament.round_robin(turns=10) self.assertEqual([(str(player), player.score) for player in sorted(tournament.players, key=lambda x: x.score)], [('Tit For Tat', 101), ('Grudger', 101), ('Go By Majority', 101), ('Defector', 108), ('Cooperator', 110)])
def test_effect_of_strategy(self): """ If opponent cooperates at least as often as they defect then the player cooperates """ P1 = axelrod.GoByMajority() P2 = axelrod.Player() P1.history = ['C', 'D', 'D', 'D'] P2.history = ['D', 'D', 'C', 'C'] self.assertEqual(P1.strategy(P2), 'C') P1.history = ['C', 'C', 'D', 'D', 'D'] P2.history = ['D', 'D', 'C', 'C', 'D'] self.assertEqual(P1.strategy(P2), 'D')
def test_dual_majority_transformer(self): """Tests that DualTransformer produces the opposite results when faced with the same opponent history. """ p1 = axelrod.GoByMajority() p2 = DualTransformer()(axelrod.GoByMajority)() p3 = axelrod.Cycler('CDD') # Cycles 'CDD' for _ in range(10): p1.play(p3) p3.reset() for _ in range(10): p2.play(p3) self.assertEqual(p1.history, [flip_action(x) for x in p2.history])
def test_specific_set_of_results(self): """ This tests specific reported results as discussed in https://github.com/Axelrod-Python/Axelrod/issues/1294 The results there used a version of mistrust with a bug that corresponds to a memory one player that start by defecting and only cooperates if both players cooperated in the previous round. """ mistrust_with_bug = axl.MemoryOnePlayer( initial=D, four_vector=(1, 0, 0, 0), ) players = [ self.player(), axl.TitForTat(), axl.GoByMajority(), axl.Grudger(), axl.WinStayLoseShift(), axl.Prober(), axl.Defector(), mistrust_with_bug, axl.Cooperator(), axl.CyclerCCD(), axl.CyclerDDC(), ] axl.seed(1) tournament = axl.Tournament(players, turns=1000, repetitions=1) results = tournament.play(progress_bar=False) scores = [ round(average_score_per_turn * 1000, 1) for average_score_per_turn in results.payoff_matrix[0] ] expected_scores = [ 3000.0, 3000.0, 3000.0, 3000.0, 3000.0, 2999.0, 983.0, 983.0, 3000.0, 3596.0, 2302.0, ] self.assertEqual(scores, expected_scores)
def test_tournament(self): """ Test tournament """ P1 = axelrod.Cooperator() P2 = axelrod.TitForTat() P3 = axelrod.Defector() P4 = axelrod.Grudger() P5 = axelrod.GoByMajority() tournament = axelrod.Axelrod(P1, P2, P3, P4, P5) results = tournament.tournament(turns=200, repetitions=5) self.assertEqual(sorted([(str(player), results[player]) for player in sorted(results.keys())]), sorted([ ('Tit For Tat', [2001, 2001, 2001, 2001, 2001]), ('Cooperator', [2200, 2200, 2200, 2200, 2200]), ('Defector', [2388, 2388, 2388, 2388, 2388]), ('Grudger', [2001, 2001, 2001, 2001, 2001]), ('Go By Majority', [2001, 2001, 2001, 2001, 2001])]))
def setUpClass(cls): cls.game = axelrod.Game() cls.players = [ axelrod.Cooperator(), axelrod.TitForTat(), axelrod.Defector(), axelrod.Grudger(), axelrod.GoByMajority()] cls.player_names = [str(p) for p in cls.players] cls.test_name = 'test' cls.test_repetitions = 5 cls.expected_outcome = [ ('Cooperator', [180, 180, 180, 180, 180]), ('Defector', [172, 172, 172, 172, 172]), ('Grudger', [199, 199, 199, 199, 199]), ('Soft Go By Majority', [199, 199, 199, 199, 199]), ('Tit For Tat', [199, 199, 199, 199, 199])] cls.expected_outcome.sort()
def setUpClass(cls): cls.game = axelrod.Game() cls.players = [ axelrod.Cooperator(), axelrod.TitForTat(), axelrod.Defector(), axelrod.Grudger(), axelrod.GoByMajority(), ] cls.player_names = [str(p) for p in cls.players] cls.test_name = "test" cls.test_repetitions = 3 cls.expected_outcome = [ ("Cooperator", [45, 45, 45]), ("Defector", [52, 52, 52]), ("Grudger", [49, 49, 49]), ("Soft Go By Majority", [49, 49, 49]), ("Tit For Tat", [49, 49, 49]), ] cls.expected_outcome.sort()
def test_multiple_instances(self): """Certain instances of classes of strategies will have different classifiers based on the initialisation variables""" P1 = axelrod.MemoryOnePlayer((.5, .5, .5, .5)) P2 = axelrod.MemoryOnePlayer((1, 0, 0, 1)) self.assertNotEqual(P1.classifier, P2.classifier) P1 = axelrod.Joss() P2 = axelrod.Joss(0) self.assertNotEqual(P1.classifier, P2.classifier) P1 = axelrod.GTFT(1) P2 = axelrod.GTFT(.5) self.assertNotEqual(P1.classifier, P2.classifier) P1 = axelrod.StochasticWSLS() P2 = axelrod.StochasticWSLS(0) self.assertNotEqual(P1.classifier, P2.classifier) P1 = axelrod.GoByMajority(5) P2 = axelrod.StochasticWSLS(.1) self.assertNotEqual(P1.classifier, P2.classifier)
def test_multiple_instances(self): """Certain instances of classes of strategies will have different classifiers based on the initialisation variables""" P1 = axl.MemoryOnePlayer(four_vector=(.5, .5, .5, .5)) P2 = axl.MemoryOnePlayer(four_vector=(1, 0, 0, 1)) self.assertNotEqual(P1.classifier, P2.classifier) P1 = axl.Joss() P2 = axl.Joss(p=0) self.assertNotEqual(P1.classifier, P2.classifier) P1 = axl.GTFT(p=1) P2 = axl.GTFT(p=.5) self.assertNotEqual(P1.classifier, P2.classifier) P1 = axl.StochasticWSLS() P2 = axl.StochasticWSLS(ep=0) self.assertNotEqual(P1.classifier, P2.classifier) P1 = axl.GoByMajority(memory_depth=5) P2 = axl.StochasticWSLS(ep=.1) self.assertNotEqual(P1.classifier, P2.classifier)
def setUpClass(cls): cls.game = axelrod.Game() cls.players = [ axelrod.Cooperator(), axelrod.TitForTat(), axelrod.Defector(), axelrod.Grudger(), axelrod.GoByMajority() ] cls.test_name = 'test' cls.test_repetitions = 5 cls.expected_payoff = [[600, 600, 0, 600, 600], [600, 600, 199, 600, 600], [1000, 204, 200.0, 204, 204], [600, 600, 199, 600.0, 600], [600, 600, 199, 600, 600]] cls.expected_cooperation = [[200, 200, 200, 200, 200], [200, 200, 1, 200, 200], [0.0, 0.0, 0.0, 0.0, 0.0], [200, 200, 1, 200, 200], [200, 200, 1, 200, 200]]
axl.Tullock(), axl.UnnamedStrategy(), axl.Random() ] # The list of agents playing in the Axelrod's second tournament. Incomplete axl_second_players = [ axl.Black(), axl.Borufsen(), axl.Cave(), axl.Champion(), axl.Colbert(), axl.Eatherley(), axl.Getzler(), axl.Gladstein(), axl.GoByMajority(), axl.GraaskampKatzen(), axl.Harrington(), axl.Kluepfel(), axl.Leyvraz(), axl.Mikkelson(), axl.MoreGrofman(), axl.MoreTidemanAndChieruzzi(), axl.RichardHufford(), axl.Tester(), axl.Tranquilizer(), axl.Weiner(), axl.White(), axl.WmAdams(), axl.Yamachi() ]
import axelrod as axl strats = [axl.TitForTat, axl.WinStayLoseShift, axl.AntiTitForTat, axl.Cooperator, axl.Defector, axl.Cycler('CD'), axl.GoByMajority(75)] for s in strats: probe = axl.TitForTat af = axl.AshlockFingerprint(s, probe) data = af.fingerprint(turns=500, repetitions=200, step=0.01, processes=0) p = af.plot() p.savefig('/scratch/c1304586/img/{}-Numerical.pdf'.format(s.name))
scores['C']['D'] = (0, 5) scores['D']['D'] = (1, 1) def ScoreMatrix(moves): moveA = moves[0] moveB = moves[1] return scores[moveA][moveB][0] # strategies = [s() for s in axelrod.basic_strategies] #strategies = [axelrod.Cooperator(), axelrod.Defector(), axelrod.CyclerCCCD(), axelrod.CyclerCCD(), \ # axelrod.HardGoByMajority(), axelrod.GoByMajority(), axelrod.HardTitForTat(), axelrod.Random(), axelrod.TitForTat(), \ # axelrod.SuspiciousTitForTat(), axelrod.TitFor2Tats(), axelrod.Prober(), axelrod.Prober2(), axelrod.Prober3()] strategies = [axelrod.Cooperator(), axelrod.Defector(), axelrod.CyclerCCD(), axelrod.HardGoByMajority(), axelrod.GoByMajority(), \ axelrod.HardTitForTat(), axelrod.TitFor2Tats(), axelrod.SuspiciousTitForTat(), axelrod.Random(), axelrod.TitForTat(), axelrod.Prober()] creator.create( "FitnessMax", base.Fitness, weights=(1.0, )) #Weight is a tuple, so here we use a single objective creator.create( "Individual", list, fitness=creator.FitnessMax) #Define the structure of the individuals toolbox = base.Toolbox() #Initialize it? memLength = 3 toolbox.register("attr_bool", random.randint, 0, 1) # Random boolean toolbox.register(
def test_representation(self): P1 = axelrod.GoByMajority() self.assertEqual(str(P1), 'Go By Majority')
def test_stochastic(self): self.assertFalse(axelrod.GoByMajority().stochastic)