Skip to content

Commit beab229

Browse files
committed
Implemented White strategy, k72r from Axelrod's Second
1 parent dacc9c8 commit beab229

File tree

4 files changed

+76
-5
lines changed

4 files changed

+76
-5
lines changed

axelrod/strategies/_strategies.py

Lines changed: 2 additions & 1 deletion
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)
13+
MoreTidemanAndChieruzzi, Getzler, Leyvraz, White)
1414
from .backstabber import BackStabber, DoubleCrosser
1515
from .better_and_better import BetterAndBetter
1616
from .bush_mosteller import BushMosteller
@@ -276,6 +276,7 @@
276276
TwoTitsForTat,
277277
VeryBad,
278278
Weiner,
279+
White,
279280
Willing,
280281
Winner12,
281282
Winner21,

axelrod/strategies/axelrod_second.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,7 @@ def calculate_chi_squared(self, turn):
11371137
In this function, the statistic is non-standard in that it excludes
11381138
summands where E_i <= 1.
11391139
"""
1140-
1140+
11411141
denom = turn - 2
11421142

11431143
expected_matrix = np.outer(self.move_history.sum(axis=1),
@@ -1165,7 +1165,7 @@ def detect_random(self, turn):
11651165
self.history, and returns True (is random) if and only if the statistic
11661166
is less than or equal to 3.
11671167
"""
1168-
1168+
11691169
denom = turn - 2
11701170

11711171
if self.move_history[0, 0] / denom >= 0.8:
@@ -1300,7 +1300,7 @@ def strategy(self, opponent: Player) -> Action:
13001300
if self.burned or random.random() > self.prob:
13011301
# Tit-for-Tat with probability 1-`prob`
13021302
return self.try_return(opponent.history[-1], inc_parity=True)
1303-
1303+
13041304
# Otherwise Defect, Cooperate, Cooperate, and increase `prob`
13051305
self.prob += 0.05
13061306
self.more_coop, self.last_generous_n_turns_ago = 2, 1
@@ -1506,3 +1506,42 @@ def strategy(self, opponent: Player) -> Action:
15061506
recent_history[-go_back] = opponent.history[-go_back]
15071507

15081508
return random_choice(self.prob_coop[tuple(recent_history)])
1509+
1510+
1511+
class White(Player):
1512+
"""
1513+
Strategy submitted to Axelrod's second tournament by Edward C Whtie (K72R)
1514+
and came in twelfth in that tournament.
1515+
1516+
If the opponent Cooperated last turn or in the first ten turns, then
1517+
Cooperate.
1518+
1519+
Otherwise Defect if and only if:
1520+
1521+
floor(log(turn)) * opponent Defections >= turn
1522+
1523+
Names:
1524+
1525+
- White: [Axelrod1980b]_
1526+
"""
1527+
1528+
name = 'White'
1529+
classifier = {
1530+
'memory_depth': float("inf"),
1531+
'stochastic': False,
1532+
'makes_use_of': set(),
1533+
'long_run_time': False,
1534+
'inspects_source': False,
1535+
'manipulates_source': False,
1536+
'manipulates_state': False
1537+
}
1538+
1539+
def strategy(self, opponent: Player) -> Action:
1540+
turn = len(self.history) + 1
1541+
1542+
if turn <= 10 or opponent.history[-1] == C:
1543+
return C
1544+
1545+
if np.floor(np.log(turn)) * opponent.defections >= turn:
1546+
return D
1547+
return C

axelrod/tests/strategies/test_axelrod_second.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,3 +960,34 @@ def test_strategy(self):
960960
self.versus_test(axelrod.Alternator(), expected_actions=actions, seed=2)
961961
actions = [(C, C), (C, D), (C, C)] + [(D, D), (C, C)] * 3
962962
self.versus_test(axelrod.Alternator(), expected_actions=actions, seed=3)
963+
964+
965+
class TestWhite(TestPlayer):
966+
name = 'White'
967+
player = axelrod.White
968+
expected_classifier = {
969+
'memory_depth': float("inf"),
970+
'stochastic': False,
971+
'makes_use_of': set(),
972+
'long_run_time': False,
973+
'inspects_source': False,
974+
'manipulates_source': False,
975+
'manipulates_state': False
976+
}
977+
978+
def test_strategy(self):
979+
actions = [(C, C)] * 30
980+
self.versus_test(axelrod.Cooperator(), expected_actions=actions)
981+
982+
actions = [(C, D)] * 10 + [(D, D)] * 20
983+
self.versus_test(axelrod.Defector(), expected_actions=actions)
984+
985+
actions = [(C, D), (C, D), (C, C), (C, C), (C, C), (C, D), (C, C),
986+
(C, D), (C, C), (C, D), (C, C), (C, D), (C, D), (D, C),
987+
(C, D), (D, D), (D, C), (C, D), (D, D), (D, C)]
988+
self.versus_test(axelrod.Random(0.5), expected_actions=actions, seed=6)
989+
actions = [(C, C), (C, D), (C, D), (C, C), (C, C), (C, C), (C, C),
990+
(C, D), (C, D), (C, D), (C, D), (D, D), (D, C), (C, C),
991+
(C, C), (C, D), (C, C), (C, D), (C, C), (C, D)]
992+
self.versus_test(axelrod.Random(0.5), expected_actions=actions, seed=12)
993+

docs/reference/overview_of_strategies.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ repository.
140140
"K69R_", "Johann Joss", "Not Implemented"
141141
"K70R_", "Robert Pebly", "Not Implemented"
142142
"K71R_", "James E Hall", "Not Implemented"
143-
"K72R_", "Edward C White Jr", "Not Implemented"
143+
"K72R_", "Edward C White Jr", ":class:`White <axelrod.strategies.axelrod_second.White>`"
144144
"K73R_", "George Zimmerman", "Not Implemented"
145145
"K74R_", "Edward Friedland", "Not Implemented"
146146
"K74RXX_", "Edward Friedland", "Not Implemented"

0 commit comments

Comments
 (0)