|
| 1 | +from axelrod.action import Action, actions_to_str |
| 2 | +from axelrod.player import Player |
| 3 | +from axelrod.strategy_transformers import ( |
| 4 | + FinalTransformer, |
| 5 | + TrackHistoryTransformer, |
| 6 | +) |
| 7 | + |
| 8 | +C, D = Action.C, Action.D |
| 9 | + |
| 10 | + |
| 11 | +class FrequencyAnalyzer(Player): |
| 12 | + """ |
| 13 | + A player starts by playing TitForTat for the first 30 turns (dataset generation phase). |
| 14 | +
|
| 15 | + Take the matrix of last 2 moves by both Player and Opponent. |
| 16 | +
|
| 17 | + While in dataset generation phase, construct a dictionary d, where keys are each 4 move sequence |
| 18 | + and the corresponding value for each key is a list of the subsequent Opponent move. The 4 move sequence |
| 19 | + starts with the Opponent move. |
| 20 | +
|
| 21 | + For example, if a game at turn 5 looks like this: |
| 22 | +
|
| 23 | + Opp: C, C, D, C, D |
| 24 | + Player: C, C, C, D, C |
| 25 | +
|
| 26 | + d should look like this: |
| 27 | +
|
| 28 | + { [CCCC]: [D], |
| 29 | + [CCDC]: [C], |
| 30 | + [DCCD]: [D] } |
| 31 | +
|
| 32 | + During dataset generation phase, Player will play TitForTat. After end of dataset generation phase, |
| 33 | + Player will switch strategies. Upon encountering a particular 4-move sequence in the game, Player will look up history |
| 34 | + of subsequent Opponent move. If ratio of defections to total moves exceeds p, Player will defect. Otherwise, |
| 35 | + Player will cooperate. |
| 36 | +
|
| 37 | + Could fall under "Hunter" class of strategies. |
| 38 | + More likely falls under LookerUp class of strategies. |
| 39 | +
|
| 40 | + Names: |
| 41 | +
|
| 42 | + - FrequencyAnalyzer (FREQ): Original by Ian Miller |
| 43 | + """ |
| 44 | + |
| 45 | + # These are various properties for the strategy |
| 46 | + name = "FrequencyAnalyzer" |
| 47 | + classifier = { |
| 48 | + "memory_depth": float("inf"), |
| 49 | + "stochastic": False, |
| 50 | + "long_run_time": False, |
| 51 | + "inspects_source": False, |
| 52 | + "manipulates_source": False, |
| 53 | + "manipulates_state": False, |
| 54 | + } |
| 55 | + |
| 56 | + def __init__(self) -> None: |
| 57 | + """ |
| 58 | + Parameters |
| 59 | + ---------- |
| 60 | + p, float |
| 61 | + The probability to cooperate |
| 62 | + """ |
| 63 | + super().__init__() |
| 64 | + self.minimum_cooperation_ratio = 0.25 |
| 65 | + self.frequency_table = dict() |
| 66 | + self.last_sequence = "" |
| 67 | + self.current_sequence = "" |
| 68 | + |
| 69 | + def strategy(self, opponent: Player) -> Action: |
| 70 | + """This is the actual strategy""" |
| 71 | + if len(self.history) > 5: |
| 72 | + self.last_sequence = ( |
| 73 | + str(opponent.history[-3]) |
| 74 | + + str(self.history[-3]) |
| 75 | + + str(opponent.history[-2]) |
| 76 | + + str(self.history[-2]) |
| 77 | + ) |
| 78 | + self.current_sequence = ( |
| 79 | + str(opponent.history[-2]) |
| 80 | + + str(self.history[-2]) |
| 81 | + + str(opponent.history[-1]) |
| 82 | + + str(self.history[-1]) |
| 83 | + ) |
| 84 | + self.update_table(opponent) |
| 85 | + |
| 86 | + # dataset generation phase |
| 87 | + if (len(self.history) < 30) or ( |
| 88 | + self.current_sequence not in self.frequency_table |
| 89 | + ): |
| 90 | + if not self.history: |
| 91 | + return C |
| 92 | + if opponent.history[-1] == D: |
| 93 | + return D |
| 94 | + return C |
| 95 | + |
| 96 | + # post-dataset generation phase |
| 97 | + results = self.frequency_table[self.current_sequence] |
| 98 | + cooperates = results.count(C) |
| 99 | + if (cooperates / len(self.history)) > self.minimum_cooperation_ratio: |
| 100 | + return C |
| 101 | + return D |
| 102 | + |
| 103 | + def update_table(self, opponent: Player): |
| 104 | + if self.last_sequence in self.frequency_table.keys(): |
| 105 | + results = self.frequency_table[self.last_sequence] |
| 106 | + results.append(opponent.history[-1]) |
| 107 | + self.frequency_table[self.last_sequence] = results |
| 108 | + else: |
| 109 | + self.frequency_table[self.last_sequence] = [opponent.history[-1]] |
0 commit comments