Skip to content
2 changes: 2 additions & 0 deletions axelrod/strategies/_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import warnings

from .adaptive import Adaptive
from .adaptive_cooperator import AdaptiveCooperator
from .adaptor import AdaptorBrief, AdaptorLong
from .alternator import Alternator
from .ann import EvolvedANN, EvolvedANN5, EvolvedANNNoise05
Expand Down Expand Up @@ -294,6 +295,7 @@
APavlov2006,
APavlov2011,
Adaptive,
AdaptiveCooperator,
AdaptiveTitForTat,
AdaptorBrief,
AdaptorLong,
Expand Down
63 changes: 63 additions & 0 deletions axelrod/strategies/adaptive_cooperator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from axelrod.action import Action
from axelrod.player import Player

C, D = Action.C, Action.D

class AdaptiveCooperator(Player):
"""
This is an adaptive strategy where the player starts
by cooperating, but switches to Tit-For-Tat if the
opponent's score exceed's its own score. Then, it
switches to Defector if the opponent's score is twice
its own score.
"""

name = 'Adaptive Cooperator'
classifier = {
"memory_depth": 1,
'stochastic': False,
'inspects_source': False,
'manipulates_source': False,
'manipulates_state': False
}

def __init__(self):
super().__init__()
self.is_tft = False
self.is_defect = False
self.current_score = 0
self.opponent_score = 0

def _score_last_round(self, opponent):
game = self.match_attributes["game"]
last_round = (self.history[-1], opponent.history[-1])
scores = game.score(last_round)
self.current_score += scores[0]
self.opponent_score += scores[1]

def strategy(self, opponent):
turn = len(self.history) + 1
if turn > 1:
self._score_last_round(opponent)

if self.opponent_score > self.current_score * 2:
self.is_defect = True
elif self.opponent_score > self.current_score:
self.is_tft = True

#response to strategies
if self.is_defect:
return D
elif self.is_tft:
if not opponent.history or len(self.history) == 0:
return C
else:
return opponent.history[-1]
else: #cooperate
return C

def reset(self):
super().__init__()
self.is_defect = False
self.is_tft = False

31 changes: 31 additions & 0 deletions axelrod/tests/strategies/test_adaptive_cooperator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import axelrod
from .test_player import TestPlayer

C, D = axelrod.Action.C, axelrod.Action.D

class TestAdaptiveCooperator(TestPlayer):
name = 'Adaptive Cooperator'
player = axelrod.AdaptiveCooperator
expected_classifier = {
"memory_depth": 1,
'stochastic': False,
'inspects_source': False,
'manipulates_source': False,
'manipulates_state': False
}

def test_strategy_cooperator(self):
actions = [(C, C), (C, C), (C, C), (C, C), (C, C)]
self.versus_test(axelrod.Cooperator(), expected_actions=actions)

def test_strategy_defector(self):
actions = [(C, D), (D, D), (D, D), (D, D), (D, D)]
self.versus_test(axelrod.Defector(), expected_actions=actions)

def test_strategy_alternator(self):
actions = [(C, C), (C, D), (D, C), (D, D), (D, C)]
self.versus_test(axelrod.Alternator(), expected_actions=actions)

def test_strategy_tit4tat(self):
actions = [(C, C), (C, C), (C, C), (C, C), (C, C)]
self.versus_test(axelrod.TitForTat(), expected_actions=actions)
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Count the number of available players::

>>> import axelrod as axl
>>> len(axl.strategies)
243
244

Create matches between two players::

Expand Down
2 changes: 2 additions & 0 deletions docs/reference/strategy_index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Here are the docstrings of all the strategies in the library.

.. automodule:: axelrod.strategies.adaptive
:members:
.. automodule:: axelrod.strategies.adaptive_cooperator
:members:
.. automodule:: axelrod.strategies.adaptor
:members:
.. automodule:: axelrod.strategies.alternator
Expand Down
Loading