Skip to content

Commit

Permalink
Merge pull request #1016 from souravsingh/new-strategy
Browse files Browse the repository at this point in the history
Add EugineNier strategy from LessWrong
  • Loading branch information
marcharper authored May 25, 2017
2 parents ed8c536 + f5f89a6 commit 592823b
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 5 deletions.
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, SlowTitForTwoTats, AdaptiveTitForTat,
SpitefulTitForTat, SlowTitForTwoTats2, Alexei)
SpitefulTitForTat, SlowTitForTwoTats2, Alexei, EugineNier)
from .verybad import VeryBad
from .worse_and_worse import (WorseAndWorse, KnowledgeableWorseAndWorse,
WorseAndWorse2, WorseAndWorse3)
Expand Down Expand Up @@ -126,6 +126,7 @@
DoubleResurrection,
EasyGo,
Eatherley,
EugineNier,
EventualCycleHunter,
EvolvedANN,
EvolvedANN5,
Expand Down
44 changes: 42 additions & 2 deletions axelrod/strategies/titfortat.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,10 +642,10 @@ def strategy(self, opponent: Player) -> Action:
class Alexei(Player):
"""
Plays similar to Tit-for-Tat, but always defect on last turn.
Names:
- Alexei's Strategy: [LessWrong2011]_
- Alexei: [LessWrong2011]_
"""

name = 'Alexei'
Expand All @@ -665,3 +665,43 @@ def strategy(self, opponent: Player) -> Action:
if opponent.history[-1] == D:
return D
return C

@FinalTransformer((D,), name_prefix=None)
class EugineNier(Player):
"""
Plays similar to Tit-for-Tat, but with two conditions:
1) Always Defect on Last Move
2) If other player defects five times, switch to all defects.
Names:
- Eugine Nier: [LessWrong2011]_
"""

name = 'EugineNier'
classifier = {
'memory_depth': float('inf'),
'stochastic': False,
'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 not (self.is_defector) and opponent.defections >= 5:
self.is_defector = True
if self.is_defector:
return D
return opponent.history[-1]

def reset(self):
super().reset()
self.is_defector = False
48 changes: 47 additions & 1 deletion axelrod/tests/strategies/test_titfortat.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,10 +593,56 @@ def test_strategy(self):
actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D)]
self.versus_test(axelrod.Alternator(), expected_actions=actions,
match_attributes={"length": -1})

actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (D, D)]
self.versus_test(axelrod.Alternator(), expected_actions=actions)

opponent = axelrod.MockPlayer(actions=[C, C, D, D, C, D])
actions = [(C, C), (C, C), (C, D), (D, D), (D, C), (D, D)]
self.versus_test(opponent, expected_actions=actions)

class TestEugineNier(TestPlayer):
"""
Tests for the Eugine Nier strategy
"""

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

def test_strategy(self):
self.first_play_test(C)
self.second_play_test(rCC=C, rCD=D, rDC=C, rDD=D)

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

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})


# Plays TfT and defects in last round
actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (D, D)]
self.versus_test(axelrod.Alternator(), expected_actions=actions,
attrs={"is_defector": False})

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

# Becomes defector after 5 defections
opponent = axelrod.MockPlayer(actions=[D, C, D, D, D, D, C, C])
actions = [(C, D), (D, C), (C, D), (D, D),
(D, D), (D, D), (D, C), (D, C)]
self.versus_test(opponent, expected_actions=actions)
2 changes: 1 addition & 1 deletion docs/tutorials/advanced/classification_of_strategies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ length of each match of the tournament::
... }
>>> strategies = axl.filtered_strategies(filterset)
>>> len(strategies)
27
28

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 592823b

Please sign in to comment.