Skip to content

Commit 8384c36

Browse files
committed
formatting
1 parent 40e867e commit 8384c36

File tree

3 files changed

+128
-61
lines changed

3 files changed

+128
-61
lines changed

axelrod/strategies/frequency_analyzer.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77

88
C, D = Action.C, Action.D
99

10+
1011
class FrequencyAnalyzer(Player):
1112
"""
12-
A player starts by playing TitForTat for the first 30 turns (dataset generation phase).
13+
A player starts by playing TitForTat for the first 30 turns (dataset generation phase).
1314
1415
Take the matrix of last 2 moves by both Player and Opponent.
1516
@@ -30,7 +31,7 @@ class FrequencyAnalyzer(Player):
3031
3132
During dataset generation phase, Player will play TitForTat. After end of dataset generation phase,
3233
Player will switch strategies. Upon encountering a particular 4-move sequence in the game, Player will look up history
33-
of subsequent Opponent move. If ratio of defections to total moves exceeds p, Player will defect. Otherwise,
34+
of subsequent Opponent move. If ratio of defections to total moves exceeds p, Player will defect. Otherwise,
3435
Player will cooperate.
3536
3637
Could fall under "Hunter" class of strategies.
@@ -51,6 +52,7 @@ class FrequencyAnalyzer(Player):
5152
"manipulates_source": False,
5253
"manipulates_state": False,
5354
}
55+
5456
def __init__(self) -> None:
5557
"""
5658
Parameters
@@ -61,14 +63,24 @@ def __init__(self) -> None:
6163
super().__init__()
6264
self.minimum_cooperation_ratio = 0.25
6365
self.frequency_table = dict()
64-
self.last_sequence = ''
65-
self.current_sequence = ''
66+
self.last_sequence = ""
67+
self.current_sequence = ""
6668

6769
def strategy(self, opponent: Player) -> Action:
6870
"""This is the actual strategy"""
6971
if len(self.history) > 5:
70-
self.last_sequence = str(opponent.history[-3]) + str(self.history[-3]) + str(opponent.history[-2]) + str(self.history[-2])
71-
self.current_sequence = str(opponent.history[-2]) + str(self.history[-2]) + str(opponent.history[-1]) + str(self.history[-1])
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+
)
7284
self.update_table(opponent)
7385

7486
if len(self.history) < 30:
@@ -100,4 +112,3 @@ def update_table(self, opponent: Player):
100112
self.frequency_table[self.last_sequence] = results
101113
else:
102114
self.frequency_table[self.last_sequence] = [opponent.history[-1]]
103-

axelrod/strategies/titfortat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -950,4 +950,4 @@ def strategy(self, opponent):
950950
# Cooperate with 0.9
951951
return self._random.random_choice(0.9)
952952
# Else TFT. Opponent played D, so play D in return.
953-
return D
953+
return D

axelrod/tests/strategies/test_frequency_analyzer.py

Lines changed: 109 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -44,60 +44,116 @@ def test_strategy_cooperator(self):
4444
self.versus_test(
4545
axl.MockPlayer(opponent_actions), expected_actions=expected, seed=4
4646
)
47-
47+
4848
def test_strategy_random(self):
4949
# Test of 50 turns against random strategy
50-
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]
51-
expected = [(C, C),
52-
(C, D),
53-
(D, D),
54-
(D, D),
55-
(D, D),
56-
(D, D),
57-
(D, D),
58-
(D, C),
59-
(C, D),
60-
(D, C),
61-
(C, D),
62-
(D, C),
63-
(C, D),
64-
(D, C),
65-
(C, D),
66-
(D, D),
67-
(D, C),
68-
(C, D),
69-
(D, C),
70-
(C, D),
71-
(D, D),
72-
(D, C),
73-
(C, D),
74-
(D, D),
75-
(D, D),
76-
(D, D),
77-
(D, D),
78-
(D, C),
79-
(C, C),
80-
(C, D), #rd 30 (end of dataset generation phase)
81-
(D, D),
82-
(D, C),
83-
(D, C), #example of non TFT (by this point, FrequencyAnalyzer is generally distrustful of opponent)
84-
(C, C),
85-
(D, D),
86-
(D, D),
87-
(D, C),
88-
(D, D),
89-
(D, C),
90-
(D, C),
91-
(D, C),
92-
(D, D),
93-
(D, D),
94-
(D, C),
95-
(D, C),
96-
(D, C),
97-
(D, D),
98-
(D, C),
99-
(D, D),
100-
(D, D)]
50+
opponent_actions = [
51+
C,
52+
D,
53+
D,
54+
D,
55+
D,
56+
D,
57+
D,
58+
C,
59+
D,
60+
C,
61+
D,
62+
C,
63+
D,
64+
C,
65+
D,
66+
D,
67+
C,
68+
D,
69+
C,
70+
D,
71+
D,
72+
C,
73+
D,
74+
D,
75+
D,
76+
D,
77+
D,
78+
C,
79+
C,
80+
D,
81+
D,
82+
C,
83+
C,
84+
C,
85+
D,
86+
D,
87+
C,
88+
D,
89+
C,
90+
C,
91+
C,
92+
D,
93+
D,
94+
C,
95+
C,
96+
C,
97+
D,
98+
C,
99+
D,
100+
D,
101+
]
102+
expected = [
103+
(C, C),
104+
(C, D),
105+
(D, D),
106+
(D, D),
107+
(D, D),
108+
(D, D),
109+
(D, D),
110+
(D, C),
111+
(C, D),
112+
(D, C),
113+
(C, D),
114+
(D, C),
115+
(C, D),
116+
(D, C),
117+
(C, D),
118+
(D, D),
119+
(D, C),
120+
(C, D),
121+
(D, C),
122+
(C, D),
123+
(D, D),
124+
(D, C),
125+
(C, D),
126+
(D, D),
127+
(D, D),
128+
(D, D),
129+
(D, D),
130+
(D, C),
131+
(C, C),
132+
(C, D), # rd 30 (end of dataset generation phase)
133+
(D, D),
134+
(D, C),
135+
(
136+
D,
137+
C,
138+
), # example of non TFT (by this point, FrequencyAnalyzer is generally distrustful of opponent)
139+
(C, C),
140+
(D, D),
141+
(D, D),
142+
(D, C),
143+
(D, D),
144+
(D, C),
145+
(D, C),
146+
(D, C),
147+
(D, D),
148+
(D, D),
149+
(D, C),
150+
(D, C),
151+
(D, C),
152+
(D, D),
153+
(D, C),
154+
(D, D),
155+
(D, D),
156+
]
101157
self.versus_test(
102158
axl.MockPlayer(opponent_actions), expected_actions=expected, seed=4
103-
)
159+
)

0 commit comments

Comments
 (0)