|
5 | 5 | C, D = Actions.C, Actions.D
|
6 | 6 |
|
7 | 7 |
|
| 8 | +def player_count(interactions): |
| 9 | + """ |
| 10 | + The number of players derived from a dictionary of interactions |
| 11 | +
|
| 12 | + Parameters |
| 13 | + ---------- |
| 14 | + interactions : dictionary |
| 15 | + A dictionary of the form: |
| 16 | +
|
| 17 | + e.g. for a round robin between Cooperator, Defector and Alternator |
| 18 | + with 2 turns per round: |
| 19 | + { |
| 20 | + (0, 0): [(C, C), (C, C)]. |
| 21 | + (0, 1): [(C, D), (C, D)], |
| 22 | + (0, 2): [(C, C), (C, D)], |
| 23 | + (1, 1): [(D, D), (D, D)], |
| 24 | + (1, 2): [(D, C), (D, D)], |
| 25 | + (2, 2): [(C, C), (D, D)] |
| 26 | + } |
| 27 | +
|
| 28 | + i.e. the key is a pair of player index numbers and the value, a list of |
| 29 | + plays. The list contains one pair per turn in the round robin. |
| 30 | + The dictionary contains one entry for each combination of players. |
| 31 | +
|
| 32 | + Returns |
| 33 | + ------- |
| 34 | + nplayers : integer |
| 35 | + The number of players in the round robin |
| 36 | +
|
| 37 | + The number of ways (c) to select groups of r members from a set of n |
| 38 | + members is given by: |
| 39 | +
|
| 40 | + c = n! / r!(n - r)! |
| 41 | +
|
| 42 | + In this case, we are selecting pairs of players (p) and thus r = 2, |
| 43 | + giving: |
| 44 | +
|
| 45 | + p = n(n-1) / 2 or p = (n^2 - n) / 2 |
| 46 | +
|
| 47 | + However, we also have the case where each player plays itself gving: |
| 48 | +
|
| 49 | + p = (n^2 + n) / 2 |
| 50 | +
|
| 51 | + Using the quadratic equation to rearrange for n gives: |
| 52 | +
|
| 53 | + n = (-1 +- sqrt(1 + 8p)) / 2 |
| 54 | +
|
| 55 | + Taking only the real roots allows us to derive the number of players |
| 56 | + given the number of pairs: |
| 57 | +
|
| 58 | + n = (sqrt(8p + 1) -1) / 2 |
| 59 | + """ |
| 60 | + return int((math.sqrt(len(interactions) * 8 + 1) - 1) / 2) |
| 61 | + |
| 62 | + |
8 | 63 | # As yet unused until RoundRobin returns interactions
|
9 |
| -def payoff_matrix(interactions, nplayers, game): |
| 64 | +def payoff_matrix(interactions, game): |
10 | 65 | """
|
11 | 66 | The payoff matrix from a single round robin.
|
12 | 67 |
|
@@ -50,6 +105,7 @@ def payoff_matrix(interactions, nplayers, game):
|
50 | 105 | and column (j) represents an individual player and the the value Pij
|
51 | 106 | is the payoff value for player (i) versus player (j).
|
52 | 107 | """
|
| 108 | + nplayers = player_count(interactions) |
53 | 109 | payoffs = [[0 for i in range(nplayers)] for j in range(nplayers)]
|
54 | 110 | for players, actions in interactions.items():
|
55 | 111 | payoff = interaction_payoff(actions, game)
|
|
0 commit comments