Skip to content

Commit 6b83b04

Browse files
committed
Update tests for Adaptor, mostly
1 parent 3bae266 commit 6b83b04

File tree

6 files changed

+58
-25
lines changed

6 files changed

+58
-25
lines changed

axelrod/strategies/adaptor.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@ class AbstractAdaptor(Player):
3232
"manipulates_state": False,
3333
}
3434

35-
def __init__(self, d: Dict[Tuple[Action], float], perr: float = 0.01) -> None:
35+
def __init__(self, d: Dict[Tuple[Action, Action], float],
36+
perr: float = 0.01) -> None:
3637
super().__init__()
3738
self.perr = perr
3839
if not d:
39-
d = {(C, C): 1., # R
40-
(C, D): 1., # S
41-
(D, C): 1., # T
42-
(D, D): 1. # P
40+
d = {(C, C): 1., # R
41+
(C, D): 1., # S
42+
(D, C): 1., # T
43+
(D, D): 1. # P
4344
}
4445
self.d = d
4546
self.s = 0.
@@ -48,7 +49,7 @@ def strategy(self, opponent: Player) -> Action:
4849
if self.history:
4950
# Update internal state from the last play
5051
last_round = (self.history[-1], opponent.history[-1])
51-
self.s += d[last_round]
52+
self.s += self.d[last_round]
5253

5354
# Compute probability of Cooperation
5455
p = self.perr + (1.0 - 2 * self.perr) * (
@@ -58,7 +59,7 @@ def strategy(self, opponent: Player) -> Action:
5859
return action
5960

6061

61-
class AdaptorBrief(Player):
62+
class AdaptorBrief(AbstractAdaptor):
6263
"""
6364
An Adaptor trained on short interactions.
6465
@@ -71,15 +72,15 @@ class AdaptorBrief(Player):
7172
name = "AdaptorBrief"
7273

7374
def __init__(self) -> None:
74-
d = {(C, C): 0., # R
75-
(C, D): 1.001505, # S
76-
(D, C): 0.992107, # T
77-
(D, D): 0.638734 # P
75+
d = {(C, C): 0., # R
76+
(C, D): -1.001505, # S
77+
(D, C): 0.992107, # T
78+
(D, D): -0.638734 # P
7879
}
7980
super().__init__(d=d)
8081

8182

82-
class AdaptorLong(Player):
83+
class AdaptorLong(AbstractAdaptor):
8384
"""
8485
An Adaptor trained on long interactions.
8586
@@ -95,6 +96,6 @@ def __init__(self) -> None:
9596
d = {(C, C): 0., # R
9697
(C, D): 1.888159, # S
9798
(D, C): 1.858883, # T
98-
(D, D): 0.995703 # P
99+
(D, D): -0.995703 # P
99100
}
100101
super().__init__(d=d)

axelrod/strategies/bush_mosteller.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ def __init__(
5151
aspiration_level_divider: float, 3.0
5252
Value that regulates the aspiration level,
5353
isn't modified during match
54-
learning rate [0 , 1]
55-
Percentage of learning speed
54+
learning rate [0 , 1]
55+
Percentage of learning speed
5656
Variables / Constants
57-
_stimulus (Var: [-1 , 1]): float
57+
stimulus (Var: [-1 , 1]): float
5858
Value that impacts the changes of action probability
5959
_aspiration_level: float
6060
Value that impacts the stimulus changes, isn't modified during match
6161
_init_c_prob , _init_d_prob : float
62-
Values used to properly set up reset(),
62+
Values used to properly set up reset(),
6363
set to original probabilities
6464
"""
6565
super().__init__()

axelrod/tests/strategies/test_adaptor.py

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class TestAdaptorBrief(TestPlayer):
1515
name = "AdaptorBrief"
1616
player = axelrod.AdaptorBrief
1717
expected_classifier = {
18-
"memory_depth": 1,
18+
"memory_depth": float("inf"),
1919
"stochastic": True,
2020
"makes_use_of": set(),
2121
"inspects_source": False,
@@ -31,9 +31,27 @@ def test_strategy(self):
3131
)
3232

3333
# Error corrected.
34-
actions = [(C, C), (D, C), (C, D), (C, C)]
34+
actions = [(C, C), (C, D), (D, C), (C, C)]
3535
self.versus_test(
36-
opponent=axelrod.AdaptorBrief(), expected_actions=actions, seed=1
36+
opponent=axelrod.AdaptorBrief(), expected_actions=actions, seed=22
37+
)
38+
39+
# Error corrected, example 2
40+
actions = [(D, C), (C, D), (D, C), (C, D), (C, C)]
41+
self.versus_test(
42+
opponent=axelrod.AdaptorBrief(), expected_actions=actions, seed=925
43+
)
44+
45+
# Versus Cooperator
46+
actions = [(C, C)] * 8
47+
self.versus_test(
48+
opponent=axelrod.Cooperator(), expected_actions=actions, seed=0
49+
)
50+
51+
# Versus Defector
52+
actions = [(C, D), (D, D), (D, D), (D, D), (D, D), (D, D), (D, D)]
53+
self.versus_test(
54+
opponent=axelrod.Defector(), expected_actions=actions, seed=0
3755
)
3856

3957

@@ -42,7 +60,7 @@ class TestAdaptorLong(TestPlayer):
4260
name = "AdaptorLong"
4361
player = axelrod.AdaptorLong
4462
expected_classifier = {
45-
"memory_depth": 1,
63+
"memory_depth": float("inf"),
4664
"stochastic": True,
4765
"makes_use_of": set(),
4866
"inspects_source": False,
@@ -58,7 +76,19 @@ def test_strategy(self):
5876
)
5977

6078
# Error corrected.
61-
actions = [(C, C), (D, C), (D, D), (D, D), (C, C)]
79+
actions = [(C, C), (C, D), (D, D), (C, C), (C, C)]
80+
self.versus_test(
81+
opponent=axelrod.AdaptorLong(), expected_actions=actions, seed=22
82+
)
83+
84+
# Versus Cooperator
85+
actions = [(C, C)] * 8
86+
self.versus_test(
87+
opponent=axelrod.Cooperator(), expected_actions=actions, seed=0
88+
)
89+
90+
# Versus Defector
91+
actions = [(C, D), (D, D), (C, D), (D, D), (D, D), (C, D), (D, D)]
6292
self.versus_test(
63-
opponent=axelrod.AdaptorLong(), expected_actions=actions, seed=1
93+
opponent=axelrod.Defector(), expected_actions=actions, seed=0
6494
)

axelrod/tests/strategies/test_meta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ class TestNMWEStochastic(TestMetaPlayer):
548548
}
549549

550550
def test_strategy(self):
551-
actions = [(C, C), (C, D), (D, C), (D, D), (D, C)]
551+
actions = [(C, C), (C, D), (C, C), (D, D), (D, C)]
552552
self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions)
553553

554554

docs/reference/all_strategies.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Here are the docstrings of all the strategies in the library.
88

99
.. automodule:: axelrod.strategies.adaptive
1010
:members:
11+
.. automodule:: axelrod.strategies.adaptor
12+
:members:
1113
.. automodule:: axelrod.strategies.alternator
1214
:members:
1315
.. automodule:: axelrod.strategies.ann

docs/tutorials/advanced/classification_of_strategies.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ strategies::
4747
... }
4848
>>> strategies = axl.filtered_strategies(filterset)
4949
>>> len(strategies)
50-
82
50+
84
5151

5252
Or, to find out how many strategies only use 1 turn worth of memory to
5353
make a decision::

0 commit comments

Comments
 (0)