Skip to content

Commit fd868be

Browse files
committed
Add Gradual Killer strategy
1 parent ae21652 commit fd868be

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed

axelrod/strategies/_strategies.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
GoByMajority5,
2828
HardGoByMajority, HardGoByMajority10, HardGoByMajority20, HardGoByMajority40,
2929
HardGoByMajority5)
30+
from .gradualkiller import GradualKiller
3031
from .grudger import (Grudger, ForgetfulGrudger, OppositeGrudger, Aggravater,
3132
SoftGrudger)
3233
from .grumpy import Grumpy
@@ -120,6 +121,7 @@
120121
GoByMajority5,
121122
Golden,
122123
Gradual,
124+
GradualKiller,
123125
Grofman,
124126
Grudger,
125127
Grumpy,

axelrod/strategies/gradualkiller.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from axelrod import Actions, Player, init_args
2+
C, D = Actions.C, Actions.D
3+
4+
5+
class GradualKiller(Player):
6+
"""
7+
It begins by defecting in the first five moves, then cooperates two times.
8+
It then defects all the time if the opponent has defected in move 6 and 7,
9+
else cooperates all the time.
10+
11+
Names
12+
13+
- Gradual Killer: [PRISON1998]_
14+
"""
15+
16+
# These are various properties for the strategy
17+
name = 'Gradual Killer'
18+
classifier = {
19+
'memory_depth': float('Inf'),
20+
'stochastic': False,
21+
'makes_use_of': set(),
22+
'long_run_time': False,
23+
'inspects_source': False,
24+
'manipulates_source': False,
25+
'manipulates_state': False
26+
}
27+
28+
def strategy(self, opponent):
29+
"""This is the actual strategy"""
30+
# First seven moves
31+
firstseven = [D, D, D, D, D, C, C]
32+
if len(self.history) < 7:
33+
return firstseven[len(self.history)]
34+
# React to the opponent's 6th and 7th moves
35+
elif opponent.history[5:7] == [D, D]:
36+
return D
37+
return C
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
"""Test for the gradual killer strategy."""
2+
3+
import axelrod
4+
from .test_player import TestHeadsUp, TestPlayer
5+
6+
from hypothesis import given
7+
from hypothesis.strategies import integers
8+
from axelrod.tests.property import strategy_lists
9+
10+
import random
11+
12+
C, D = axelrod.Actions.C, axelrod.Actions.D
13+
14+
15+
class TestGradualKiller(TestPlayer):
16+
17+
name = "Gradual Killer"
18+
player = axelrod.GradualKiller
19+
expected_classifier = {
20+
'memory_depth': float('Inf'),
21+
'stochastic': False,
22+
'makes_use_of': set(),
23+
'inspects_source': False,
24+
'manipulates_source': False,
25+
'manipulates_state': False
26+
}
27+
28+
def test_strategy(self):
29+
"""Starts by Defecting."""
30+
self.first_play_test(D)
31+
32+
def test_effect_of_strategy(self):
33+
"""Fist seven moves."""
34+
self.markov_test([D, D, D, D])
35+
self.responses_test([], [], [D, D, D, D, D, C, C])
36+
37+
def test_effect_of_strategy_with_history_CC(self):
38+
"""Continues with C if opponent played CC on 6 and 7."""
39+
P1 = axelrod.GradualKiller()
40+
P2 = axelrod.Player()
41+
P1.history = [D, D, D, D, D, C, C]
42+
P2.history = [C, C, C, C, C, C, C]
43+
self.assertEqual(P1.strategy(P2), 'C')
44+
P1.history = [D, D, D, D, D, C, C, C]
45+
P2.history = [C, C, C, C, C, C, C, C]
46+
self.assertEqual(P1.strategy(P2), 'C')
47+
P1.history = [D, D, D, D, D, C, C, C, C]
48+
P2.history = [C, C, C, C, C, C, C, C, C]
49+
self.assertEqual(P1.strategy(P2), 'C')
50+
P1.history = [D, D, D, D, D, C, C, C, C, C]
51+
P2.history = [C, C, C, C, C, C, C, C, C, C]
52+
self.assertEqual(P1.strategy(P2), 'C')
53+
54+
def test_effect_of_strategy_with_history_CD(self):
55+
"""Continues with C if opponent played CD on 6 and 7."""
56+
P1 = axelrod.GradualKiller()
57+
P2 = axelrod.Player()
58+
P1.history = [D, D, D, D, D, C, C]
59+
P2.history = [C, C, C, C, C, C, D]
60+
self.assertEqual(P1.strategy(P2), 'C')
61+
P1.history = [D, D, D, D, D, C, C, C]
62+
P2.history = [C, C, C, C, C, C, D, D]
63+
self.assertEqual(P1.strategy(P2), 'C')
64+
P1.history = [D, D, D, D, D, C, C, C, C]
65+
P2.history = [C, C, C, C, C, C, D, D, C]
66+
self.assertEqual(P1.strategy(P2), 'C')
67+
P1.history = [D, D, D, D, D, C, C, C, C, C]
68+
P2.history = [C, C, C, C, C, C, D, D, C, C]
69+
self.assertEqual(P1.strategy(P2), 'C')
70+
71+
def test_effect_of_strategy_with_history_DC(self):
72+
"""Continues with C if opponent played DC on 6 and 7."""
73+
P1 = axelrod.GradualKiller()
74+
P2 = axelrod.Player()
75+
P1.history = [D, D, D, D, D, C, C]
76+
P2.history = [C, C, C, C, C, D, C]
77+
self.assertEqual(P1.strategy(P2), 'C')
78+
P1.history = [D, D, D, D, D, C, C, C]
79+
P2.history = [C, C, C, C, C, D, C, C]
80+
self.assertEqual(P1.strategy(P2), 'C')
81+
P1.history = [D, D, D, D, D, C, C, C, C]
82+
P2.history = [C, C, C, C, C, D, C, C, D]
83+
self.assertEqual(P1.strategy(P2), 'C')
84+
P1.history = [D, D, D, D, D, C, C, C, C, C]
85+
P2.history = [C, C, C, C, C, D, C, C, D, C]
86+
self.assertEqual(P1.strategy(P2), 'C')
87+
88+
def test_effect_of_strategy_with_history_CC(self):
89+
"""Continues with D if opponent played DD on 6 and 7."""
90+
P1 = axelrod.GradualKiller()
91+
P2 = axelrod.Player()
92+
P1.history = [D, D, D, D, D, C, C]
93+
P2.history = [C, C, C, C, C, D, D]
94+
self.assertEqual(P1.strategy(P2), 'D')
95+
P1.history = [D, D, D, D, D, C, C, D]
96+
P2.history = [C, C, C, C, C, D, D, C]
97+
self.assertEqual(P1.strategy(P2), 'D')
98+
P1.history = [D, D, D, D, D, C, C, D, D]
99+
P2.history = [C, C, C, C, C, D, D, C, C]
100+
self.assertEqual(P1.strategy(P2), 'D')
101+
P1.history = [D, D, D, D, D, C, C, D, D, D]
102+
P2.history = [C, C, C, C, C, D, D, C, C, D]
103+
self.assertEqual(P1.strategy(P2), 'D')
104+

docs/reference/bibliography.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ documentation.
1818
.. [Stewart2012] Stewart, a. J., & Plotkin, J. B. (2012). Extortion and cooperation in the Prisoner’s Dilemma. Proceedings of the National Academy of Sciences, 109(26), 10134–10135. http://doi.org/10.1073/pnas.1208087109
1919
.. [Szabó1992] Szabó, G., & Fáth, G. (2007). Evolutionary games on graphs. Physics Reports, 446(4-6), 97–216. http://doi.org/10.1016/j.physrep.2007.04.004
2020
.. [Tzafestas2000] Tzafestas, E. (2000). Toward adaptive cooperative behavior. From Animals to Animals: Proceedings of the 6th International Conference on the Simulation of Adaptive Behavior {(SAB-2000)}, 2, 334–340.
21+
.. [PRISON1998] LIFL (1998) PRISON. Available at: http://www.lifl.fr/IPD/ipd.frame.html (Accessed: 19 September 2016).

0 commit comments

Comments
 (0)