Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented Black, K83R from Axelrod's Second #1155

Merged
merged 3 commits into from
Jan 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions axelrod/strategies/_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from .axelrod_second import (
Champion, Eatherley, Tester, Gladstein, Tranquilizer, MoreGrofman,
Kluepfel, Borufsen, Cave, WmAdams, GraaskampKatzen, Weiner, Harrington,
MoreTidemanAndChieruzzi, Getzler, Leyvraz, White)
MoreTidemanAndChieruzzi, Getzler, Leyvraz, White, Black)
from .backstabber import BackStabber, DoubleCrosser
from .better_and_better import BetterAndBetter
from .bush_mosteller import BushMosteller
Expand Down Expand Up @@ -107,8 +107,9 @@
Appeaser,
ArrogantQLearner,
AverageCopier,
BetterAndBetter,
BackStabber,
BetterAndBetter,
Black,
Borufsen,
Bully,
BushMosteller,
Expand Down
52 changes: 52 additions & 0 deletions axelrod/strategies/axelrod_second.py
Original file line number Diff line number Diff line change
Expand Up @@ -1545,3 +1545,55 @@ def strategy(self, opponent: Player) -> Action:
if np.floor(np.log(turn)) * opponent.defections >= turn:
return D
return C


class Black(Player):
"""
Strategy submitted to Axelrod's second tournament by Paul E Black (K83R)
and came in fourteenth in that tournament.

The strategy Cooperates for the first five turns. Then it calculates the
number of opponent defects in the last five moves and Cooperates with
probability `prob_coop`[`number_defects`], where:

prob_coop[number_defects] = 1 - (number_defects^ 2 - 1) / 25

Names:

- Black: [Axelrod1980b]_
"""

name = 'Black'
classifier = {
'memory_depth': 5,
'stochastic': True,
'makes_use_of': set(),
'long_run_time': False,
'inspects_source': False,
'manipulates_source': False,
'manipulates_state': False
}

def __init__(self) -> None:
super().__init__()
# Maps number of opponent defects from last five moves to own
# Cooperation probability
self.prob_coop = {
0: 1.0,
1: 1.0,
2: 0.88,
3: 0.68,
4: 0.4,
5: 0.04
}

def strategy(self, opponent: Player) -> Action:
if len(opponent.history) < 5:
return C

recent_history = opponent.history[-5:]

did_d = np.vectorize(lambda action: int(action == D))
number_defects = np.sum(did_d(recent_history))

return random_choice(self.prob_coop[number_defects])
26 changes: 26 additions & 0 deletions axelrod/tests/strategies/test_axelrod_second.py
Original file line number Diff line number Diff line change
Expand Up @@ -991,3 +991,29 @@ def test_strategy(self):
(C, C), (C, D), (C, C), (C, D), (C, C), (C, D)]
self.versus_test(axelrod.Random(0.5), expected_actions=actions, seed=12)


class TestBlack(TestPlayer):
name = 'Black'
player = axelrod.Black
expected_classifier = {
'memory_depth': 5,
'stochastic': True,
'makes_use_of': set(),
'long_run_time': False,
'inspects_source': False,
'manipulates_source': False,
'manipulates_state': False
}

def test_strategy(self):
actions = [(C, C)] * 30
self.versus_test(axelrod.Cooperator(), expected_actions=actions)

actions = [(C, D)] * 5
actions += [(D, D), (D, D), (D, D), (D, D), (D, D), (D, D), (D, D), (D, D), (D, D), (C, D)]
self.versus_test(axelrod.Defector(), expected_actions=actions, seed=1)

actions = [(C, D)] * 5
actions += [(D, D), (C, D), (D, D), (D, D), (D, D), (C, D), (D, D), (D, D), (D, D), (D, D)]
self.versus_test(axelrod.Defector(), expected_actions=actions, seed=15)

2 changes: 1 addition & 1 deletion docs/reference/overview_of_strategies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ repository.
"K80R_", "Robyn M Dawes and Mark Batell", Not Implemented
"K81R_", "Martyn Jones", "Not Implemented"
"K82R_", "Robert A Leyland", "Not Implemented"
"K83R_", "Paul E Black", "Not Implemented"
"K83R_", "Paul E Black", ":class:`White <axelrod.strategies.axelrod_second.White>`"
"K84R_", "T Nicolaus Tideman and Paula Chieruzzi", ":class:`More Tideman And Chieruzzi <axelrod.strategies.axelrod_second.MoreTidemanAndChieruzzi>`"
"K85R_", "Robert B Falk and James M Langsted", "Not Implemented"
"K86R_", "Bernard Grofman", "Not Implemented"
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)
81
82

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