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 22 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
3 changes: 2 additions & 1 deletion axelrod/strategies/_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +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 @@ -186,6 +186,7 @@
MathConstantHunter,
NaiveProber,
MEM2,
Michaelos,
MindBender,
MindController,
MindReader,
Expand Down
53 changes: 52 additions & 1 deletion axelrod/strategies/titfortat.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,7 @@ def strategy(self, opponent: Player) -> Action:
# Otherwise play previous move
return self.history[-1]


@FinalTransformer((D,), name_prefix=None)
class Alexei(Player):
"""
Expand Down Expand Up @@ -677,6 +678,7 @@ def strategy(self, opponent: Player) -> Action:
return D
return C


@FinalTransformer((D,), name_prefix=None)
class EugineNier(Player):
"""
Expand Down Expand Up @@ -761,7 +763,7 @@ def __init__(self, N: int=3, M: int=2) -> None:
super().__init__()
self.N = N
self.M = M
self.classifier['memory_depth'] = max([M,N])
self.classifier['memory_depth'] = max([M, N])
self.retaliate_count = 0

def strategy(self, opponent: Player) -> Action:
Expand All @@ -772,3 +774,52 @@ def strategy(self, opponent: Player) -> Action:
self.retaliate_count -= 1
return D
return C


@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': 1,
Copy link
Member

Choose a reason for hiding this comment

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

I believe that the memory depth is not 1.

If on turn n Michaelos defects and the opponent cooperates then 50% of the time I defect and defect throughout, However if I have already become a defector than I will defect 100% of the time so I not only need to know the previous turns (that I D and you C but I also need to know if I D'd because I have already "transformed" in to a defector).

I believe the memory depth is in fact float('inf'):

If our actions are ...(D, D), (D, C) then I do not know if my previous plays of D are TfT retaliations or because I have previously been turned in to a defector so I do not know if I need to randomly sample as a result of the last round being (D, C).

'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):
Copy link
Member

Choose a reason for hiding this comment

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

PEP8:

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
47 changes: 47 additions & 0 deletions axelrod/tests/strategies/test_titfortat.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,3 +717,50 @@ 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': 1,
'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), (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)
8 changes: 4 additions & 4 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)
67
68

Or, to find out how many strategies only use 1 turn worth of memory to
make a decision::
Expand All @@ -57,7 +57,7 @@ make a decision::
... }
>>> strategies = axl.filtered_strategies(filterset)
>>> len(strategies)
28
29

Multiple filters can be specified within the filterset dictionary. To specify a
range of memory_depth values, we can use the 'min_memory_depth' and
Expand All @@ -69,7 +69,7 @@ range of memory_depth values, we can use the 'min_memory_depth' and
... }
>>> strategies = axl.filtered_strategies(filterset)
>>> len(strategies)
51
52

We can also identify strategies that make use of particular properties of the
tournament. For example, here is the number of strategies that make use of the
Expand All @@ -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