Skip to content

Commit da76269

Browse files
committed
Implemented Mikkelson, k66r from Axelrod's second.
1 parent 79f3e55 commit da76269

File tree

3 files changed

+120
-1
lines changed

3 files changed

+120
-1
lines changed

axelrod/strategies/_strategies.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
Champion, Eatherley, Tester, Gladstein, Tranquilizer, MoreGrofman,
1212
Kluepfel, Borufsen, Cave, WmAdams, GraaskampKatzen, Weiner, Harrington,
1313
MoreTidemanAndChieruzzi, Getzler, Leyvraz, White, Black, RichardHufford,
14-
Yamachi, Colbert)
14+
Yamachi, Colbert, Mikkelson)
1515
from .backstabber import BackStabber, DoubleCrosser
1616
from .better_and_better import BetterAndBetter
1717
from .bush_mosteller import BushMosteller
@@ -208,6 +208,7 @@
208208
NaiveProber,
209209
MEM2,
210210
Michaelos,
211+
Mikkelson,
211212
MindBender,
212213
MindController,
213214
MindReader,

axelrod/strategies/axelrod_second.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,3 +1851,67 @@ def __init__(self) -> None:
18511851

18521852
super().__init__(transitions=transitions, initial_state=0,
18531853
initial_action=C)
1854+
1855+
1856+
class Mikkelson(FSMPlayer):
1857+
"""
1858+
Strategy submitted to Axelrod's second tournament by Ray Mikkelson (K66R)
1859+
and came in twentieth in that tournament.
1860+
1861+
The strategy keeps track of a variable called `credit`, which determines if
1862+
the strategy will Cooperate, in the sense that if `credit` is positive,
1863+
then the strategy Cooperates. `credit` is initialized to 7. After the
1864+
first turn, `credit` increments if the opponent Cooperated last turn, and
1865+
decreases by two otherwise. `credit` is capped above by 8 and below by -7.
1866+
[`credit` is assessed as postive or negative, after increasing based on
1867+
opponent's last turn.]
1868+
1869+
If `credit` is non-positive within the first ten turns, then the strategy
1870+
Defects and `credit` is set to 4. If `credit` is non-positive later, then
1871+
the strategy Defects if and only if (total # opponent Defections) / (turn#)
1872+
is at least 15%. [Turn # starts at 1.]
1873+
1874+
Names:
1875+
1876+
- Mikkelson: [Axelrod1980b]_
1877+
"""
1878+
1879+
name = 'Mikkelson'
1880+
classifier = {
1881+
'memory_depth': float("inf"),
1882+
'stochastic': False,
1883+
'makes_use_of': set(),
1884+
'long_run_time': False,
1885+
'inspects_source': False,
1886+
'manipulates_source': False,
1887+
'manipulates_state': False
1888+
}
1889+
1890+
def __init__(self) -> None:
1891+
super().__init__()
1892+
self.credit = 7
1893+
1894+
def strategy(self, opponent: Player) -> Action:
1895+
turn = len(self.history) + 1
1896+
if turn == 1:
1897+
return C
1898+
1899+
if opponent.history[-1] == C:
1900+
self.credit += 1
1901+
if self.credit > 8:
1902+
self.credit = 8
1903+
else:
1904+
self.credit -= 2
1905+
if self.credit < -7:
1906+
self.credit = -7
1907+
1908+
if turn == 2:
1909+
return C
1910+
if self.credit > 0:
1911+
return C
1912+
if turn <= 10:
1913+
self.credit = 4
1914+
return D
1915+
if opponent.defections / turn >= 0.15:
1916+
return D
1917+
return C

axelrod/tests/strategies/test_axelrod_second.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,3 +1189,57 @@ def test_strategy(self):
11891189
actions = [(C, C)] * 5 + [(D, C)] + [(C, C)] * 4
11901190
actions += [(C, D)] + [(D, C), (D, C), (C, C), (C, C)] + [(C, C)]
11911191
self.versus_test(OddBall, expected_actions=actions)
1192+
1193+
1194+
class TestMikkelson(TestPlayer):
1195+
name = 'Mikkelson'
1196+
player = axelrod.Mikkelson
1197+
expected_classifier = {
1198+
'memory_depth': float("inf"),
1199+
'stochastic': False,
1200+
'makes_use_of': set(),
1201+
'long_run_time': False,
1202+
'inspects_source': False,
1203+
'manipulates_source': False,
1204+
'manipulates_state': False
1205+
}
1206+
1207+
def test_strategy(self):
1208+
actions = [(C, C)] * 30
1209+
self.versus_test(axelrod.Cooperator(), expected_actions=actions, attrs={"credit": 8})
1210+
1211+
actions = [(C, D), (C, D), (C, D), (C, D)]
1212+
self.versus_test(axelrod.Defector(), expected_actions=actions, attrs={"credit": 1})
1213+
# Defect then reset to 4
1214+
actions += [(D, D)]
1215+
self.versus_test(axelrod.Defector(), expected_actions=actions, attrs={"credit": 4})
1216+
# Repeat
1217+
actions += [(C, D), (D, D)] * 2
1218+
self.versus_test(axelrod.Defector(), expected_actions=actions, attrs={"credit": 4})
1219+
# With ten turns passed, keep defecting now
1220+
actions += [(C, D), (D, D)]
1221+
self.versus_test(axelrod.Defector(), expected_actions=actions, attrs={"credit": 0})
1222+
# With ten turns passed, keep defecting now
1223+
actions += [(D, D)] * 30
1224+
self.versus_test(axelrod.Defector(), expected_actions=actions, attrs={"credit": -7})
1225+
1226+
1227+
actions = [(C, D), (C, D), (C, C)]
1228+
self.versus_test(axelrod.Cycler("DDC"), expected_actions=actions, attrs={"credit": 3})
1229+
actions += [(C, D), (C, D)]
1230+
self.versus_test(axelrod.Cycler("DDC"), expected_actions=actions, attrs={"credit": 2})
1231+
actions += [(D, C)]
1232+
self.versus_test(axelrod.Cycler("DDC"), expected_actions=actions, attrs={"credit": 4})
1233+
actions += [(C, D)]
1234+
self.versus_test(axelrod.Cycler("DDC"), expected_actions=actions, attrs={"credit": 5})
1235+
actions += [(C, D)]
1236+
self.versus_test(axelrod.Cycler("DDC"), expected_actions=actions, attrs={"credit": 3})
1237+
1238+
opponent_actions = [C] * 100 + [D] * 10
1239+
Change_of_Heart = axelrod.MockPlayer(actions=opponent_actions)
1240+
actions = [(C, C)] * 100 + [(C, D)] * 4
1241+
self.versus_test(Change_of_Heart, expected_actions=actions, attrs={"credit": 2})
1242+
Change_of_Heart = axelrod.MockPlayer(actions=opponent_actions)
1243+
actions += [(C, D)] * 2
1244+
self.versus_test(Change_of_Heart, expected_actions=actions, attrs={"credit": -2})
1245+
# Still Cooperate, because Defect rate is low

0 commit comments

Comments
 (0)