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

Michaelos strategy #1091

Merged
merged 24 commits into from
Jul 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
4 changes: 2 additions & 2 deletions axelrod/strategies/_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@
TitForTat, TitFor2Tats, TwoTitsForTat, Bully, SneakyTitForTat,
SuspiciousTitForTat, AntiTitForTat, HardTitForTat, HardTitFor2Tats,
OmegaTFT, Gradual, ContriteTitForTat, AdaptiveTitForTat,
SpitefulTitForTat, SlowTitForTwoTats2, Alexei, EugineNier,
DynamicTwoTitsForTat, NTitsForMTats)
SpitefulTitForTat, SlowTitForTwoTats2, Alexei, EugineNier, DynamicTwoTitsForTat, NTitsForMTats, Michaelos)
from .verybad import VeryBad
from .worse_and_worse import (WorseAndWorse, KnowledgeableWorseAndWorse,
WorseAndWorse2, WorseAndWorse3)
Expand Down Expand Up @@ -191,6 +190,7 @@
MathConstantHunter,
NaiveProber,
MEM2,
Michaelos,
MindBender,
MindController,
MindReader,
Expand Down
49 changes: 49 additions & 0 deletions axelrod/strategies/titfortat.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,3 +778,52 @@ def strategy(self, opponent: Player) -> Action:
def reset(self):
super().reset()
self.retaliate_count = 0


@FinalTransformer((D,), name_prefix=None)
class Michaelos(Player):
"""
Plays similar to Tit-for-Tat with two exceptions:
1) Defect on last turn.
2) After own defection and opponent's cooperation, 50 percent of the time,
cooperate. The other 50 percent of the time, always defect for the rest of
the game.

Names:

- Michaelos: [LessWrong2011]_
"""

name = 'Michaelos'
classifier = {
'memory_depth': float('inf'),
'stochastic': True,
'makes_use_of': {'length'},
'long_run_time': False,
'inspects_source': False,
'manipulates_source': False,
'manipulates_state': False
}

def __init__(self):
super().__init__()
self.is_defector = False

def strategy(self, opponent: Player) -> Action:
if not self.history:
return C
if self.is_defector:
return D
if self.history[-1] == D and opponent.history[-1] == C:
decision = random_choice()
if decision == C:
return C
else:
self.is_defector = True
return D

return opponent.history[-1]

def reset(self):
super().reset()
self.is_defector = False
51 changes: 51 additions & 0 deletions axelrod/tests/strategies/test_titfortat.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,3 +717,54 @@ def test_varying_memory_depth(self):
self.assertEqual(self.player(1, 1).classifier['memory_depth'], 1)
self.assertEqual(self.player(0, 3).classifier['memory_depth'], 3)
self.assertEqual(self.player(5, 3).classifier['memory_depth'], 5)


class TestMichaelos(TestPlayer):
"""
Tests for the Michaelos strategy
"""

name = "Michaelos: (D,)"
player = axelrod.Michaelos
expected_classifier = {
'memory_depth': float('inf'),
'stochastic': True,
'makes_use_of': {'length'},
'inspects_source': False,
'manipulates_source': False,
'manipulates_state': False
}

def test_strategy(self):

actions = [(C, C), (C, C), (C, C), (D, C)]
self.versus_test(axelrod.Cooperator(), expected_actions=actions,
attrs={"is_defector": False}, seed=2)

actions = [(C, C), (C, C), (C, C), (C, C)]
self.versus_test(axelrod.Cooperator(), expected_actions=actions,
attrs={"is_defector": False},
match_attributes={"length": -1}, seed=2)

actions = [(C, D), (D, D), (D, D), (D, D)]
self.versus_test(axelrod.Defector(), expected_actions=actions,
attrs={"is_defector": False}, seed=2)

actions = [(C, D), (D, D), (D, D), (D, D)]
self.versus_test(axelrod.Defector(), expected_actions=actions,
attrs={"is_defector": False},
match_attributes={"length": -1}, seed=2)

# Chance of becoming a defector is 50% after (D, C) occurs.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you repeat this test with a different seed to show differing behaviour after (C, D) (so only need 4 round please):

actions = [(C, C), (C, D), (C, C), (C, D)]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, I don't think this scenario is possible for the strategy unless I am misinterpreting something. After (C, D) Michaelos will play D since it is still playing like Tit-for-Tat.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the second test, does tweaking the test mean changing the expected actions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After (C, D) Michaelos will play D since it is still playing like Tit-for-Tat.

Sorry you're correct, that was a typo from me, I simply mean to include a random seed version to illustrate that 50% of the time after (D, C) (not (C, D) - sorry) the strategy will cooperate **and ** is_defector will be False (so kind of like your seed=1 test but without the second (C, D).

For the second test, does tweaking the test mean changing the expected actions?

Yes.

actions = [(C, C), (C, D), (D, C), (C, D), (D, C)]
self.versus_test(axelrod.Alternator(), expected_actions=actions,
attrs={"is_defector": False}, seed=3)

actions = [(C, C), (C, D), (D, C), (D, D), (D, C), (D, D), (D, C)]
self.versus_test(axelrod.Alternator(), expected_actions=actions,
attrs={"is_defector": True}, seed=2)

actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (D, D), (D, C)]
self.versus_test(axelrod.Alternator(), expected_actions=actions,
attrs={"is_defector": True},
match_attributes={"length": -1}, seed=1)
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 @@ -47,7 +47,7 @@ strategies::
... }
>>> strategies = axl.filtered_strategies(filterset)
>>> len(strategies)
71
72

Or, to find out how many strategies only use 1 turn worth of memory to
make a decision::
Expand Down Expand Up @@ -80,7 +80,7 @@ length of each match of the tournament::
... }
>>> strategies = axl.filtered_strategies(filterset)
>>> len(strategies)
28
29

Note that in the filterset dictionary, the value for the 'makes_use_of' key
must be a list. Here is how we might identify the number of strategies that use
Expand Down