diff --git a/axelrod/strategies/_strategies.py b/axelrod/strategies/_strategies.py index bc80eeccc..f9d85100d 100644 --- a/axelrod/strategies/_strategies.py +++ b/axelrod/strategies/_strategies.py @@ -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 @@ -294,6 +295,7 @@ APavlov2006, APavlov2011, Adaptive, + AdaptiveCooperator, AdaptiveTitForTat, AdaptorBrief, AdaptorLong, diff --git a/axelrod/strategies/adaptive_cooperator.py b/axelrod/strategies/adaptive_cooperator.py new file mode 100644 index 000000000..8596df227 --- /dev/null +++ b/axelrod/strategies/adaptive_cooperator.py @@ -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 + diff --git a/axelrod/tests/strategies/test_adaptive_cooperator.py b/axelrod/tests/strategies/test_adaptive_cooperator.py new file mode 100644 index 000000000..fa9d1f063 --- /dev/null +++ b/axelrod/tests/strategies/test_adaptive_cooperator.py @@ -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) diff --git a/docs/index.rst b/docs/index.rst index 82b9f41b5..a379bbc7f 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -53,7 +53,7 @@ Count the number of available players:: >>> import axelrod as axl >>> len(axl.strategies) - 243 + 244 Create matches between two players:: diff --git a/docs/reference/strategy_index.rst b/docs/reference/strategy_index.rst index 9764d3082..7a656a1c3 100644 --- a/docs/reference/strategy_index.rst +++ b/docs/reference/strategy_index.rst @@ -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