def simulate_tournament_utility(player, opponents, turns=500, repetitions=200): """ Returns the simulated utility of a memory one strategy in a tournament. """ strategies = [axl.MemoryOnePlayer(p) for p in opponents] + [axl.MemoryOnePlayer(player)] number_of_players = len(strategies) edges = [(i, number_of_players - 1) for i in range(number_of_players - 1)] tournament = axl.Tournament(players=strategies, turns=turns, repetitions=repetitions, edges=edges) results = tournament.play(progress_bar=False) return np.mean(results.normalised_scores[-1])
def test_matches_with_det_player_for_stochastic_classes(self): """A test based on a bug found in the cache. See: https://github.com/Axelrod-Python/Axelrod/issues/779""" p1 = axl.MemoryOnePlayer(four_vector=(0, 0, 0, 0)) p2 = axl.MemoryOnePlayer(four_vector=(1, 0, 1, 0)) p3 = axl.MemoryOnePlayer(four_vector=(1, 1, 1, 0)) m = axl.Match((p1, p2), turns=3) self.assertEqual(m.play(), [(C, C), (D, C), (D, D)]) m = axl.Match((p2, p3), turns=3) self.assertEqual(m.play(), [(C, C), (C, C), (C, C)]) m = axl.Match((p1, p3), turns=3) self.assertEqual(m.play(), [(C, C), (D, C), (D, C)])
def tournament_score_gambler(pattern, turns, repetitions, opponents, params): """Calculates the score of a gambler in a tournament.""" parameters = Plays(self_plays=params[0], op_plays=params[1], op_openings=params[2]) size = get_lookup_table_size(params) initial_action = [ np.random.choice([C, D], p=[pattern[0], 1 - pattern[0]]) for _ in range(size) ] player = axl.Gambler( pattern=pattern[1:], parameters=parameters, initial_actions=initial_action, ) opponents = [axl.MemoryOnePlayer(q) for q in opponents] players = opponents + [player] number_of_players = len(players) edges = [(i, number_of_players - 1) for i in range(number_of_players - 1)] tournament = axl.Tournament(players=players, turns=turns, edges=edges, repetitions=repetitions) results = tournament.play(progress_bar=False) return -np.mean(results.normalised_scores[-1])
class TestGenericPlayerOne(unittest.TestCase): """A class to test the naming and classification of generic memory one players.""" p1 = axl.MemoryOnePlayer(four_vector=(0, 0, 0, 0)) p2 = axl.MemoryOnePlayer(four_vector=(1, 0, 1, 0)) p3 = axl.MemoryOnePlayer(four_vector=(1, 0.5, 1, 0.5)) def test_name(self): self.assertEqual(self.p1.name, "Generic Memory One Player: (0, 0, 0, 0)") self.assertEqual(self.p2.name, "Generic Memory One Player: (1, 0, 1, 0)") self.assertEqual(self.p3.name, "Generic Memory One Player: (1, 0.5, 1, 0.5)") def test_stochastic_classification(self): self.assertFalse(self.p1.classifier["stochastic"]) self.assertFalse(self.p2.classifier["stochastic"]) self.assertTrue(self.p3.classifier["stochastic"])
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)
class TestMemoryOneWSLS(TestWinStayLoseShift): """WinStayLoseShift is equivalent to MemoryOnePlayer((1, 0, 0, 1), C)""" name = "Generic Memory One Player: (1, 0, 0, 1), C" player = lambda x: axl.MemoryOnePlayer(four_vector=(1, 0, 0, 1)) expected_classifier = { "memory_depth": 1, "stochastic": False, "makes_use_of": set(), "long_run_time": False, "inspects_source": False, "manipulates_source": False, "manipulates_state": False, }
class TestMemoryOneDefector(TestDefector): """Defector is equivalent to MemoryOnePlayer((0, 0, 0, 0), D)""" name = "Generic Memory One Player: (0, 0, 0, 0), D" player = lambda x: axl.MemoryOnePlayer(four_vector=(0, 0, 0, 0), initial=D) expected_classifier = { "memory_depth": 0, "stochastic": False, "makes_use_of": set(), "long_run_time": False, "inspects_source": False, "manipulates_source": False, "manipulates_state": False, }
class TestMemoryOneCooperator(TestCooperator): """Cooperator is equivalent to MemoryOnePlayer((1, 1, 1, 1), C)""" name = "Generic Memory One Player: (1, 1, 1, 1), C" player = lambda x: axl.MemoryOnePlayer(four_vector=(1, 1, 1, 1)) expected_classifier = { "memory_depth": 0, "stochastic": False, "makes_use_of": set(), "long_run_time": False, "inspects_source": False, "manipulates_source": False, "manipulates_state": False, }
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 simulate_match_utility(player, opponent, turns=500, repetitions=200): """ Returns the simulated utility of a memory one player against a single opponent. """ total = 0 players = [axl.MemoryOnePlayer(vector) for vector in [player, opponent]] for rep in range(repetitions): match = axl.Match(players=players, turns=turns) _ = match.play() total += match.final_score_per_turn()[0] return total / repetitions
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)