def cooperation_matrix(interactions): """ The cooperation matrix from a single round robin. Parameters ---------- interactions : dictionary A dictionary of the form: e.g. for a round robin between Cooperator, Defector and Alternator with 2 turns per round: { (0, 0): [(C, C), (C, C)]. (0, 1): [(C, D), (C, D)], (0, 2): [(C, C), (C, D)], (1, 1): [(D, D), (D, D)], (1, 2): [(D, C), (D, D)], (2, 2): [(C, C), (D, D)] } i.e. the key is a pair of player index numbers and the value, a list of plays. The list contains one pair per turn in the round robin. The dictionary contains one entry for each combination of players. nplayers : integer The number of players in the round robin Returns ------- list The cooperation matrix (C) of the form: [ [a, b, c], [d, e, f], [g, h, i], ] i.e. an n by n matrix where n is the number of players. Each row (i) and column (j) represents an individual player and the the value Cij is the number of times player i cooperated against opponent j. """ nplayers = player_count(interactions) cooperation = [[0 for i in range(nplayers)] for j in range(nplayers)] for players, actions in interactions.items(): p1_actions, p2_actions = zip(*actions) p1_cooperation = p1_actions.count(C) p2_cooperation = p2_actions.count(C) cooperation[players[0]][players[1]] = p1_cooperation if players[0] != players[1]: cooperation[players[1]][players[0]] = p2_cooperation return cooperation
def test_player_count(self): nplayers = ap.player_count(self.interactions) self.assertEqual(nplayers, 3) nplayers = ap.player_count({"test": "test"}) self.assertEqual(nplayers, 1)
def test_player_count(self): nplayers = ap.player_count(self.interactions) self.assertEqual(nplayers, 3) nplayers = ap.player_count({'test': 'test'}) self.assertEqual(nplayers, 1)