Fishing villages share a bay. Each season, two villages that share a stretch of water each either keep to the agreed catch (C, cooperate) or overfish (D, defect), both at the same time. With payoff = (T, R, P, S) and T > R > P > S, the season pays
| other keeps to it (C) | other overfishes (D) | |
|---|---|---|
| keeps to it (C) | R each |
S to you, T to the other |
| overfishes (D) | T to you, S to the other |
P each |
so whatever the other does, overfishing pays more this season: a prisoners' dilemma. But the same two villages meet again, rounds seasons in a row, and each village follows a fixed strategy, a rule that decides this season's move from the moves made in earlier seasons. The strategies, by name:
"cooperator": always C."defector": always D."alternator": C in season 1, D in season 2, C in season 3, and so on."tit-for-tat": C in season 1, then whatever the other played in the previous season."suspicious": D in season 1, then whatever the other played in the previous season."grim": C until the other has played D at least once; from the next season on, D forever."forgiving": D only when the other played D in both of the two previous seasons; otherwise C (so C in seasons 1 and 2)."pavlov": C in season 1; then C if both villages made the same move in the previous season, D if they made different moves.
In a round-robin tournament, entries lists the strategy of each village (the same name may appear several times; each is a separate village). Every pair of different villages plays one match of rounds seasons, starting with no history; a village does not play itself. A village's score is the sum of its payoffs over all its matches.
Write tournament(entries, rounds, payoff) that returns a tuple (scores, winners): the list of scores (in the order of entries) and the list of indices of the villages with the highest score, in increasing order.
Examples
Input: entries = ["tit-for-tat", "defector", "cooperator"], rounds = 5, payoff = (6, 4, 1, 0)
Output: ([24, 40, 20], [1])
Explanation: tit-for-tat loses season 1 to the defector (0 against 6), then both overfish:
4 seasons at 1 each. Against the cooperator it earns 5 * 4 = 20. The defector earns
6 + 4 * 1 = 10 against tit-for-tat and 5 * 6 = 30 against the cooperator.
Input: entries = ["tit-for-tat", "tit-for-tat", "defector", "defector"], rounds = 10, payoff = (6, 4, 1, 0)
Output: ([58, 58, 40, 40], [0, 1])
Input: entries = ["tit-for-tat", "grim", "defector", "pavlov", "forgiving", "suspicious",
"alternator", "cooperator"], rounds = 20, payoff = (6, 4, 1, 0)
Output: ([457, 430, 360, 433, 454, 376, 408, 436], [0])
Goals
- Implement strategies of a repeated game as rules that read the history of play
- Run a round-robin tournament and total each entry's payoffs
- See how strategies that reward cooperation and punish defection fare against each other