Skip to content

SpitefulCC from [Mathieu2015] #1364

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

Merged
merged 6 commits into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions axelrod/strategies/_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
GrudgerAlternator,
OppositeGrudger,
SoftGrudger,
SpitefulCC,
)
from .grumpy import Grumpy
from .handshake import Handshake
Expand Down Expand Up @@ -467,6 +468,7 @@
SolutionB1,
SolutionB5,
SpitefulTitForTat,
SpitefulCC,
Stalker,
StochasticCooperator,
StochasticWSLS,
Expand Down
34 changes: 33 additions & 1 deletion axelrod/strategies/grudger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -310,3 +310,35 @@ 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,
"long_run_time": False,
"inspects_source": False,
"manipulates_source": False,
"manipulates_state": False,
}

@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:
return D
return C
33 changes: 32 additions & 1 deletion axelrod/tests/strategies/test_grudger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion axelrod/tests/strategies/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
3 changes: 1 addition & 2 deletions 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)
236
237

Create matches between two players::

Expand Down Expand Up @@ -111,4 +111,3 @@ Indices and tables
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`