Skip to content

Commit

Permalink
Merge pull request #1091 from dmanc/master
Browse files Browse the repository at this point in the history
Michaelos strategy
  • Loading branch information
meatballs authored Jul 30, 2017
2 parents 6e4daa5 + e02e232 commit 66bf7b2
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 4 deletions.
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.
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

0 comments on commit 66bf7b2

Please sign in to comment.