Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRG] Add Resurrection Strategy #865

Merged
merged 17 commits into from
Mar 1, 2017
Merged
2 changes: 2 additions & 0 deletions axelrod/strategies/_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
from .qlearner import (
RiskyQLearner, ArrogantQLearner, HesitantQLearner, CautiousQLearner)
from .rand import Random
from .resurrection import Resurrection
from .retaliate import (
Retaliate, Retaliate2, Retaliate3, LimitedRetaliate, LimitedRetaliate2,
LimitedRetaliate3)
Expand Down Expand Up @@ -204,6 +205,7 @@
Random,
RandomHunter,
RemorsefulProber,
Resurrection,
Retaliate,
Retaliate2,
Retaliate3,
Expand Down
36 changes: 36 additions & 0 deletions axelrod/strategies/resurrection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from axelrod.actions import Actions, Action
from axelrod.player import Player

C, D = Actions.C, Actions.D


class Resurrection(Player):
"""
A player starts by cooperating and defects if the number of rounds
played by the player is greater than five and the last five rounds are defections.
Otherwise, the strategy plays like Tit-for-tat.

Names:
- Resurrection: Name from CoopSim https://github.com/jecki/CoopSim
"""

# These are various properties for the strategy
name = 'Resurrection'
classifier = {
'memory_depth': 1,
'stochastic': False,
'makes_use_of': set(),
'long_run_time': False,
'inspects_source': False,
'manipulates_source': False,
'manipulates_state': False
}

def strategy(self, opponent: Player) -> Action:
if len(self.history) == 0:
return C
if len(self.history) >= 5 and self.history[-5:] == [D, D, D, D, D]:
return D
else:
return opponent.history[-1]

35 changes: 35 additions & 0 deletions axelrod/tests/unit/test_resurrection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""Test for the Resurrection strategy."""

import axelrod
from .test_player import TestPlayer

C, D = axelrod.Actions.C, axelrod.Actions.D
class Resurrection(TestPlayer):

name = "Resurrection"
player = axelrod.Resurrection
expected_classifier = {
'memory_depth': 1,
'stochastic': False,
'makes_use_of': set(),
'long_run_time': False,
'inspects_source': False,
'manipulates_source': False,
'manipulates_state': False
}

def test_strategy(self):
# Starts by Cooperating
self.first_play_test(C)

# Check if the turns played are greater than 5
self.responses_test([D], [D, C, C, D, D, D, D, D] , [C] * 8)

#Check if turns played are less than 5.
self.responses_test([C], [D, C, D, C], [C] * 4)

# Check for TFT behavior after 5 rounds
self.responses_test([C], [C] * 5, [C, C, C, C, C, D])

# Check for four defections and a cooperation by opponent
self.responses_test([D], [C] * 5, [D, D, D, D, D, C])
3 changes: 3 additions & 0 deletions docs/reference/all_strategies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ Here are the docstrings of all the strategies in the library.
.. automodule:: axelrod.strategies.rand
:members:
:undoc-members:
.. automodule:: axelrod.strategies.resurrection
:members:
:undoc-members:
.. automodule:: axelrod.strategies.retaliate
:members:
:undoc-members:
Expand Down
4 changes: 2 additions & 2 deletions docs/tutorials/advanced/classification_of_strategies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ make a decision::
... }
>>> strategies = axl.filtered_strategies(filterset)
>>> len(strategies)
27
28

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

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