Skip to content

Commit 856452c

Browse files
committed
Implemented Black, K83R from Axelrod's Second.
1 parent 0a857d3 commit 856452c

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