Skip to content

Add Tricky Level Punisher #1178

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 9 commits into from
May 14, 2018
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
3 changes: 2 additions & 1 deletion axelrod/strategies/_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
from .oncebitten import OnceBitten, FoolMeOnce, ForgetfulFoolMeOnce, FoolMeForever
from .prober import (CollectiveStrategy, Prober, Prober2, Prober3, Prober4,
HardProber, NaiveProber, RemorsefulProber)
from .punisher import Punisher, InversePunisher, LevelPunisher
from .punisher import Punisher, InversePunisher, LevelPunisher, TrickyLevelPunisher
from .qlearner import (
RiskyQLearner, ArrogantQLearner, HesitantQLearner, CautiousQLearner)
from .rand import Random
Expand Down Expand Up @@ -279,6 +279,7 @@
Tranquilizer,
TrickyCooperator,
TrickyDefector,
TrickyLevelPunisher,
Tullock,
TwoTitsForTat,
VeryBad,
Expand Down
37 changes: 37 additions & 0 deletions axelrod/strategies/punisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,40 @@ def strategy(self, opponent: Player) -> Action:
return D
else:
return C


class TrickyLevelPunisher(Player):
"""
A player starts by cooperating however, after 10, 50 and 100 rounds
will defect if at any point the percentage of defections
by an opponent is greater than 20%, 10% and 5% respectively.

Names:

- Tricky Level Punisher: [Eckhart2015]_
"""

name = 'Tricky Level Punisher'
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 strategy(self, opponent: Player) -> Action:
if len(opponent.history) == 0:
return C
if len(opponent.history) < 10:
if opponent.defections / len(opponent.history) > 0.2:
return D
if len(opponent.history) < 50:
if opponent.defections / len(opponent.history) > 0.1:
return D
if len(opponent.history) < 100:
if opponent.defections / len(opponent.history) > 0.05:
return D
return C
2 changes: 1 addition & 1 deletion axelrod/tests/strategies/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def test_strategy(self):
self.versus_test(opponent=axelrod.Alternator(),
expected_actions=actions, seed=0)

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

Expand Down
43 changes: 43 additions & 0 deletions axelrod/tests/strategies/test_punisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,46 @@ def test_strategy(self):
opponent = axelrod.MockPlayer([C] * 4 + [D] + [C] * 4 + [D])
actions = [(C, C)] * 4 + [(C, D)] + [(C, C)] * 4 + [(C, D), (C, C)]
self.versus_test(opponent=opponent, expected_actions=actions)


class TestTrickyLevelPunisher(TestPlayer):

name = "Level Punisher"
player = axelrod.LevelPunisher
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):
# Cooperates if the turns played are less than 10.
actions = [(C, C)] * 9
self.versus_test(opponent=axelrod.Cooperator(),
expected_actions=actions)

# After 10 rounds
# Check if number of defections by opponent is greater than 20%
opponent = axelrod.MockPlayer([C] * 4 + [D] * 2 + [C] * 3 + [D])
actions = [(C, C)] * 4 + [(C, D)] * 2 + [(C, C)] * 3 + [(C, D), (D, C)]
self.versus_test(opponent=opponent, expected_actions=actions)

# Check if number of defections by opponent is greater than 10%
opponent = axelrod.MockPlayer([C] * 4 + [D] + [C] * 4 + [D])
actions = [(C, C)] * 4 + [(C, D)] + [(C, C)] * 4 + [(C, D), (C, C)]
self.versus_test(opponent=opponent, expected_actions=actions)

# After 10 rounds
# Check if number of defections by opponent is greater than 5%
opponent = axelrod.MockPlayer([C] * 4 + [D] + [C] * 5)
actions = [(C, C)] * 4 + [(C, D)] + [(C, C)] * 5
self.versus_test(opponent=opponent, expected_actions=actions)

# Check if number of defections by opponent is less than 5%
opponent = axelrod.MockPlayer([C]*10)
actions = [(C, C)] * 5
self.versus_test(opponent=opponent, expected_actions=actions)