Skip to content

Commit d706204

Browse files
committed
fixed freq analyzer test
1 parent 3b0ac2c commit d706204

File tree

5 files changed

+63
-15
lines changed

5 files changed

+63
-15
lines changed

axelrod/strategies/_strategies.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@
238238
)
239239
from .shortmem import ShortMem
240240
from .stalker import Stalker
241-
from .frequency_analyzer import FreqAnalyzer
241+
from .frequency_analyzer import FrequencyAnalyzer
242242
from .titfortat import (
243243
AdaptiveTitForTat,
244244
Alexei,
@@ -367,7 +367,7 @@
367367
ForgivingTitForTat,
368368
Fortress3,
369369
Fortress4,
370-
FreqAnalyzer,
370+
FrequencyAnalyzer,
371371
GTFT,
372372
GeneralSoftGrudger,
373373
GoByMajority,

axelrod/strategies/frequency_analyzer.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

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

10-
class FreqAnalyzer(Player):
10+
class FrequencyAnalyzer(Player):
1111
"""
1212
A player starts by playing TitForTat for the first 30 turns (dataset generation phase).
1313
@@ -38,11 +38,11 @@ class FreqAnalyzer(Player):
3838
3939
Names:
4040
41-
- FreqAnalyzer (FREQ): Original by Ian Miller
41+
- FrequencyAnalyzer (FREQ): Original by Ian Miller
4242
"""
4343

4444
# These are various properties for the strategy
45-
name = "FreqAnalyzer"
45+
name = "FrequencyAnalyzer"
4646
classifier = {
4747
"memory_depth": float("inf"),
4848
"stochastic": False,
@@ -59,7 +59,7 @@ def __init__(self) -> None:
5959
The probability to cooperate
6060
"""
6161
super().__init__()
62-
self.minimum_cooperation_ratio = 0.5
62+
self.minimum_cooperation_ratio = 0.8
6363
self.frequency_table = dict()
6464
self.last_sequence = ''
6565
self.current_sequence = ''
@@ -69,7 +69,6 @@ def strategy(self, opponent: Player) -> Action:
6969
if len(self.history) > 5:
7070
self.last_sequence = str(opponent.history[-3]) + str(self.history[-3]) + str(opponent.history[-2]) + str(self.history[-2])
7171
self.current_sequence = str(opponent.history[-2]) + str(self.history[-2]) + str(opponent.history[-1]) + str(self.history[-1])
72-
7372
self.update_table(opponent)
7473

7574
if len(self.history) < 30:
@@ -97,16 +96,10 @@ def strategy(self, opponent: Player) -> Action:
9796
return C
9897

9998
def update_table(self, opponent: Player):
100-
print(self.frequency_table)
101-
print("___________________")
102-
print("current sequence is {}", self.last_sequence)
10399
if self.last_sequence in self.frequency_table.keys():
104-
print("seen this key before")
105-
print("freq table keys = {}", self.frequency_table.keys())
106100
results = self.frequency_table[self.last_sequence]
107101
results.append(opponent.history[-1])
108102
self.frequency_table[self.last_sequence] = results
109103
else:
110-
print("not seen this key ever")
111104
self.frequency_table[self.last_sequence] = [opponent.history[-1]]
112105

axelrod/strategies/titfortat.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -950,5 +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
954-
953+
return D
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""Tests for the FrequencyAnalyzer strategy."""
2+
3+
import axelrod as axl
4+
5+
from .test_player import TestPlayer
6+
7+
C, D = axl.Action.C, axl.Action.D
8+
9+
10+
class Test(TestPlayer):
11+
12+
name = "FrequencyAnalyzer"
13+
player = axl.FrequencyAnalyzer
14+
expected_classifier = {
15+
"memory_depth": float("inf"),
16+
"stochastic": False,
17+
"long_run_time": False,
18+
"makes_use_of": set(),
19+
"inspects_source": False,
20+
"manipulates_source": False,
21+
"manipulates_state": False,
22+
}
23+
24+
def test_strategy_early(self):
25+
# Test games that end while still in dataset generation phase (<30 turns)
26+
opponent_actions = [C, C, D, C, D]
27+
expected = [(C, C), (C, C), (C, D), (D, C), (C, D)]
28+
self.versus_test(
29+
axl.MockPlayer(opponent_actions), expected_actions=expected, seed=4
30+
)
31+
32+
def test_strategy_defector(self):
33+
# Test against all defections
34+
opponent_actions = [D] * 30
35+
expected = [(C, D)] + [(D, D)] * 29
36+
self.versus_test(
37+
axl.MockPlayer(opponent_actions), expected_actions=expected, seed=4
38+
)
39+
40+
def test_strategy_cooperator(self):
41+
# Test games that end while still in dataset generation phase (<30 turns)
42+
opponent_actions = [C] * 30
43+
expected = [(C, C)] * 30
44+
self.versus_test(
45+
axl.MockPlayer(opponent_actions), expected_actions=expected, seed=4
46+
)
47+
48+
def test_strategy_random(self):
49+
# Test of 50 turns against random strategy
50+
opponent_actions = [C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, D, C, C, C, C, D, D, C, C, C, D, D, D, C, C, D, D, D, D]
51+
expected = [(C, C), (C, C), (C, C), (C, C), (C, C), (C, C), (C, C), (C, C), (C, C), (C, C), (C, C), (C, C), (C, C), (C, C), (C, C), (C, C), (C, C), (C, C), (C, C), (C, C), (C, C), (C, C), (C, C), (C, C), (C, C), (C, C), (C, C), (C, C), (C, C), (C, C), (D, C), (C, D), (D, C), (C, C), (C, C), (D, C), (D, D), (D, D), (D, C), (C, C), (D, C), (D, D), (D, D), (D, D), (D, C), (D, C), (C, D), (D, D), (D, D), (D, D)]
52+
self.versus_test(
53+
axl.MockPlayer(opponent_actions), expected_actions=expected, seed=4
54+
)

docs/reference/strategy_index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ Here are the docstrings of all the strategies in the library.
4848
:members:
4949
.. automodule:: axelrod.strategies.forgiver
5050
:members:
51+
.. automodule:: axelrod.strategies.frequencyanalyzer
52+
:members:
5153
.. automodule:: axelrod.strategies.gambler
5254
:members:
5355
.. automodule:: axelrod.strategies.gobymajority

0 commit comments

Comments
 (0)