From 35810456f21e8fae147dcbba9e7c1983c772de1e Mon Sep 17 00:00:00 2001 From: Laura Romero Date: Fri, 7 Aug 2020 19:37:09 +0200 Subject: [PATCH 1/5] Added new SpitefulCC Player to grudger.py, updated the list of all strategies and added coverage test in test_grudger.py --- axelrod/strategies/_strategies.py | 2 ++ axelrod/strategies/grudger.py | 31 +++++++++++++++++++++- axelrod/tests/strategies/test_grudger.py | 33 +++++++++++++++++++++++- 3 files changed, 64 insertions(+), 2 deletions(-) diff --git a/axelrod/strategies/_strategies.py b/axelrod/strategies/_strategies.py index 752e56f20..05c0408ed 100644 --- a/axelrod/strategies/_strategies.py +++ b/axelrod/strategies/_strategies.py @@ -142,6 +142,7 @@ GrudgerAlternator, OppositeGrudger, SoftGrudger, + SpitefulCC, ) from .grumpy import Grumpy from .handshake import Handshake @@ -467,6 +468,7 @@ SolutionB1, SolutionB5, SpitefulTitForTat, + SpitefulCC, Stalker, StochasticCooperator, StochasticWSLS, diff --git a/axelrod/strategies/grudger.py b/axelrod/strategies/grudger.py index a646851af..3623720db 100644 --- a/axelrod/strategies/grudger.py +++ b/axelrod/strategies/grudger.py @@ -24,7 +24,7 @@ class Grudger(Player): name = "Grudger" classifier = { - "memory_depth": float('inf'), + "memory_depth": float("inf"), "stochastic": False, "long_run_time": False, "inspects_source": False, @@ -310,3 +310,32 @@ def strategy(self, opponent: Player) -> Action: def __repr__(self) -> str: return "%s: n=%s,d=%s,c=%s" % (self.name, self.n, self.d, self.c) + + +class SpitefulCC(Player): + """ + Behaves like Grudger after cooperating for 2 turns + + Names: + + - spiteful_cc: [Mathieu2015]_ + """ + + name = "SpitefulCC" + classifier = { + "memory_depth": float("inf"), # Long memory + "stochastic": False, + "makes_use_of": set(), + "long_run_time": False, + "inspects_source": False, + "manipulates_source": False, + "manipulates_state": False, + } + + @staticmethod + def strategy(opponent: Player) -> Action: + if len(opponent.history) < 2: + return C + elif opponent.defections: + return D + return C diff --git a/axelrod/tests/strategies/test_grudger.py b/axelrod/tests/strategies/test_grudger.py index 79194b5d8..9bd980b89 100644 --- a/axelrod/tests/strategies/test_grudger.py +++ b/axelrod/tests/strategies/test_grudger.py @@ -12,7 +12,7 @@ class TestGrudger(TestPlayer): name = "Grudger" player = axl.Grudger expected_classifier = { - "memory_depth": float('inf'), + "memory_depth": float("inf"), "stochastic": False, "makes_use_of": set(), "long_run_time": False, @@ -276,3 +276,34 @@ def test_strategy(self): expected_actions=actions, init_kwargs={"n": 1, "d": 1, "c": 1}, ) + + +class TestSpitefulCC(TestPlayer): + + name = "SpitefulCC" + player = axl.SpitefulCC + expected_classifier = { + "memory_depth": float("inf"), # Long memory + "stochastic": False, + "makes_use_of": set(), + "long_run_time": False, + "inspects_source": False, + "manipulates_source": False, + "manipulates_state": False, + } + + def test_strategy(self): + # If opponent defects at any point then the player will defect forever. + # Cooperates for the first 2 turns. + opponent = axl.Cooperator() + actions = [(C, C)] * 20 + self.versus_test(opponent, expected_actions=actions) + + opponent = axl.Defector() + actions = [(C, D)] * 2 + [(D, D)] * 20 + self.versus_test(opponent, expected_actions=actions) + + opponent_actions = [D] * 20 + [C] * 20 + opponent = axl.MockPlayer(actions=opponent_actions) + actions = [(C, D)] * 2 + [(D, D)] * 18 + [(D, C)] * 20 + self.versus_test(opponent, expected_actions=actions) From f81b061d724586266c3943cd252c2a4390f47680 Mon Sep 17 00:00:00 2001 From: Laura Romero Date: Sat, 8 Aug 2020 17:11:00 +0200 Subject: [PATCH 2/5] changed 236 strategies to 237 in docs/index.rst --- docs/index.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index a0f933d12..f1f5c73ef 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) - 236 + 237 Create matches between two players:: @@ -111,4 +111,3 @@ Indices and tables * :ref:`genindex` * :ref:`modindex` * :ref:`search` - From 1d2f4e664549c795c075ba0e5d7e1b384e423f36 Mon Sep 17 00:00:00 2001 From: Laura Romero Date: Sat, 8 Aug 2020 17:33:02 +0200 Subject: [PATCH 3/5] changed SpitefulCC classifier and added strategy description --- axelrod/strategies/grudger.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/axelrod/strategies/grudger.py b/axelrod/strategies/grudger.py index 3623720db..dfba4dd55 100644 --- a/axelrod/strategies/grudger.py +++ b/axelrod/strategies/grudger.py @@ -325,7 +325,6 @@ class SpitefulCC(Player): classifier = { "memory_depth": float("inf"), # Long memory "stochastic": False, - "makes_use_of": set(), "long_run_time": False, "inspects_source": False, "manipulates_source": False, @@ -334,6 +333,10 @@ class SpitefulCC(Player): @staticmethod def strategy(opponent: Player) -> Action: + """ + Cooperates until the oponent defects. Then defects forever. + Always cooperates twice at the start. + """ if len(opponent.history) < 2: return C elif opponent.defections: From 0e5080333f60f7a10c77d4a2620bfa33b7d76d33 Mon Sep 17 00:00:00 2001 From: Laura Romero Date: Sat, 8 Aug 2020 20:20:16 +0200 Subject: [PATCH 4/5] changed description spitefulCC strategy --- axelrod/strategies/grudger.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/axelrod/strategies/grudger.py b/axelrod/strategies/grudger.py index dfba4dd55..c4e197e1c 100644 --- a/axelrod/strategies/grudger.py +++ b/axelrod/strategies/grudger.py @@ -334,7 +334,7 @@ class SpitefulCC(Player): @staticmethod def strategy(opponent: Player) -> Action: """ - Cooperates until the oponent defects. Then defects forever. + Cooperates until the oponent defects, then defects forever. Always cooperates twice at the start. """ if len(opponent.history) < 2: From 43930e82e1c026ce9d2684b4419d596e57842bf2 Mon Sep 17 00:00:00 2001 From: RomeroLaura <35375163+RomeroLaura@users.noreply.github.com> Date: Wed, 19 Aug 2020 15:51:39 +0200 Subject: [PATCH 5/5] fix test_meta.py change line 641 of axelrod/tests/strategies/test_meta.py --- axelrod/tests/strategies/test_meta.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/axelrod/tests/strategies/test_meta.py b/axelrod/tests/strategies/test_meta.py index 6ec22db6b..971dae521 100644 --- a/axelrod/tests/strategies/test_meta.py +++ b/axelrod/tests/strategies/test_meta.py @@ -638,7 +638,7 @@ def classifier_test(self, expected_class_classifier=None): pass def test_strategy(self): - actions = [(C, C), (C, D), (D, C), (D, D), (D, C)] + actions = [(C, C), (C, D), (C, C), (D, D), (D, C)] self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=11)