Skip to content

Commit 19779ab

Browse files
authored
Merge pull request #865 from souravsingh/add-strategy
[MRG] Add Resurrection Strategy
2 parents 8f07243 + bba9fc7 commit 19779ab

File tree

5 files changed

+78
-2
lines changed

5 files changed

+78
-2
lines changed

axelrod/strategies/_strategies.py

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
from .qlearner import (
6464
RiskyQLearner, ArrogantQLearner, HesitantQLearner, CautiousQLearner)
6565
from .rand import Random
66+
from .resurrection import Resurrection
6667
from .retaliate import (
6768
Retaliate, Retaliate2, Retaliate3, LimitedRetaliate, LimitedRetaliate2,
6869
LimitedRetaliate3)
@@ -206,6 +207,7 @@
206207
Random,
207208
RandomHunter,
208209
RemorsefulProber,
210+
Resurrection,
209211
Retaliate,
210212
Retaliate2,
211213
Retaliate3,

axelrod/strategies/resurrection.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from axelrod.actions import Actions, Action
2+
from axelrod.player import Player
3+
4+
C, D = Actions.C, Actions.D
5+
6+
7+
class Resurrection(Player):
8+
"""
9+
A player starts by cooperating and defects if the number of rounds
10+
played by the player is greater than five and the last five rounds are defections.
11+
Otherwise, the strategy plays like Tit-for-tat.
12+
13+
Names:
14+
- Resurrection: Name from CoopSim https://github.com/jecki/CoopSim
15+
"""
16+
17+
# These are various properties for the strategy
18+
name = 'Resurrection'
19+
classifier = {
20+
'memory_depth': 1,
21+
'stochastic': False,
22+
'makes_use_of': set(),
23+
'long_run_time': False,
24+
'inspects_source': False,
25+
'manipulates_source': False,
26+
'manipulates_state': False
27+
}
28+
29+
def strategy(self, opponent: Player) -> Action:
30+
if len(self.history) == 0:
31+
return C
32+
if len(self.history) >= 5 and self.history[-5:] == [D, D, D, D, D]:
33+
return D
34+
else:
35+
return opponent.history[-1]
36+
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""Test for the Resurrection strategy."""
2+
3+
import axelrod
4+
from .test_player import TestPlayer
5+
6+
C, D = axelrod.Actions.C, axelrod.Actions.D
7+
class Resurrection(TestPlayer):
8+
9+
name = "Resurrection"
10+
player = axelrod.Resurrection
11+
expected_classifier = {
12+
'memory_depth': 1,
13+
'stochastic': False,
14+
'makes_use_of': set(),
15+
'long_run_time': False,
16+
'inspects_source': False,
17+
'manipulates_source': False,
18+
'manipulates_state': False
19+
}
20+
21+
def test_strategy(self):
22+
# Starts by Cooperating
23+
self.first_play_test(C)
24+
25+
# Check if the turns played are greater than 5
26+
self.responses_test([D], [D, C, C, D, D, D, D, D] , [C] * 8)
27+
28+
#Check if turns played are less than 5.
29+
self.responses_test([C], [D, C, D, C], [C] * 4)
30+
31+
# Check for TFT behavior after 5 rounds
32+
self.responses_test([C], [C] * 5, [C, C, C, C, C, D])
33+
34+
# Check for four defections and a cooperation by opponent
35+
self.responses_test([D], [C] * 5, [D, D, D, D, D, C])

docs/reference/all_strategies.rst

+3
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ Here are the docstrings of all the strategies in the library.
126126
.. automodule:: axelrod.strategies.rand
127127
:members:
128128
:undoc-members:
129+
.. automodule:: axelrod.strategies.resurrection
130+
:members:
131+
:undoc-members:
129132
.. automodule:: axelrod.strategies.retaliate
130133
:members:
131134
:undoc-members:

docs/tutorials/advanced/classification_of_strategies.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ make a decision::
5757
... }
5858
>>> strategies = axl.filtered_strategies(filterset)
5959
>>> len(strategies)
60-
27
60+
28
6161

6262
Multiple filters can be specified within the filterset dictionary. To specify a
6363
range of memory_depth values, we can use the 'min_memory_depth' and
@@ -69,7 +69,7 @@ range of memory_depth values, we can use the 'min_memory_depth' and
6969
... }
7070
>>> strategies = axl.filtered_strategies(filterset)
7171
>>> len(strategies)
72-
48
72+
49
7373

7474
We can also identify strategies that make use of particular properties of the
7575
tournament. For example, here is the number of strategies that make use of the

0 commit comments

Comments
 (0)