Skip to content

Commit a643480

Browse files
committed
Implemented WmAdams strategy (k44r from Axelrod's second)
1 parent d5c8f77 commit a643480

File tree

5 files changed

+77
-3
lines changed

5 files changed

+77
-3
lines changed

axelrod/strategies/_strategies.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
UnnamedStrategy, SteinAndRapoport, TidemanAndChieruzzi)
1010
from .axelrod_second import (
1111
Champion, Eatherley, Tester, Gladstein, Tranquilizer, MoreGrofman,
12-
Kluepfel, Borufsen, Cave)
12+
Kluepfel, Borufsen, Cave, WmAdams)
1313
from .backstabber import BackStabber, DoubleCrosser
1414
from .better_and_better import BetterAndBetter
1515
from .bush_mosteller import BushMosteller
@@ -274,6 +274,7 @@
274274
Winner21,
275275
WinShiftLoseStay,
276276
WinStayLoseShift,
277+
WmAdams,
277278
WorseAndWorse,
278279
WorseAndWorse2,
279280
WorseAndWorse3,

axelrod/strategies/axelrod_second.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,3 +771,40 @@ def strategy(self, opponent: Player) -> Action:
771771
return random_choice(0.5)
772772
else:
773773
return C
774+
775+
class WmAdams(Player):
776+
"""
777+
Strategy submitted to Axelrod's second tournament by William Adams (K49R),
778+
and came in fifth in that tournament.
779+
780+
Count the number of opponent defects after their first move, call
781+
`c_defect`. Defect if c_defect equals 4, 7, or 9. If c_defect > 9,
782+
then defect immediately after opponent defect with probability =
783+
(0.5)^(c_defect-1). Otherwise cooperate.
784+
785+
Names:
786+
787+
- WmAdams: [Axelrod1980b]_
788+
"""
789+
790+
name = "WmAdams"
791+
classifier = {
792+
'memory_depth': float('inf'),
793+
'stochastic': True,
794+
'makes_use_of': set(),
795+
'long_run_time': False,
796+
'inspects_source': False,
797+
'manipulates_source': False,
798+
'manipulates_state': False
799+
}
800+
801+
def strategy(self, opponent: Player) -> Action:
802+
if len(self.history) <=1:
803+
return C
804+
did_d = np.vectorize(lambda action: float(action == D))
805+
number_defects = np.sum(did_d(opponent.history[1:]))
806+
807+
if number_defects == 4 or number_defects == 7 or number_defects == 9: return D
808+
if number_defects > 9 and opponent.history[-1] == D:
809+
return random_choice((0.5)**(number_defects-9))
810+
return C

axelrod/tests/strategies/test_axelrod_second.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,3 +542,39 @@ def test_strategy(self):
542542
actions += [(D, C), (C, D), (D, C)] # 17 D have come, so tit for tat for a while
543543
actions += [(D, D), (D, C)] * 100 # Random finally detected
544544
self.versus_test(axelrod.Alternator(), expected_actions=actions)
545+
546+
class TestCave(TestPlayer):
547+
name = "WmAdams"
548+
player = axelrod.WmAdams
549+
expected_classifier = {
550+
'memory_depth': float('inf'),
551+
'stochastic': True,
552+
'makes_use_of': set(),
553+
'long_run_time': False,
554+
'inspects_source': False,
555+
'manipulates_source': False,
556+
'manipulates_state': False
557+
}
558+
559+
def test_strategy(self):
560+
actions = [(C, C)] * 100 # Cooperate forever
561+
self.versus_test(axelrod.Cooperator(), expected_actions=actions)
562+
563+
# Will ignore the first four defects
564+
opponent_actions = [D] * 4 + [C] * 100
565+
defect_four = axelrod.MockPlayer(actions=opponent_actions)
566+
actions = [(C, D)] * 4 + [(C, C)] * 100
567+
self.versus_test(defect_four, expected_actions=actions)
568+
569+
actions = [(C, D), (C, D), (C, D), (C, D), (C, D), (D, D), (C, D), (C, D), (D, D), (C, D), (D, D), (C, D), (D, D), (D, D), (D, D), (D, D)]
570+
self.versus_test(axelrod.Defector(), expected_actions=actions, seed=1)
571+
actions = [(C, D), (C, D), (C, D), (C, D), (C, D), (D, D), (C, D), (C, D), (D, D), (C, D), (D, D), (D, D), (D, D), (C, D), (D, D), (D, D)]
572+
self.versus_test(axelrod.Defector(), expected_actions=actions, seed=2)
573+
574+
# After responding to the 11th D (counted as 10 D), just start cooperating
575+
opponent_actions = [D] * 11 + [C] * 100
576+
changed_man = axelrod.MockPlayer(actions=opponent_actions)
577+
actions = [(C, D), (C, D), (C, D), (C, D), (C, D), (D, D), (C, D), (C, D), (D, D), (C, D), (D, D), (C, C)]
578+
actions += [(C,C)] * 99
579+
self.versus_test(changed_man, expected_actions=actions, seed=1)
580+

docs/reference/overview_of_strategies.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ repository.
114114
"K41R_", "Herb Weiner", "Not Implemented"
115115
"K42R_", "Otto Borufsen", ":class:`Borufsen <axelrod.strategies.axelrod_second.Borufsen>`"
116116
"K43R_", "R D Anderson", "Not Implemented"
117-
"K44R_", "William Adams", "Not Implemented"
117+
"K44R_", "William Adams", ":class:`WmAdams <axelrod.strategies.axelrod_second.WmAdams>`"
118118
"K45R_", "Michael F McGurrin", "Not Implemented"
119119
"K46R_", "Graham J Eatherley", ":class:`Eatherley <axelrod.strategies.axelrod_second.Eatherley>`"
120120
"K47R_", "Richard Hufford", "Not Implemented"

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-
77
50+
78
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)