Skip to content

Commit

Permalink
Update tests for Adaptor, mostly
Browse files Browse the repository at this point in the history
  • Loading branch information
marcharper committed Oct 30, 2018
1 parent 6f9bb57 commit cb51977
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 25 deletions.
27 changes: 14 additions & 13 deletions axelrod/strategies/adaptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ class AbstractAdaptor(Player):
"manipulates_state": False,
}

def __init__(self, d: Dict[Tuple[Action], float], perr: float = 0.01) -> None:
def __init__(self, d: Dict[Tuple[Action, Action], float],
perr: float = 0.01) -> None:
super().__init__()
self.perr = perr
if not d:
d = {(C, C): 1., # R
(C, D): 1., # S
(D, C): 1., # T
(D, D): 1. # P
d = {(C, C): 1., # R
(C, D): 1., # S
(D, C): 1., # T
(D, D): 1. # P
}
self.d = d
self.s = 0.
Expand All @@ -48,7 +49,7 @@ def strategy(self, opponent: Player) -> Action:
if self.history:
# Update internal state from the last play
last_round = (self.history[-1], opponent.history[-1])
self.s += d[last_round]
self.s += self.d[last_round]

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


class AdaptorBrief(Player):
class AdaptorBrief(AbstractAdaptor):
"""
An Adaptor trained on short interactions.
Expand All @@ -71,15 +72,15 @@ class AdaptorBrief(Player):
name = "AdaptorBrief"

def __init__(self) -> None:
d = {(C, C): 0., # R
(C, D): 1.001505, # S
(D, C): 0.992107, # T
(D, D): 0.638734 # P
d = {(C, C): 0., # R
(C, D): -1.001505, # S
(D, C): 0.992107, # T
(D, D): -0.638734 # P
}
super().__init__(d=d)


class AdaptorLong(Player):
class AdaptorLong(AbstractAdaptor):
"""
An Adaptor trained on long interactions.
Expand All @@ -95,6 +96,6 @@ def __init__(self) -> None:
d = {(C, C): 0., # R
(C, D): 1.888159, # S
(D, C): 1.858883, # T
(D, D): 0.995703 # P
(D, D): -0.995703 # P
}
super().__init__(d=d)
8 changes: 4 additions & 4 deletions axelrod/strategies/bush_mosteller.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ def __init__(
aspiration_level_divider: float, 3.0
Value that regulates the aspiration level,
isn't modified during match
learning rate [0 , 1]
Percentage of learning speed
learning rate [0 , 1]
Percentage of learning speed
Variables / Constants
_stimulus (Var: [-1 , 1]): float
stimulus (Var: [-1 , 1]): float
Value that impacts the changes of action probability
_aspiration_level: float
Value that impacts the stimulus changes, isn't modified during match
_init_c_prob , _init_d_prob : float
Values used to properly set up reset(),
Values used to properly set up reset(),
set to original probabilities
"""
super().__init__()
Expand Down
42 changes: 36 additions & 6 deletions axelrod/tests/strategies/test_adaptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class TestAdaptorBrief(TestPlayer):
name = "AdaptorBrief"
player = axelrod.AdaptorBrief
expected_classifier = {
"memory_depth": 1,
"memory_depth": float("inf"),
"stochastic": True,
"makes_use_of": set(),
"inspects_source": False,
Expand All @@ -31,9 +31,27 @@ def test_strategy(self):
)

# Error corrected.
actions = [(C, C), (D, C), (C, D), (C, C)]
actions = [(C, C), (C, D), (D, C), (C, C)]
self.versus_test(
opponent=axelrod.AdaptorBrief(), expected_actions=actions, seed=1
opponent=axelrod.AdaptorBrief(), expected_actions=actions, seed=22
)

# Error corrected, example 2
actions = [(D, C), (C, D), (D, C), (C, D), (C, C)]
self.versus_test(
opponent=axelrod.AdaptorBrief(), expected_actions=actions, seed=925
)

# Versus Cooperator
actions = [(C, C)] * 8
self.versus_test(
opponent=axelrod.Cooperator(), expected_actions=actions, seed=0
)

# Versus Defector
actions = [(C, D), (D, D), (D, D), (D, D), (D, D), (D, D), (D, D)]
self.versus_test(
opponent=axelrod.Defector(), expected_actions=actions, seed=0
)


Expand All @@ -42,7 +60,7 @@ class TestAdaptorLong(TestPlayer):
name = "AdaptorLong"
player = axelrod.AdaptorLong
expected_classifier = {
"memory_depth": 1,
"memory_depth": float("inf"),
"stochastic": True,
"makes_use_of": set(),
"inspects_source": False,
Expand All @@ -58,7 +76,19 @@ def test_strategy(self):
)

# Error corrected.
actions = [(C, C), (D, C), (D, D), (D, D), (C, C)]
actions = [(C, C), (C, D), (D, D), (C, C), (C, C)]
self.versus_test(
opponent=axelrod.AdaptorLong(), expected_actions=actions, seed=22
)

# Versus Cooperator
actions = [(C, C)] * 8
self.versus_test(
opponent=axelrod.Cooperator(), expected_actions=actions, seed=0
)

# Versus Defector
actions = [(C, D), (D, D), (C, D), (D, D), (D, D), (C, D), (D, D)]
self.versus_test(
opponent=axelrod.AdaptorLong(), expected_actions=actions, seed=1
opponent=axelrod.Defector(), expected_actions=actions, seed=0
)
2 changes: 1 addition & 1 deletion axelrod/tests/strategies/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ class TestNMWEStochastic(TestMetaPlayer):
}

def test_strategy(self):
actions = [(C, C), (C, D), (D, C), (D, D), (D, C)]
actions = [(C, C), (C, D), (C, C), (D, D), (D, C)]
self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions)


Expand Down
2 changes: 2 additions & 0 deletions docs/reference/all_strategies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Here are the docstrings of all the strategies in the library.

.. automodule:: axelrod.strategies.adaptive
:members:
.. automodule:: axelrod.strategies.adaptor
:members:
.. automodule:: axelrod.strategies.alternator
:members:
.. automodule:: axelrod.strategies.ann
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorials/advanced/classification_of_strategies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ strategies::
... }
>>> strategies = axl.filtered_strategies(filterset)
>>> len(strategies)
82
84

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

0 comments on commit cb51977

Please sign in to comment.