def test_strategy(self): axelrod.seed(0) vector = [random.random() for _ in range(16)] actions = [(C, C), (C, C), (D, D), (D, C), (C, C), (C, D), (C, C)] self.versus_test( opponent=axelrod.CyclerCCD(), expected_actions=actions, seed=0, init_kwargs={"sixteen_vector": vector}, ) actions = [(C, C), (C, C), (C, D), (D, C), (C, C), (C, D), (C, C)] self.versus_test( opponent=axelrod.CyclerCCD(), expected_actions=actions, seed=1, init_kwargs={"sixteen_vector": vector}, ) actions = [(C, C), (C, C), (D, C), (D, D), (C, D), (C, C), (D, C)] self.versus_test( opponent=axelrod.TitForTat(), expected_actions=actions, seed=0, init_kwargs={"sixteen_vector": vector}, ) actions = [(C, C), (C, C), (C, C), (D, C), (D, D), (C, D), (C, C)] self.versus_test( opponent=axelrod.TitForTat(), expected_actions=actions, seed=1, init_kwargs={"sixteen_vector": vector}, )
def test_strategy(self): rng = axl.RandomGenerator(seed=7888) vector = [rng.random() for _ in range(16)] actions = [(C, C), (C, C), (D, D), (C, C), (D, C), (D, D), (D, C)] self.versus_test( opponent=axl.CyclerCCD(), expected_actions=actions, seed=0, init_kwargs={"sixteen_vector": vector}, ) actions = [(C, C), (C, C), (C, D), (C, C), (D, C), (D, D), (D, C)] self.versus_test( opponent=axl.CyclerCCD(), expected_actions=actions, seed=1, init_kwargs={"sixteen_vector": vector}, ) actions = [(C, C), (C, C), (D, C), (D, D), (D, D), (D, D), (D, D)] self.versus_test( opponent=axl.TitForTat(), expected_actions=actions, seed=0, init_kwargs={"sixteen_vector": vector}, ) actions = [(C, C), (C, C), (C, C), (D, C), (D, D), (D, D), (D, D)] self.versus_test( opponent=axl.TitForTat(), expected_actions=actions, seed=1, init_kwargs={"sixteen_vector": vector}, )
def test_strategy(self): self.first_play_test(C) player = self.player() # Test against cyclers for opponent in [ axelrod.CyclerCCD(), axelrod.CyclerCCCD(), axelrod.CyclerCCCCCD(), axelrod.Alternator() ]: player.reset() for i in range(30): player.play(opponent) self.assertEqual(player.history[-1], D) # Test against non-cyclers axelrod.seed(40) for opponent in [ axelrod.Random(), axelrod.AntiCycler(), axelrod.Cooperator(), axelrod.Defector() ]: player.reset() for i in range(30): player.play(opponent) self.assertEqual(player.history[-1], C)
def test_strategy(self): player = self.player() # Test against cyclers for opponent in [ axelrod.CyclerCCD(), axelrod.CyclerCCCD(), axelrod.CyclerCCCCCD(), axelrod.Alternator(), ]: player.reset() for i in range(50): player.play(opponent) self.assertEqual(player.history[-1], D) # Test against non-cyclers and cooperators axelrod.seed(43) for opponent in [ axelrod.Random(), axelrod.AntiCycler(), axelrod.DoubleCrosser(), axelrod.Cooperator(), ]: player.reset() for i in range(50): player.play(opponent) self.assertEqual(player.history[-1], C)
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 tft_strats(): strategies = [ axelrod.TitForTat(), axelrod.Alternator(), axelrod.CyclerCCD(), axelrod.CyclerCCCD(), axelrod.CyclerCCCCCD(), axelrod.AntiCycler(), axelrod.WinStayLoseShift(), axelrod.FoolMeOnce() ] return strategies
def test_flip_state_distribution(self): player = axelrod.Alternator() opponent = axelrod.CyclerCCD() for _ in range(16): player.play(opponent) expected = defaultdict(int, {(C, C): 5, (D, C): 6, (C, D): 3, (D, D): 2}) self.assertEqual(player.state_distribution, expected) flip_state_distribution(player) flip_expected = defaultdict(int, {(D, C): 5, (C, C): 6, (D, D): 3, (C, D): 2}) self.assertEqual(player.state_distribution, flip_expected)
def test_dual_tft_transformer(self): """Tests that DualTransformer produces the opposite results when faced with the same opponent history. """ p1 = axelrod.TitForTat() p2 = DualTransformer()(axelrod.TitForTat)() p3 = axelrod.CyclerCCD() # Cycles 'CCD' 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_flip_play_attributes(self): p1 = axelrod.WinStayLoseShift() p2 = DualTransformer()(axelrod.WinStayLoseShift)() p3 = axelrod.CyclerCCD() for _ in range(10): p1.play(p3) p3.reset() for _ in range(10): p2.play(p3) flip_play_attributes(p1) self.assertEqual(p1.history, p2.history) self.assertEqual(p1.cooperations, p2.cooperations) self.assertEqual(p1.defections, p2.defections) self.assertEqual(p1.state_distribution, p2.state_distribution)
def assert_dual_wrapper_correct(self, player_class): """Show that against an identical opponent, the dual transformer reverses all actions correctly.""" turns = 20 seed = 1 p1 = player_class() p2 = DualTransformer()(player_class)() p3 = axl.CyclerCCD() # Cycles 'CCD' match = axl.Match((p1, p3), turns=turns, seed=seed) match.play() p3.reset() match = axl.Match((p2, p3), turns=turns, seed=seed) match.play() self.assertEqual(p1.history, [x.flip() for x in p2.history])
def assert_dual_wrapper_correct(self, player_class): turns = 100 p1 = player_class() p2 = DualTransformer()(player_class)() p3 = axelrod.CyclerCCD() # Cycles 'CCD' axelrod.seed(0) for _ in range(turns): p1.play(p3) p3.reset() axelrod.seed(0) for _ in range(turns): p2.play(p3) self.assertEqual(p1.history, [x.flip() for x in p2.history])
def test_output_from_literature(self): """ This strategy is not fully described in the literature, however the following two results are reported in: 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. """ player = axelrod.Gradual() opp1 = axelrod.Defector() match = axelrod.Match((player, opp1), 1000) match.play() self.assertEqual(match.final_score(), (915, 1340)) opp2 = axelrod.CyclerCCD() match = axelrod.Match((player, opp2), 1000) match.play() self.assertEqual(match.final_score(), (3472, 767))
def main(): strategies = [ axelrod.Cooperator(), axelrod.Defector(), axelrod.Random(0.4), axelrod.Random(0.5), axelrod.Random(0.9), axelrod.Alternator(), axelrod.TitForTat(), axelrod.GTFT(), axelrod.WinStayLoseShift(), axelrod.ZDGTFT2(), axelrod.ZDExtort2(), axelrod.TitFor2Tats(), axelrod.TwoTitsForTat(), axelrod.CyclerCCD(), axelrod.CyclerCCCD(), axelrod.CyclerCCCCCD(), axelrod.HardTitForTat(), axelrod.AntiCycler(), axelrod.Grudger() ] for opponent in strategies: data_dict, test_results, estimate = infer_depth(opponent) print opponent print "-" * len(str(opponent)) print "Collected Data" print_dict(data_dict) C_count = sum(v[0] for (k, v) in data_dict.items()) D_count = sum(v[1] for (k, v) in data_dict.items()) print "C count, D count: %s, %s" % (C_count, D_count) print "\nFisher Exact Tests" print_dict(test_results) print "\nEstimated Memory One Probabilities" print_dict(estimate) print
def test_retailiation(self): """Tests the RetaliateTransformer.""" p1 = RetaliationTransformer(1)(axelrod.Cooperator)() p2 = axelrod.Defector() for _ in range(5): p1.play(p2) self.assertEqual(p1.history, [C, D, D, D, D]) self.assertEqual(p2.history, [D, D, D, D, D]) p1 = RetaliationTransformer(1)(axelrod.Cooperator)() p2 = axelrod.Alternator() for _ in range(5): p1.play(p2) self.assertEqual(p1.history, [C, C, D, C, D]) self.assertEqual(p2.history, [C, D, C, D, C]) TwoTitsForTat = RetaliationTransformer(2)(axelrod.Cooperator) p1 = TwoTitsForTat() p2 = axelrod.CyclerCCD() for _ in range(9): p1.play(p2) self.assertEqual(p1.history, [C, C, C, D, D, C, D, D, C]) self.assertEqual(p2.history, [C, C, D, C, C, D, C, C, D])
def test_strategy(self): player = self.player() # Test against cyclers for opponent in [ axl.CyclerCCD(), axl.CyclerCCCD(), axl.CyclerCCCCCD(), axl.Alternator(), ]: player.reset() match = Match((player, opponent), turns=50) match.play() self.assertEqual(player.history[-1], D) # Test against non-cyclers and cooperators for opponent in [ axl.Random(), axl.AntiCycler(), axl.DoubleCrosser(), axl.Cooperator(), ]: player.reset() match = Match((player, opponent), turns=50, seed=43) match.play() self.assertEqual(player.history[-1], C)
selfScore = [] oppScore = [] selfAvgList = [] oppAvgList = [] avgScore = [] oppAvgScore = [] evolveCode = [0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0,\ 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0] # For final experiments singEvolveCode = [0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1,\ 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0] # strategies = [axelrod.Cooperator(), axelrod.Defector()] # strategies = [axelrod.TitFor2Tats(), axelrod.SuspiciousTitForTat(), axelrod.TitForTat(), axelrod.Prober()] strategies = [axelrod.Cooperator(), axelrod.Defector(), axelrod.CyclerCCD(),axelrod.HardTitForTat(),\ axelrod.TitFor2Tats(), axelrod.SuspiciousTitForTat(), axelrod.Random(), axelrod.TitForTat(), axelrod.Prober()] learner = axelrod.LearnerAxel(memory_depth=2, exploreProb=0.1, learnerType=2) multAxel = axelrod.EvolveAxel(3, evolveCode, 'MULT') singAxel = axelrod.EvolveAxel(3, evolveCode, 'SING') ply = learner # print ply for p in strategies: print "Currently playing against strategy:", p for turn in range(numTurns): ply.play(p) selfList = map(ScoreMatrix, zip(ply.history, p.history))
def test_retailiating_cooperator_against_2TFT(self): TwoTitsForTat = RetaliationTransformer(2)(axl.Cooperator) p1 = TwoTitsForTat() p2 = axl.CyclerCCD() self.versus_test(p1, p2, [C, C, C, D, D, C, D, D, C], [C, C, D, C, C, D, C, C, D])
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(