Skip to content

Commit b174daa

Browse files
janga1997marcharper
authored andcommitted
Add new strategy ShortMem (#857)
* Add new strategy ShortMem * Fix errors * Make changes to docstring * Edit for efficiency * Add credit to author * Make changes to ShortMem * Include ShortMem in _strategies.py * Add tests for ShortMem * Add paper to bibliography * Fix error in docstring * Word wrap to 80 columns * Fix style issues * Fix logical error in strategy and test * Revert "Fix logical error in strategy and test" This reverts commit a7b8d9f. * Fix Readability
1 parent 6d136e1 commit b174daa

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

axelrod/strategies/_strategies.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
Retaliate, Retaliate2, Retaliate3, LimitedRetaliate, LimitedRetaliate2,
6868
LimitedRetaliate3)
6969
from .sequence_player import SequencePlayer, ThueMorse, ThueMorseInverse
70+
from .shortmem import ShortMem
7071
from .titfortat import (
7172
TitForTat, TitFor2Tats, TwoTitsForTat, Bully, SneakyTitForTat,
7273
SuspiciousTitForTat, AntiTitForTat, HardTitForTat, HardTitFor2Tats,
@@ -208,6 +209,7 @@
208209
RevisedDowning,
209210
Ripoff,
210211
RiskyQLearner,
212+
ShortMem,
211213
Shubik,
212214
SlowTitForTwoTats,
213215
SneakyTitForTat,

axelrod/strategies/shortmem.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from axelrod import Actions, Player
2+
from axelrod.actions import Action
3+
4+
C, D = Actions.C, Actions.D
5+
6+
class ShortMem(Player):
7+
"""
8+
A player starts by always cooperating for the first 10 moves.
9+
10+
From the tenth round on, the player analyzes the last ten actions, and
11+
compare the number of defects and cooperates of the opponent, based in
12+
percentage. If cooperation occurs 30% more than defection, it will
13+
cooperate.
14+
If defection occurs 30% more than cooperation, the program will defect.
15+
Otherwise, the program follows the TitForTat algorithm.
16+
17+
Names:
18+
19+
- ShortMem: [Andre2013]_
20+
"""
21+
22+
name = 'ShortMem'
23+
classifier = {
24+
'memory_depth': 10,
25+
'stochastic': False,
26+
'makes_use_of': set(),
27+
'long_run_time': False,
28+
'inspects_source': False,
29+
'manipulates_source': False,
30+
'manipulates_state': False
31+
}
32+
33+
@staticmethod
34+
def strategy(opponent: Player) -> Action:
35+
36+
if len(opponent.history) <= 10:
37+
return C
38+
39+
array = opponent.history[-10:]
40+
C_counts = array.count('C')
41+
D_counts = array.count('D')
42+
43+
if C_counts - D_counts >= 3:
44+
return C
45+
elif D_counts - C_counts >= 3:
46+
return D
47+
else:
48+
return opponent.history[-1]
49+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""Tests for the ShortMem strategy."""
2+
3+
import axelrod
4+
from .test_player import TestPlayer
5+
6+
C, D = axelrod.Actions.C, axelrod.Actions.D
7+
8+
class TestCooperator(TestPlayer):
9+
10+
name = "ShortMem"
11+
player = axelrod.ShortMem
12+
expected_classifier = {
13+
'memory_depth': 10,
14+
'stochastic': False,
15+
'makes_use_of': set(),
16+
'inspects_source': False,
17+
'manipulates_source': False,
18+
'manipulates_state': False
19+
}
20+
21+
def test_strategy(self):
22+
# Starts by cooperating for the first ten moves.
23+
self.responses_test([C], [C] * 4, [D] * 4)
24+
self.responses_test([C], [C] * 9, [D] * 9)
25+
self.responses_test([C], [C] * 4 + [D] *6, [D] * 4 + [C] * 6)
26+
27+
#Cooperate if in the last ten moves, Cooperations - Defections >= 3
28+
self.responses_test([C], [C] * 13 + [D] * 7, [D] * 16 + [C] * 7)
29+
30+
#Defect if in the last ten moves, Defections - Cooperations >= 3
31+
self.responses_test([D], [D] * 13 + [C] * 7, [C] * 12 + [D] * 8)
32+
33+
#If neither of the above conditions are met, apply TitForTat
34+
self.responses_test([D], [C] * 13 + [D] * 7, [C] * 15 + [D] * 5)
35+
self.responses_test([C], [C] * 17 + [D] * 7, [D] * 18 + [C] * 6)

docs/reference/bibliography.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ documentation.
3737
.. [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
3838
.. [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
3939
.. [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.
40+
.. [Andre2013] Andre L. C., Honovan P., Felipe T. and Frederico G. (2013). Iterated Prisoner’s Dilemma - An extended analysis, http://abricom.org.br/wp-content/uploads/2016/03/bricsccicbic2013_submission_202.pdf

0 commit comments

Comments
 (0)