Skip to content

Commit 6543db8

Browse files
committed
New strategy: Collective Strategy
1 parent b166a91 commit 6543db8

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

axelrod/strategies/_strategies.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@
5252
from .mutual import Desperate, Hopeless, Willing
5353
from .negation import Negation
5454
from .oncebitten import OnceBitten, FoolMeOnce, ForgetfulFoolMeOnce, FoolMeForever
55-
from .prober import (Prober, Prober2, Prober3, Prober4, HardProber,
56-
NaiveProber, RemorsefulProber)
55+
from .prober import (CollectiveStrategy, Prober, Prober2, Prober3, Prober4,
56+
HardProber, NaiveProber, RemorsefulProber)
5757
from .punisher import Punisher, InversePunisher
5858
from .qlearner import (
5959
RiskyQLearner, ArrogantQLearner, HesitantQLearner, CautiousQLearner)
@@ -92,6 +92,7 @@
9292
Calculator,
9393
CautiousQLearner,
9494
Champion,
95+
CollectiveStrategy,
9596
ContriteTitForTat,
9697
Cooperator,
9798
CooperatorHunter,

axelrod/strategies/prober.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,42 @@
55
C, D = Actions.C, Actions.D
66

77

8+
class CollectiveStrategy(Player):
9+
"""Defined in [Li2009]. 'It always cooperates in the first move and defects
10+
in the second move. If the opponent also cooperates in the first move and
11+
defects in the second move, CS will cooperate until the opponent defects.
12+
Otherwise, CS will always defect.'
13+
14+
Names:
15+
Collective Strategy [Li2009]
16+
17+
"""
18+
19+
name = "CollectiveStrategy"
20+
21+
classifier = {
22+
'stochastic': False,
23+
'memory_depth': float('inf'), # Long memory
24+
'makes_use_of': set(),
25+
'long_run_time': False,
26+
'inspects_source': False,
27+
'manipulates_source': False,
28+
'manipulates_state': False
29+
}
30+
31+
def strategy(self, opponent):
32+
turn = len(self.history)
33+
if turn == 0:
34+
return C
35+
if turn == 1:
36+
return D
37+
if opponent.defections > 1:
38+
return D
39+
if opponent.history[0:2] == [C, D]:
40+
return C
41+
return D
42+
43+
844
class Prober(Player):
945
"""
1046
Plays D, C, C initially. Defects forever if opponent cooperated in moves 2

axelrod/tests/unit/test_prober.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,34 @@
88
C, D = axelrod.Actions.C, axelrod.Actions.D
99

1010

11+
class TestCollectiveStrategy(TestPlayer):
12+
13+
name = "CollectiveStrategy"
14+
player = axelrod.CollectiveStrategy
15+
expected_classifier = {
16+
'memory_depth': float('inf'),
17+
'stochastic': False,
18+
'makes_use_of': set(),
19+
'long_run_time': False,
20+
'inspects_source': False,
21+
'manipulates_source': False,
22+
'manipulates_state': False
23+
}
24+
25+
def test_initial_strategy(self):
26+
"""Starts by playing CD."""
27+
self.responses_test([], [], [C, D])
28+
29+
def test_strategy(self):
30+
# Defects forever unless opponent matched first two moves
31+
self.responses_test([C, D], [C, C], [D] * 10)
32+
self.responses_test([C, D], [D, C], [D] * 10)
33+
self.responses_test([C, D], [D, D], [D] * 10)
34+
35+
self.responses_test([C, D], [C, D], [C] * 10)
36+
self.responses_test([C, D, D], [C, D, D], [D] * 10)
37+
38+
1139
class TestProber(TestPlayer):
1240

1341
name = "Prober"

docs/reference/bibliography.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ documentation.
1919
.. [Frean1994] Frean, Marcus R. “The Prisoner's Dilemma without Synchrony.” Proceedings: Biological Sciences, vol. 257, no. 1348, 1994, pp. 75–79. www.jstor.org/stable/50253.
2020
.. [Hilde2013] Hilbe, C., Nowak, M.A. and Traulsen, A. (2013). Adaptive dynamics of extortion and compliance, PLoS ONE, 8(11), p. e77886. doi: 10.1371/journal.pone.0077886.
2121
.. [Kraines1989] Kraines, D. & Kraines, V. Theor Decis (1989) 26: 47. doi:10.1007/BF00134056
22+
.. [Li2009] Li, J. & Kendall, G. (2009). A Strategy with Novel Evolutionary Features for the Iterated Prisoner’s Dilemma. Evolutionary Computation 17(2): 257–274.
2223
.. [Li2011] Li, J., Hingston, P., Member, S., & Kendall, G. (2011). Engineering Design of Strategies for Winning Iterated Prisoner ’ s Dilemma Competitions, 3(4), 348–360.
2324
.. [Li2014] Li, J. and Kendall, G. (2016). The Effect of Memory Size on the Evolutionary Stability of Strategies in Iterated Prisoner's Dilemma. IEEE Transactions on Evolutionary Computation, 18(6) 819-826
2425
.. [Nachbar1992] Nachbar J., Evolution in the finitely repeated prisoner’s dilemma, Journal of Economic Behavior & Organization, 19(3): 307-326, 1992.

0 commit comments

Comments
 (0)