Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
miller-ian committed Jan 8, 2025
1 parent 40e867e commit 8384c36
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 61 deletions.
25 changes: 18 additions & 7 deletions axelrod/strategies/frequency_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

C, D = Action.C, Action.D


class FrequencyAnalyzer(Player):
"""
A player starts by playing TitForTat for the first 30 turns (dataset generation phase).
A player starts by playing TitForTat for the first 30 turns (dataset generation phase).
Take the matrix of last 2 moves by both Player and Opponent.
Expand All @@ -30,7 +31,7 @@ class FrequencyAnalyzer(Player):
During dataset generation phase, Player will play TitForTat. After end of dataset generation phase,
Player will switch strategies. Upon encountering a particular 4-move sequence in the game, Player will look up history
of subsequent Opponent move. If ratio of defections to total moves exceeds p, Player will defect. Otherwise,
of subsequent Opponent move. If ratio of defections to total moves exceeds p, Player will defect. Otherwise,
Player will cooperate.
Could fall under "Hunter" class of strategies.
Expand All @@ -51,6 +52,7 @@ class FrequencyAnalyzer(Player):
"manipulates_source": False,
"manipulates_state": False,
}

def __init__(self) -> None:
"""
Parameters
Expand All @@ -61,14 +63,24 @@ def __init__(self) -> None:
super().__init__()
self.minimum_cooperation_ratio = 0.25
self.frequency_table = dict()
self.last_sequence = ''
self.current_sequence = ''
self.last_sequence = ""
self.current_sequence = ""

def strategy(self, opponent: Player) -> Action:
"""This is the actual strategy"""
if len(self.history) > 5:
self.last_sequence = str(opponent.history[-3]) + str(self.history[-3]) + str(opponent.history[-2]) + str(self.history[-2])
self.current_sequence = str(opponent.history[-2]) + str(self.history[-2]) + str(opponent.history[-1]) + str(self.history[-1])
self.last_sequence = (
str(opponent.history[-3])
+ str(self.history[-3])
+ str(opponent.history[-2])
+ str(self.history[-2])
)
self.current_sequence = (
str(opponent.history[-2])
+ str(self.history[-2])
+ str(opponent.history[-1])
+ str(self.history[-1])
)
self.update_table(opponent)

if len(self.history) < 30:
Expand Down Expand Up @@ -100,4 +112,3 @@ def update_table(self, opponent: Player):
self.frequency_table[self.last_sequence] = results
else:
self.frequency_table[self.last_sequence] = [opponent.history[-1]]

2 changes: 1 addition & 1 deletion axelrod/strategies/titfortat.py
Original file line number Diff line number Diff line change
Expand Up @@ -950,4 +950,4 @@ def strategy(self, opponent):
# Cooperate with 0.9
return self._random.random_choice(0.9)
# Else TFT. Opponent played D, so play D in return.
return D
return D
162 changes: 109 additions & 53 deletions axelrod/tests/strategies/test_frequency_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,60 +44,116 @@ def test_strategy_cooperator(self):
self.versus_test(
axl.MockPlayer(opponent_actions), expected_actions=expected, seed=4
)

def test_strategy_random(self):
# Test of 50 turns against random strategy
opponent_actions = [C, D, D, D, D, D, D, C, D, C, D, C, D, C, D, D, C, D, C, D, D, C, D, D, D, D, D, C, C, D, D, C, C, C, D, D, C, D, C, C, C, D, D, C, C, C, D, C, D, D]
expected = [(C, C),
(C, D),
(D, D),
(D, D),
(D, D),
(D, D),
(D, D),
(D, C),
(C, D),
(D, C),
(C, D),
(D, C),
(C, D),
(D, C),
(C, D),
(D, D),
(D, C),
(C, D),
(D, C),
(C, D),
(D, D),
(D, C),
(C, D),
(D, D),
(D, D),
(D, D),
(D, D),
(D, C),
(C, C),
(C, D), #rd 30 (end of dataset generation phase)
(D, D),
(D, C),
(D, C), #example of non TFT (by this point, FrequencyAnalyzer is generally distrustful of opponent)
(C, C),
(D, D),
(D, D),
(D, C),
(D, D),
(D, C),
(D, C),
(D, C),
(D, D),
(D, D),
(D, C),
(D, C),
(D, C),
(D, D),
(D, C),
(D, D),
(D, D)]
opponent_actions = [
C,
D,
D,
D,
D,
D,
D,
C,
D,
C,
D,
C,
D,
C,
D,
D,
C,
D,
C,
D,
D,
C,
D,
D,
D,
D,
D,
C,
C,
D,
D,
C,
C,
C,
D,
D,
C,
D,
C,
C,
C,
D,
D,
C,
C,
C,
D,
C,
D,
D,
]
expected = [
(C, C),
(C, D),
(D, D),
(D, D),
(D, D),
(D, D),
(D, D),
(D, C),
(C, D),
(D, C),
(C, D),
(D, C),
(C, D),
(D, C),
(C, D),
(D, D),
(D, C),
(C, D),
(D, C),
(C, D),
(D, D),
(D, C),
(C, D),
(D, D),
(D, D),
(D, D),
(D, D),
(D, C),
(C, C),
(C, D), # rd 30 (end of dataset generation phase)
(D, D),
(D, C),
(
D,
C,
), # example of non TFT (by this point, FrequencyAnalyzer is generally distrustful of opponent)
(C, C),
(D, D),
(D, D),
(D, C),
(D, D),
(D, C),
(D, C),
(D, C),
(D, D),
(D, D),
(D, C),
(D, C),
(D, C),
(D, D),
(D, C),
(D, D),
(D, D),
]
self.versus_test(
axl.MockPlayer(opponent_actions), expected_actions=expected, seed=4
)
)

0 comments on commit 8384c36

Please sign in to comment.