Skip to content

Commit aca6ffd

Browse files
gaffney2010marcharper
authored andcommitted
Implemented Black, K83R from Axelrod's Second (#1155)
* Implemented Black, K83R from Axelrod's Second. * Fixed docstrings.
1 parent 3ee5c1f commit aca6ffd

File tree

5 files changed

+83
-4
lines changed

5 files changed

+83
-4
lines changed

axelrod/strategies/_strategies.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from .axelrod_second import (
1111
Champion, Eatherley, Tester, Gladstein, Tranquilizer, MoreGrofman,
1212
Kluepfel, Borufsen, Cave, WmAdams, GraaskampKatzen, Weiner, Harrington,
13-
MoreTidemanAndChieruzzi, Getzler, Leyvraz, White)
13+
MoreTidemanAndChieruzzi, Getzler, Leyvraz, White, Black)
1414
from .backstabber import BackStabber, DoubleCrosser
1515
from .better_and_better import BetterAndBetter
1616
from .bush_mosteller import BushMosteller
@@ -107,8 +107,9 @@
107107
Appeaser,
108108
ArrogantQLearner,
109109
AverageCopier,
110-
BetterAndBetter,
111110
BackStabber,
111+
BetterAndBetter,
112+
Black,
112113
Borufsen,
113114
Bully,
114115
BushMosteller,

axelrod/strategies/axelrod_second.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,3 +1545,55 @@ def strategy(self, opponent: Player) -> Action:
15451545
if np.floor(np.log(turn)) * opponent.defections >= turn:
15461546
return D
15471547
return C
1548+
1549+
1550+
class Black(Player):
1551+
"""
1552+
Strategy submitted to Axelrod's second tournament by Paul E Black (K83R)
1553+
and came in fourteenth in that tournament.
1554+
1555+
The strategy Cooperates for the first five turns. Then it calculates the
1556+
number of opponent defects in the last five moves and Cooperates with
1557+
probability `prob_coop`[`number_defects`], where:
1558+
1559+
prob_coop[number_defects] = 1 - (number_defects^ 2 - 1) / 25
1560+
1561+
Names:
1562+
1563+
- Black: [Axelrod1980b]_
1564+
"""
1565+
1566+
name = 'Black'
1567+
classifier = {
1568+
'memory_depth': 5,
1569+
'stochastic': True,
1570+
'makes_use_of': set(),
1571+
'long_run_time': False,
1572+
'inspects_source': False,
1573+
'manipulates_source': False,
1574+
'manipulates_state': False
1575+
}
1576+
1577+
def __init__(self) -> None:
1578+
super().__init__()
1579+
# Maps number of opponent defects from last five moves to own
1580+
# Cooperation probability
1581+
self.prob_coop = {
1582+
0: 1.0,
1583+
1: 1.0,
1584+
2: 0.88,
1585+
3: 0.68,
1586+
4: 0.4,
1587+
5: 0.04
1588+
}
1589+
1590+
def strategy(self, opponent: Player) -> Action:
1591+
if len(opponent.history) < 5:
1592+
return C
1593+
1594+
recent_history = opponent.history[-5:]
1595+
1596+
did_d = np.vectorize(lambda action: int(action == D))
1597+
number_defects = np.sum(did_d(recent_history))
1598+
1599+
return random_choice(self.prob_coop[number_defects])

axelrod/tests/strategies/test_axelrod_second.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,3 +991,29 @@ def test_strategy(self):
991991
(C, C), (C, D), (C, C), (C, D), (C, C), (C, D)]
992992
self.versus_test(axelrod.Random(0.5), expected_actions=actions, seed=12)
993993

994+
995+
class TestBlack(TestPlayer):
996+
name = 'Black'
997+
player = axelrod.Black
998+
expected_classifier = {
999+
'memory_depth': 5,
1000+
'stochastic': True,
1001+
'makes_use_of': set(),
1002+
'long_run_time': False,
1003+
'inspects_source': False,
1004+
'manipulates_source': False,
1005+
'manipulates_state': False
1006+
}
1007+
1008+
def test_strategy(self):
1009+
actions = [(C, C)] * 30
1010+
self.versus_test(axelrod.Cooperator(), expected_actions=actions)
1011+
1012+
actions = [(C, D)] * 5
1013+
actions += [(D, D), (D, D), (D, D), (D, D), (D, D), (D, D), (D, D), (D, D), (D, D), (C, D)]
1014+
self.versus_test(axelrod.Defector(), expected_actions=actions, seed=1)
1015+
1016+
actions = [(C, D)] * 5
1017+
actions += [(D, D), (C, D), (D, D), (D, D), (D, D), (C, D), (D, D), (D, D), (D, D), (D, D)]
1018+
self.versus_test(axelrod.Defector(), expected_actions=actions, seed=15)
1019+

docs/reference/overview_of_strategies.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ repository.
152152
"K80R_", "Robyn M Dawes and Mark Batell", Not Implemented
153153
"K81R_", "Martyn Jones", "Not Implemented"
154154
"K82R_", "Robert A Leyland", "Not Implemented"
155-
"K83R_", "Paul E Black", "Not Implemented"
155+
"K83R_", "Paul E Black", ":class:`White <axelrod.strategies.axelrod_second.White>`"
156156
"K84R_", "T Nicolaus Tideman and Paula Chieruzzi", ":class:`More Tideman And Chieruzzi <axelrod.strategies.axelrod_second.MoreTidemanAndChieruzzi>`"
157157
"K85R_", "Robert B Falk and James M Langsted", "Not Implemented"
158158
"K86R_", "Bernard Grofman", "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-
81
50+
82
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)