def getdata(n=1000, ld=10, players=[axl.Cooperator(), axl.Defector(), axl.TitForTat(), axl.WinStayLoseShift(), axl.Prober(), axl.Grudger()]): ds = ClassificationDataSet(ld*2, nb_classes=len(players)) j = 2 k = 2 while j<n: for a in (0, 0.01, 0.02): tournament = axl.Tournament(players, turns=ld, repetitions=1, noise=a) results = tournament.play(keep_interactions=True) print() for index_pair, interaction in results.interactions.items(): player1 = tournament.players[index_pair[0]] player2 = tournament.players[index_pair[1]] r1 = [] r2 = [] for i in interaction[0]: if i == (Action.C, Action.C): r2.append(1), r2.append(1), r1.append('R') # CC R 1 if i == (Action.D, Action.D): r2.append(-1), r2.append(-1), r1.append('P') # DD P 4 if i == (Action.D, Action.C): r2.append(-1), r2.append(1), r1.append('T') # DC T 2 if i == (Action.C, Action.D): r2.append(1), r2.append(-1), r1.append('S') # CD S 3 if r1 != (ld*['P']) and r1 != ld*['R'] and r1 != ld*['S'] and r1 != ld*['T']: print('%i %i %s (%s) vs %s (%s): %s' % (k, j, player1, index_pair[0], player2, index_pair[1], r1)) ds.addSample(r2, (index_pair[0])) j+=1 k+=1 print(len(ds)) return ds
def test_rounds(self): self.versus_test( axl.WinStayLoseShift(), axl.Bully(), [C, D, C, C, D], [D, D, C, D, D], )
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 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_wsls_fingerprint(self): axl.seed(0) # Fingerprinting is a random process. test_data = { Point(x=0.0, y=0.0): 3.000, Point(x=0.0, y=0.25): 1.710, Point(x=0.0, y=0.5): 1.440, Point(x=0.0, y=0.75): 1.080, Point(x=0.0, y=1.0): 0.500, Point(x=0.25, y=0.0): 3.000, Point(x=0.25, y=0.25): 2.280, Point(x=0.25, y=0.5): 1.670, Point(x=0.25, y=0.75): 1.490, Point(x=0.25, y=1.0): 0.770, Point(x=0.5, y=0.0): 3.000, Point(x=0.5, y=0.25): 2.740, Point(x=0.5, y=0.5): 2.240, Point(x=0.5, y=0.75): 1.730, Point(x=0.5, y=1.0): 1.000, Point(x=0.75, y=0.0): 3.000, Point(x=0.75, y=0.25): 3.520, Point(x=0.75, y=0.5): 2.830, Point(x=0.75, y=0.75): 1.750, Point(x=0.75, y=1.0): 1.250, Point(x=1.0, y=0.0): 3.000, Point(x=1.0, y=0.25): 4.440, Point(x=1.0, y=0.5): 4.410, Point(x=1.0, y=0.75): 4.440, Point(x=1.0, y=1.0): 1.300, } af = axl.AshlockFingerprint(axl.WinStayLoseShift(), axl.TitForTat) data = af.fingerprint(turns=50, repetitions=2, step=0.25, progress_bar=False) for key, value in data.items(): self.assertAlmostEqual(value, test_data[key], places=2)
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_init_with_instances(self): player = axl.WinStayLoseShift() fingerprint = AshlockFingerprint(player) self.assertEqual(fingerprint.strategy, player) self.assertEqual(fingerprint.probe, axl.TitForTat) probe = axl.Random() fingerprint = AshlockFingerprint(axl.WinStayLoseShift, probe) self.assertEqual(fingerprint.strategy, axl.WinStayLoseShift) self.assertEqual(fingerprint.probe, probe) fingerprint = AshlockFingerprint(player, probe) self.assertEqual(fingerprint.strategy, player) self.assertEqual(fingerprint.probe, probe)
def test_dual_wsls_transformer(self): """Tests that DualTransformer produces the opposite results when faced with the same opponent history. """ p1 = axelrod.WinStayLoseShift() p2 = DualTransformer()(axelrod.WinStayLoseShift)() 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 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_wsls_fingerprint(self): test_data = { Point(x=0.0, y=0.0): 3.0, Point(x=0.0, y=0.25): 1.83, Point(x=0.0, y=0.5): 1.12, Point(x=0.0, y=0.75): 1.04, Point(x=0.0, y=1.0): 0.5, Point(x=0.25, y=0.0): 3.0, Point(x=0.25, y=0.25): 2.12, Point(x=0.25, y=0.5): 2.17, Point(x=0.25, y=0.75): 1.33, Point(x=0.25, y=1.0): 0.77, Point(x=0.5, y=0.0): 3.0, Point(x=0.5, y=0.25): 2.9299999999999997, Point(x=0.5, y=0.5): 2.3600000000000003, Point(x=0.5, y=0.75): 1.74, Point(x=0.5, y=1.0): 1.05, Point(x=0.75, y=0.0): 3.0, Point(x=0.75, y=0.25): 2.52, Point(x=0.75, y=0.5): 2.79, Point(x=0.75, y=0.75): 2.41, Point(x=0.75, y=1.0): 1.2, Point(x=1.0, y=0.0): 3.0, Point(x=1.0, y=0.25): 4.86, Point(x=1.0, y=0.5): 4.36, Point(x=1.0, y=0.75): 4.05, Point(x=1.0, y=1.0): 1.3 } af = axl.AshlockFingerprint(axl.WinStayLoseShift(), axl.TitForTat) data = af.fingerprint(turns=50, repetitions=2, step=0.25, progress_bar=False, seed=0) for key, value in data.items(): self.assertAlmostEqual(value, test_data[key], places=2)
def test_rounds(self): self.versus_test(axelrod.TitForTat(), axelrod.WinStayLoseShift(), [C, C, C, C], [C, C, C, C])
axl.Prober(), axl.Prober2(), axl.Prober3(), axl.HardProber(), axl.NaiveProber() ] # The list of agents playing in the Case's simulation case_players = [ axl.Cooperator(), axl.Defector(), axl.TitForTat(), axl.Grudger(), axl.Detective(), axl.TitFor2Tats(), axl.WinStayLoseShift(), axl.Random() ] # The list of agents who won or survived the most based on the previous tournaments and simulations best_players = [ axl.SteinAndRapoport(), axl.Grudger(), axl.RevisedDowning(), axl.TitForTat(), axl.Davis(), axl.Nydegger(), axl.Cave(), axl.Tranquilizer(), axl.White(), axl.Eatherley(),
writer = csv.writer(f) writer.writerow([repetitions, N, i, *player_names, t, s]) if __name__ == "__main__": outcomes_file = "../data/outcomes.csv" output_file = "../data/fixation_validation.csv" with open(output_file, "w") as f: f.write("Repetitions,N,i,Player 1,Player 2,Theoretic,Simulated\n") player_pairs = [(axl.Defector(), axl.Defector()), (axl.Defector(), axl.Alternator()), (axl.Defector(), axl.Cooperator()), (axl.Defector(), axl.TitForTat()), (axl.Defector(), axl.WinStayLoseShift()), (axl.Random(), axl.Random()), (axl.Random(), axl.ZDExtort2()), (axl.Random(), axl.GTFT()), (axl.Random(), axl.ALLCorALLD()), (axl.Random(), axl.PSOGambler2_2_2()), (axl.Cooperator(), axl.Random()), (axl.Cooperator(), axl.ZDExtort2()), (axl.Cooperator(), axl.GTFT()), (axl.Cooperator(), axl.ALLCorALLD()), (axl.Cooperator(), axl.PSOGambler2_2_2()), (axl.Alternator(), axl.Random()), (axl.Alternator(), axl.ZDExtort2()), (axl.Alternator(), axl.GTFT()), (axl.Alternator(), axl.ALLCorALLD()), (axl.Alternator(), axl.PSOGambler2_2_2()),